我正在尝试重新编辑并保留我的阵列,但它不断出现一个错误,表示它超出范围我不知道为什么?
Dim FirstArray(0) As Variant
Dim SecondArray(0) As Variant
Dim ArrSize As Variant
row = 9
ArrSize = 1
Do While Not Cells(row, 3).Comment Is Nothing
FirstArray(ArrSize - 1) = Cells(row, 3).Value
ReDim Preserve FirstArray(ArrSize)
ArrSize = ArrSize + 1
row = row + 1
Loop
答案 0 :(得分:4)
应该是:
Dim FirstArray() as Variant
Dim SecondArray() As Variant
我最初犯了同样的错误
要声明变量数组,您不能给它一个初始大小。
另外,在为0索引赋值之前,请确保将其重新编号为1。
ReDim Preserve FirstArray(ArrSize)
FirstArray(ArrSize - 1) = Cells(row, 3).Value
希望这有帮助!
答案 1 :(得分:1)
你也可以使用一个ArrayList对象,它可以节省ReDim的功能。还添加了功能(see here for example)
Dim FirstArray As Object
Set FirstArray = CreateObject("System.Collections.ArrayList")
row = 9
Do While Not Cells(row, 3).Comment Is Nothing
FirstArray.Add Cells(row, 3).Value
row = row + 1
Loop