Part | Description | |
Preserve | Optional. Keyword used to preserve the data in an existing array when you change the size of the last dimension. | |
varname | Required. Name of the variable; follows standard variable naming conventions. |
Part | Description | |
subscripts | Required. 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] . . . | ||
When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present. | ||
type | Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), 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 doesn't change the Variant to some other type. |
ReDim X(10, 10, 10)
. . .
ReDim Preserve X(10, 10, 15)
Similarly, when you use Preserve, you can change the size of the array only by changing the upper bound; changing the lower bound causes an error.
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 following statement resizes the array but does not erase elements.
Redim Preserve MyArray(15) ' Resize to 15 elements.