我一直在使用2D阵列,出于某种原因,我无法理解为什么我的代码不适用于1D阵列。请参阅以下代码:
Sub doesntwork()
Dim i As Integer
Dim arr As Variant
arr = Range("A1:A3").Value
For i = LBound(arr, 1) To UBound(arr, 1)
Debug.Print arr(i)
Next
End Sub
Sub works()
Dim i As Integer, j As Integer
Dim arr As Variant
arr = Range("A1:A3").Value
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
Debug.Print arr(i, j)
Next j
Next i
End Sub
我得到一个"下标超出范围"顶部错误。它们似乎与我相当,为什么会失败?