请附上我写的一个小测试程序的截图,以说明我遇到的困惑。 <> 中的文本是我输入的,以解释我遇到的错误。
创建 MsgBox 是为了使程序处于中断模式,以便我可以看到值是什么。
这是代码。 (我已经丢弃了早期版本,但值应该仍然相同)
Sub test()
Dim Test1()
Dim Test2()
'values hardcoded in the sheet from where this macro is launched
'Cells(7,4) = 1
'Cells(7,5) = 2
'Cells(7,6) = 3
'Cells(7,7) = 4
'Cells(7,8) = 5
'Cells(7,9) = 6
'Cells(7,10) = 7
Set Rng1 = Range(Cells(7, 4), Cells(7, 10))
Test1 = Rng1.Value2
ReDim Test2(4)
Test2 = Rng1.Value2
MsgBox ("This was a test program")
End Sub
我的问题
Redim
语句强制它有 4 个值,为什么它会选择第五个值?这是否意味着如果可能遇到更多值并且 Excel 将始终执行必要的操作,我们不必担心重新调整数组的大小? 提前非常感谢。
最佳答案
如果你从一个范围中得到一个数组,它几乎总是一个二维数组。唯一的异常(exception)是作为单个单元格的区域。
编辑#1 :
要验证 Ubounds 和 Lbounds,请尝试运行 主要 :
Sub SizeAndShape(ary)
MsgBox LBound(ary, 1) & "-" & UBound(ary, 1) & vbCrLf & LBound(ary, 2) & "-" & UBound(ary, 2)
End Sub
Sub MAIN()
Set Rng1 = Range(Cells(7, 4), Cells(7, 10))
Test1 = Rng1.Value2
Call SizeAndShape(Test1)
End Sub
关于arrays - VBA 中的数组下标 - 谁能解释一下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22689600/