For example, in the following declaration
| Declaration with dimensions | Declaration without dimensions |
|---|---|
Dim MyArray(2)
| Dim MyArray() |
| The array "MyArray" has been declared with dimensions, so ReDim cannot be used later to resize it. | The array has been declared without dimensions (i.e. nothing in parentheses) thus ReDim can be used to resize the array. |
The Preserve keyword is used to keep the data in an array intact. The syntax is as follows:
ReDim MyArray(3)
ReDim Preserve MyArray(3)
For arrays of multiple dimensions, you can only ReDim the latest dimension after the array has been redimensioned once, and your initial declaration must match the number of dimensions you want to use.
This works:
Dim MyArray(,)
ReDim MyArray(1, 3)
ReDim MyArray(1, 4)
This does not work:
Dim MyArray()
ReDim MyArray(1, 3)
This does not work:
Dim MyArray(,)
ReDim MyArray(1, 3)
ReDim MyArray(2, 3)