ReDim [Preserve] varname(size) [As type][= initexpr] [,varname(subscripts) [As type][= initexpr]] . . .
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. |
size | RequiredSize of each dimension: |
type | Optional. Data type of the variable; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, a user-defined type, or an object type. Use a separate As type clause for each variable being defined. For an Object containing an array, type describes the type of each element of the array, but doesn't change the Object to some other type. |
The ReDim statement is used to size or resize an array that has already been formally declared.
You can use the ReDim statement repeatedly to change the number of elements; however, you cannot change the number of dimensions in the 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 anObject.
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 change the size of only 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)
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.
If you make an array smaller than it was, data in the eliminated elements will be lost. If you pass an array to a procedure by reference, you can't redimension the array within the procedure.
Array Function | Date Type Summary | Dim Statement | Option Strict | Private Statement | Public Statement