Excel VBA-为变量赋值的下标超出范围



使用此数组时,我在尝试打印时会得到超出范围的下标。我看到LBound为1,而For循环中的nTemp在第一次迭代中为1。有什么指导意见吗?

copyArr = .Range(.Cells(1, moveRng.Column), .Cells(rowsUsed, moveRng.Column)).Value
Debug.Print vbTab & "copyArr range is " & LBound(copyArr) & " : " & UBound(copyArr)
For nTemp = LBound(copyArr) To UBound(copyArr)
Debug.Print vbTab & "copyArr(" & nTemp & ") = " & copyArr(nTemp)
Next nTemp

以下是发生错误之前的调试输出示例。

Found header 'Device Type' for moveRng at column # 6
Found header 'IP Address' for insertRng at column # 3
copyArr range is 1 : 3

以下是声明的变量

Dim searchRng As Range, moveRng As Range, insertRng As Range, targetRng As Range, copyArr As Variant
Dim sTemp As String, deleteAddr As String
Dim nTemp As Long, copyOffset As Long, insertOffset As Long, deleteOffset As Long, rowsUsed As Long

如有任何指导,我们将不胜感激。

@BigBen应该为此获得荣誉。1d与2d阵列的简单错误。

Debug.Print vbTab & "copyArr(" & nTemp & ") = " & copyArr(nTemp, 1)

这解决了问题。

最新更新