Redeclares variables and reallocate storage space.
ReDim [Preserve] varname[(subscripts)] [As type] [, varname[(subscripts)] [As type]] ... |
The ReDim statement syntax has these parts:
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. |
subscripts | Optional. Dimensions of an array variable; up
to 10 multiple dimensions may be declared. The subscripts argument uses
the following syntax: count1[, count2] . . . where count1, count2 are constants, indicating the upper limit of allowable indices for the defined array. The lower limit of allowable indices always equals 0. So, for a one-dimensional array the number of elements can be calculated as count1+1 . |
type | Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, or an object type. Use a separate As type clause for each variable you declare. |
The ReDim statement is used to declare or resize a dynamic array, which
was already described with the Dim statement. Also the ReDim statement
allows to completely redeclare a variable, declared earlier.
It possible to use the ReDim statement again to change the number of
elements and dimensions of the array.
The Preserve keyword can only be used with arrays. It's only possible
to modify the last dimension of the array, however it's not possible to change
the number of dimensions. For instance, if an array has only one dimension,
it's possible to change this dimension as it's the last and only in the array.
However, if an array has two or more dimensions, it's only possible to change
the value of the last dumension; the contents of the arrays will be preserved.
The following example demonstrates how to increase the value of the last dimension
of a dynamic array without destroying data it contains.
ReDim A(10, 10, 10)
. . .
ReDim Preserve A(10, 10, 15)
If the size of the array is decreased, the data in deleted elements will be
lost.
For other variables (not arrays) redeclaring means modyfing the type of the
variable, while the original value is preserved or rounded.
When redeclaring variables-objects not equal to Nothing, the object is
destroyed if it was created using the New statement, and the variable
was the last link to it. The variable is also reset to Nothing.
This example uses the ReDim statement to allocate and reallocate storage space for dynamic-array variables. It also shows how a variable can be redeclared to a new type.
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. 'The next statement declares variable A as Integer Dim A As Integer A = 10 'Initialize A Trace "A= " & A ' trace A ReDim A As Double 'Redeclare A as Double Trace "A= " & A ' trace A |
See Also |
Data Type Summary, Dim Statement, Set Statement, Static Statement, Const Statement |