所以我在Excels VBA中有一个多维数组。我正在使用多个分号分隔的行读取excel中的txt文件。读取每一行,并拆分成一个数组,并添加到多D数组中。 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
中的每个数组。我通常使用像 -
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
所以我必须添加一个中间步骤来将每个数组拉入一个临时变量并像这样处理它吗?或者是否有人有办法让特定的" Row"没先把它拔出来?
我也尝试过:
For i = LBound(brCodes, 2) To UBound(brCodes, 2)
For j = LBound(brCodes(i)) To UBound(brCodes(i))
MsgBox ("")
Next
Next
但我得到了相同的下标超出范围错误
答案 0 :(得分:3)
<强> UNTESTED 强>
它不是一个多维数组。它是Variants
的一维数组,每个变体都包含任意长度的数组。我认为你的第二次尝试很接近,但它应该是:
For i = LBound(brCodes) To UBound(brCodes)
For j = LBound(brCodes(i)) To UBound(brCodes(i))
MsgBox ("")
Next
Next