我在Excel的VBA中有一个多维数组。我正在使用Excel读取一个包含多个分号分隔行的txt文件。每一行都会被读取,然后被分割成一个数组,并添加到多维数组中。代码如下:
Dim filenum As Integer 'number of next "free file"
Dim splitCols As Variant
Dim counter As Long
Dim brCodes() As Variant
Dim textline As String
Dim lines As Variant
Dim numrows As Long
Dim numcols As Long
numcols = getNumColumns(ActiveSheet.Name)
numrows = getNumRows(ActiveSheet.Name)
counter = 0
filenum = FreeFile() 'find next free filenum
Open FileName For Input As #filenum 'open file for reading
'codes are put into an array
While Not EOF(filenum) 'process while file has more lines.
counter = counter + 1
Line Input #filenum, textline 'grab current line into textline
splitCols = Split(textline, ";")
ReDim Preserve brCodes(1 To counter)
brCodes(counter) = splitCols
Wend
Close #filenum 'close file\
现在我想循环遍历 brCodes
中的每个数组。我通常使用像这样的 for 循环 -
for i = lbound(brCodes,2) to ubound(brCodes,2)
'process stuff
next
但是brCodes中的数组长度不同。文本文件中的行具有不同数量的分号。它看起来像这样:
str1;str2;str3;str4;sdtr5
str1;str2;str3;str4;sdtr5;str6;str7
str1;str2;str3;str4
那么我需要添加一个中间步骤来将每个数组提取到临时变量中并进行处理吗?或者是否有一种方法可以在不先提取它的情况下获取特定“行”的ubound?
添加
我也尝试过:
For i = LBound(brCodes, 2) To UBound(brCodes, 2)
For j = LBound(brCodes(i)) To UBound(brCodes(i))
MsgBox ("")
Next
Next
但我收到了相同的下标越界错误。