VBA does not let me apply to the particular element in the dynamical array.
Dim a() As Variant
a = Range("A2:A11").Value
Range("B2:B11").Value = a 'Just to make sure that the list is not empty and is working correctly.
MsgBox a(1) 'OR a(7)=0 OR IF a(4)=0 then MsgBox "!"
I expect to use the first element of the array a, but get an error message at the fourth line, trying to execute MsgBox a(1) or any expression, which involves arr_name(num_index)). I tried the identical code with the static array, which works without any problems:
- Dim b(10) As Variant
- b(1) = 1234
- MsgBox b(1)
The debugger also says that "subscript is out of range". At the same time, The array is for sure not void since the 3rd line works correctly and the array from a is copied to the array of neighboring cells and displayed there just a moment before the macros stops and I'm getting the error message. What is wrong with my code?
a
is 2D - any array from a sheet is automatically so. So you needMsgBox a(1,1)
. Or you can transpose it to turn it into 1D. – SJR