Load multidimensional VBA Array from disk

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I'm trying to save and then load a multi-dimensional VBA array to/from disk. According to the MSDN website, the number of dimensions are saved as a descriptor in the file, but I can't figure out how to access/load them. The example below works, but only because I have hard coded the array dimensions. The commented out line works in a dynamic sense, but the array's dimensions are lost in the process.

Here's some sample code:

Sub WriteArray() Dim file_name As String Dim file_length As Long Dim fnum As Integer  Dim values() As Boolean ReDim values(1 To 5, 1 To 10, 1 To 20)  Dim i As Integer 'Populate the simple array     For i = 1 To 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 the file. fnum = FreeFile Open file_name For Binary As #fnum Put #fnum, 1, values Close fnum  End Sub  Sub ReadArray() Dim file_name As String Dim file_length As Long Dim fnum As Integer Dim newArray() As Boolean  file_name = "array.to.file.vba.bin" 'txtFile.Text" fnum = FreeFile  file_length = FileLen(file_name) 'ReDim newArray(1 To file_length) 'This loads the data, but not with the right dimensions.  ReDim newArray(1 To 5, 1 To 10, 1 To 20) 'This works but with dimensions hard coded.  'How to re-dim here using the dimensions saved in the file?  Open file_name For Binary As #fnum Get #fnum, 1, newArray Close fnum  End Sub

I need to give credit to the VB Helper website because the example above is based on one they posted here.

回答1:

To be honest I didn't know this VBA technique which allows to write array into text file. Or maybe I forgot it. :) Therefore I dived into it.

1st. Writing to the file.

I have some problems with Boolean type of your array. It's not working but it's working with Variant type. And I changed open mode from Binary to Random. Moreover, I used Len parameter for Open Statement with value according to this MSDN information.

This is the first sub improved:

Sub WriteArray()      Dim file_name As String     Dim file_length As Long     Dim fnum As Integer      Dim values() As Variant     ReDim values(1 To 5, 1 To 10, 1 To 20)      Dim i As Integer 'Populate the simple array         For i = 1 To 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 the file.     fnum = FreeFile      '<<<<<<< this is new >>>>>>>     Dim arrLen As Long         arrLen = (2 + 3 * 8) + (5 * 10 * 20 * 3)      '<<<<<<< this is changed >>>>>>>     Open file_name For Random As #fnum Len = arrLen     Put #fnum, 1, values     Close fnum  End Sub

2nd. Reading from file

Our array will be Variant type dynamic. I changed file open type to Random from Binary and used Len parameter with the max possible value according to this MSDN information.

This is the second sub improved:

Sub ReadArray()     Dim file_name As String     Dim fnum As Integer     Dim newArray() As Variant      file_name = "array.to.file.vba.bin" 'txtFile.Text"     fnum = FreeFile      '<<<<<<< this is new >>>>>>>     Dim lenAAA         lenAAA = 32767  '>>> MAX possible value      '<<<<<<< this is changed >>>>>>>     Open file_name For Random As #fnum Len = lenAAA     Get #fnum, 1, newArray     Close fnum  End Sub

Screen shot of variables value.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!