【问题标题】:Load multidimensional VBA Array from disk从磁盘加载多维 VBA 数组
【发布时间】:2013-08-09 11:41:36
【问题描述】:

我正在尝试将多维 VBA 数组保存到磁盘或从磁盘加载。根据MSDN website,维数作为描述符保存在文件中,但我不知道如何访问/加载它们。下面的示例有效,但这只是因为我对数组维度进行了硬编码。注释掉的行在动态意义上起作用,但在此过程中会丢失数组的维度。

下面是一些示例代码:

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

我需要感谢 VB Helper 网站,因为上面的示例基于他们发布的 here

【问题讨论】:

    标签: arrays vba multidimensional-array


    【解决方案1】:

    说实话,我不知道这种允许将数组写入文本文件的 VBA 技术。或者也许我忘记了。 :) 因此我潜入了它。

    第一。写入文件。

    我对您的数组的 Boolean 类型有一些问题。它不工作,但它正在使用Variant type。我将打开模式从Binary 更改为Random。此外,我将Len parameter 用于Open Statement,其值根据this MSDN information

    这是第一个改进的子:

    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
    

    第二。从文件中读取

    我们的数组将是Variant type dynamic。我将文件打开类型从Binary 更改为Random,并根据this MSDN information 使用具有最大可能值的Len parameter

    这是第二个改进的子:

    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
    

    变量值的屏幕截图。

    【讨论】:

    • 这很有帮助。至少我现在可以让它运行起来。我注意到最大文件长度为 32,767,但我认为它适用于“不同”类型的文件。既然我意识到它会影响我,我可能不得不将我的数组保存为碎片(重新加载和组装它们仍然比重新运行它们更快)。我希望找到一个解决方案,我可以使用由 VBA“自动”写入磁盘的数组描述符。但是,似乎没有人真正将它用于多维数组的功能(或者,至少我在网上找不到任何示例)。
    • @D.Woods,根据您的目标,您可以探索ADODB 将数据写入文件的功能或FileSystemObject 作为替代方案。
    • 感谢您的额外建议。我对 ADODB 不熟悉,所以我看了一下。不幸的是,我认为它不适用于我的情况,因为它需要 Windows 版本的 Excel。我正在为多用户环境进行开发,其中产品需要在 OS X 和 Windows 版本的 Excel 上都可用。
    猜你喜欢
    • 2017-07-28
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 2018-10-23
    相关资源
    最近更新 更多