Description
Used at the procedure level to declare dynamic-array variables and allocate or reallocate storage space.
Syntax
ReDim [Preserve] varname(subscripts) [As type][,varname(subscripts) [As type]] . . .
The ReDim statement syntax has these parts:
Part |
Description |
Preserve |
Preserves the data in an existing array when you change the size of the last dimension. |
varname |
Name of the variable; follows standard variable naming conventions. |
subscripts |
Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax: |
[lower To] upper [,[lower To] upper] . . . | |
type |
Data type of the variable; may be Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined. For a Variant containing an array, type describes the type of each element of the array, but does not change the Variant to some other type. |
Remarks
The ReDim statement is usually used to size or resize a dynamic array that has already been formally declared using a Private, Public or Dim statement with empty parentheses (without dimension subscripts).
You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array. However, you can't declare an array of one data type and later use ReDim to change the array to another data type, unless the array is contained in a Variant. If the array is contained in a Variant, the type of the elements can be changed using an As type clause.
If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions you can only change the size of the last dimension and still preserve the contents of the array. The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.
ReDim X(10, 10, 10) . . . ReDim Preserve X(10, 10, 15)
Caution
If you make an array smaller than it was, data in the eliminated elements will be lost.
When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string, and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it was a separate variable. A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it does not refer to any particular instance of an object.
Note
In order to resize an array contained in a Variant, you must explicitly declare the Variant variable before attempting to resize its array.
See Also
Array Function, Dim Statement, Option Base Statement, Private Statement, Public Statement, Static Statement.
Example
This example uses the ReDim statement to declare dynamic-array variables and then allocate and reallocate storage space.
Dim MyArray() As Integer ' Declare dynamic array. ReDim MyArray(5) ' Allocate 5 elements. For I = 1 To 5 ' Loop 5 times. MyArray(I) = I ' Initialize array. Next I ' The next statement resizes the array and erases the elements. ReDim MyArray(10) ' Resize to 10 elements. For I = 1 To 10 ' Loop 10 times. MyArray(I) = I ' Initialize array. Next I ' The next statement resizes the array but does not erase elements. ReDim Preserve MyArray(15) ' Resize to 15 elements.