Load multidimensional VBA Array from disk -
i'm trying save , load multi-dimensional vba array to/from disk. according msdn website, number of dimensions saved descriptor in file, can't figure out how access/load them. example below works, because have hard coded array dimensions. commented out line works in dynamic sense, array's dimensions lost in process.
here's sample code:
sub writearray() dim file_name string dim file_length long dim fnum integer dim values() boolean redim values(1 5, 1 10, 1 20) dim integer 'populate simple array = 1 20 values(1, 1, i) = true next ' delete existing file (if any). file_name = "array.to.file.vba.bin" on error resume next kill file_name on error goto 0 ' save file. fnum = freefile open file_name binary #fnum put #fnum, 1, values close fnum end sub sub readarray() dim file_name string dim file_length long dim fnum integer dim newarray() boolean file_name = "array.to.file.vba.bin" 'txtfile.text" fnum = freefile file_length = filelen(file_name) 'redim newarray(1 file_length) 'this loads data, not right dimensions. redim newarray(1 5, 1 10, 1 20) 'this works dimensions hard coded. 'how re-dim here using dimensions saved in file? open file_name binary #fnum #fnum, 1, newarray close fnum end sub
i need give credit vb helper website because example above based on 1 posted here.
to honest didn't know vba technique allows write array text file. or maybe forgot it. :) therefore dived it.
1st. writing file.
i have problems boolean
type of array. it's not working it's working variant type
. , changed open mode binary
random
. moreover, used len parameter
open statement
value according this msdn information.
this first sub improved:
sub writearray() dim file_name string dim file_length long dim fnum integer dim values() variant redim values(1 5, 1 10, 1 20) dim integer 'populate simple array = 1 20 values(1, 1, i) = true next ' delete existing file (if any). file_name = "array.to.file.vba.bin" on error resume next kill file_name on error goto 0 ' save file. fnum = freefile '<<<<<<< new >>>>>>> dim arrlen long arrlen = (2 + 3 * 8) + (5 * 10 * 20 * 3) '<<<<<<< changed >>>>>>> open file_name random #fnum len = arrlen put #fnum, 1, values close fnum end sub
2nd. reading file
our array variant type dynamic
. changed file open type random
binary
, used len parameter
max possible value according this msdn information.
this second sub improved:
sub readarray() dim file_name string dim fnum integer dim newarray() variant file_name = "array.to.file.vba.bin" 'txtfile.text" fnum = freefile '<<<<<<< new >>>>>>> dim lenaaa lenaaa = 32767 '>>> max possible value '<<<<<<< changed >>>>>>> open file_name random #fnum len = lenaaa #fnum, 1, newarray close fnum end sub
screen shot of variables value.
Comments
Post a Comment