简体   繁体   English

Excel VBA - 分配了范围值的变体变量的下标超出范围

[英]Excel VBA - Subscript out of range for variant variable assigned a range value

When working with this array I get subscript out of range when trying to print it out.使用此数组时,我在尝试打印时下标超出范围。 I see the LBound is 1, and nTemp in the For loop = 1 in the first iteration.我看到 LBound 是 1,并且在第一次迭代中 For 循环中的 nTemp = 1。 Any guidance on what is wrong here?关于这里出了什么问题的任何指导?

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

Here is a sample of the debug output prior to the error.以下是出现错误之前的调试输出示例。

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

Here are the variable declares这是变量声明

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

Any guidance would be appreciated.任何指导将不胜感激。

@BigBen should get credit for this. @BigBen 应该为此获得赞誉。 Simple error with 1d vs 2d arrays.一维数组与二维数组的简单错误。

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

That solved it.那解决了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM