arrays – 填充临时数组以避免在动态多维数组上使用Preserve

作为VBA的初学者,我试图特别了解这些内容,所以原谅我的尘埃.

我有一个子程序,我正在写文本文件中的行.每行用空格分隔,作为文本标识符.我要求将每一行拆分为多维数组.

Sub ReadLines()
Dim LineValues() As String
Dim row As Long, col As Long
Dim DataArray() As String
Dim TempArray() As String
Dim FileContent As String
Dim FilePath As String

FilePath = "c:\mytextfile.txt"
row = 0
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
Close TextFile
LineValues = Split(FileContent, vbCrLf)

For X = LBound(LineValues) To UBound(LineValues) 
 If Len(Trim(LineValues(X))) <> 0 Then
 DataArray = Split(LineValues(X), "'") 
 col = UBound(DataArray)
 TempArray = DataArray
 ReDim DataArray(col, row) 
 For i = LBound(TempArray) To UBound(TempArray)
 DataArray(i, row) = TempArray(i)
 Next i
 End If
 row = row + 1 
Next X

在与ReDim Preserve的多维问题进行斗争之后,我找到了这段代码. (只能修改最后一个维度)我的文本文件中的多维数组将具有未知的列和行,具体取决于用户输入.

此代码正确执行此过程…但只是无法正确存储数组元素!上面的意图是使用临时数组(TempArray),同时我ReDim(并清空)我感兴趣的数组(DataArray),然后将最初从DataArray复制的元素复制回调整大小的维度.

然而,当单步执行代码时,我可以看到每一行都正确放置,但随后每行迭代都会擦除,
    DataArray = Split(LineValues(X),“’”)

我基本上有一个矩阵,其大小由总行数确定,但仅由最后一行的列数(以及最后一行的值)作为结果.

我知道为什么会发生这种情况,但是这里有人可以提出解决方案吗?作为初学者,这有点让人抓狂!

编辑,完整的问题描述

为了充分说明,这是一个子程序,我将其作为脚本的一部分调用,读取包含无关数据的文本文件.这个文本文件看起来像这样,(引用的含糊不清是有意的)

 '<irrelevant text I want to ignore until seeing pattern 'NumberOfVariables?'>
    ...
    ...
    NumberOfVariables?
    NUMBEROFVARIABLES
    'for the end user, I need to be able to pull information from each of these fields assigned to a variable to create strings as headers as per a specific format
'note that variable and variable type 
    Variable#1 VARIABLETYPE Location? LOCATION UNITS DESCRIPTION 'text for each field is enclosed as follows '' (code formatting on site prevents me doing this)
    Variable#2 VARIABLETYPE Location? LOCATION UNITS DESCRIPTION
    ...
    Variable#NUMBEROFVARIABLES
    ' from here there is a column of data that is assigned to each variable such that
    Variable#1Element1         Variable#2Element1       'etc until #NUMBEROFVARIABLES
    Variable#1Element2         Variable#2Element2 
    Variable#1Element3         Variable#2Element3 
    Variable#1FinalElement     Variable#2FinalElement 

主要目标是使用原始帖子中的脚本将这些字段放在多维矩阵中,然后我可以使用它来对付某些条件语句,以根据最终用户的要求获取标题字符串.

然后,我会找到一种方法让数据列与每个变量匹配,以便它可以自动化到Excel中.

更进一步的是某种带有下拉的MsgBox,可以选择要复制的变量,但是现在正在思考我的开发阶段的天空中的馅饼!

最佳答案 试试这个.我没有测试过:

Sub ReadLines()

    Const FilePath$= "c:\mytextfile.txt"

    Dim iFile%, c&, i&, j&, k&, Content$, Lines, Temp, Data

    c = 499
    Open FilePath For Input As #iFile
    Content = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
    Close #iFile

    Lines = Split(Content, vbCrLf)
    ReDim Data(0 To UBound(Lines), 0 To c)

    For i = 0 To UBound(Lines)
        If Len(Trim$(Lines(i))) Then
            Temp = Split(Lines(i), "'")
            If k < UBound(Temp) Then k = UBound(Temp)
            If k > c Then
                c = k * 2
                ReDim Preserve Data(0 To UBound(Lines), 0 To c)
            End If
            For j = 0 To UBound(Temp)
                Data(i, j) = Temp(j)
            Next
        End If
    Next
    ReDim Preserve Data(0 To UBound(Lines), 0 To k)

End Sub
点赞