所以我在Excels VBA中有一个多维数组。我正在excel中阅读一个包含多行分号的txt文件。读取每一行,并将其拆分为一个数组,然后将其添加到多维数组中。Codez:
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
中的每个数组。我通常使用forloop之类的-
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
但我得到了相同的下标超出范围的错误
未测试
它不是多维数组。它是Variants
的一维数组,每个变体都有一个任意长度的数组。我认为你的第二次尝试很接近,但应该是这样的:
For i = LBound(brCodes) To UBound(brCodes)
For j = LBound(brCodes(i)) To UBound(brCodes(i))
MsgBox ("")
Next
Next