我正在努力重新定义阵列。我简化了我的代码,见下文:
Sub Knop1_Klikken()
'all the cells are defined in an array
Dim col, i As Integer, defrow As Long
Dim notAct(1 To 20, 1 To 43) As Integer
For col = 2 To 4
For row = 1 To 20
notAct(row, kol) = 0 'setting 0 not required after Dim
Cells(row, kol).Value = notAct(row, kol)
Next row
Next col
'a loop with a if-statement to see if a specifica
For x = 1 To 100
If notAct(2, 2) = 0 Then
i = i + 1
If i = 10 Then
notAct(2, 2) = 1
End If
End If
Next x
End Sub
问题是我想要redim
数组。我试图在for循环next x
MsgBox notAct(2, 2) 'it returns a 0
ReDim notAct(2, 2)
MsgBox notAct(2, 2) 'it returns a 0
vba并不欣赏ReDim notAct(2,2)。有没有办法让redim工作在2d阵列中。我想在notAct(2,2)得到1时返回值。
答案 0 :(得分:1)
您需要先调整数组而不使用尺寸:
Dim notAct() as Integer
你可以重新定位它:
Redim notAct(1 to 20, 1 to 43) as Integer
请注意,对数组进行重新定标会导致丢失当前存储的所有数据。要保留数据,您需要使用Preserve关键字:
Redim Preserve notAct(1 to 20, 1 to 2) as Integer
请注意,使用Preserve关键字时,您只能更改最后一个维度的边界。