从磁盘加载多维VBA阵列 [英] Load multidimensional VBA Array from disk

查看:73
本文介绍了从磁盘加载多维VBA阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存多维VBA阵列,然后将其加载到磁盘或从磁盘加载.根据 MSDN网站 ,维度的数量作为描述符保存在文件中,但是我不知道如何访问/加载它们.下面的示例有效,但这仅是因为我已经对数组维进行了硬编码.注释掉的行是动态的,但是在此过程中会丢失数组的维数.

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.

下面是一些示例代码:

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网站,因为上面的示例基于他们发布的内容这里.

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

推荐答案

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

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.

第一名.写入文件.

我的数组Boolean类型存在一些问题.它不起作用,但可以与Variant type一起使用.然后我将打开模式从Binary更改为Random.此外,我将Len parameter用于Open Statement,其值根据此MSDN信息.

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

第二名.从文件读取

我们的数组将为Variant type dynamic.我将文件打开类型从Binary更改为Random,并根据

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

变量值的屏幕截图.

这篇关于从磁盘加载多维VBA阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆