<< Click to Display Table of Contents >> ReDim (statement) |
ReDim [Preserve] VarName(Subscripts) [As Type] [, ...]
Use the ReDim statement to set or change the length of a dynamic array.
The array contents will be erased at this point, unless you prepend Preserve to the variable name and change only the length of the last dimension.
VarName is the name of the array variable.
Subscripts defines the number and size of the dimensions (see the section Arrays).
Type is the data type (see the section Data types).
Dynamic arrays
To create a dynamic array, it must first be declared with the statements Global or Dim, but with empty parentheses instead of the usual specification of the number and size of the dimensions.
Example: Dim A()
The number and size of the dimensions can be later specified in the first call of the ReDim statement.
Example: ReDim A(42)
In further invocations of the ReDim statement, the size of the dimensions can be changed at will. The number of the dimensions and the type of the array however cannot be changed after the initial setting.
Note: When executing the ReDim statement, the existing content of the array is deleted.
If you use the keyword Preserve together with this statement, you can only change the last dimension. If an array has, for example, two dimensions, only the second dimension can be enlarged or shrunk. But the advantage is that: the existing content of the array is preserved.
Example:
Dim B()
ReDim B(10)
.
.
ReDim Preserve B(20)
See also: Dim, Option Base, Static, section Arrays