User Guide > Scripting > Script Statements > ReDim Statement
Was this helpful?
ReDim Statement
 
Description
Resize an array variable
Syntax (one-dimensional)
ReDim [Preserve] ArrayName(UpperBound)
ReDim [Preserve] ArrayName(LowerBound To UpperBound)
Where ArrayName is the array variable name; UpperBound is the new UpperBound of the array; LowerBound is the index number of the first element; UpperBound is the index number of the last element.
Preserve is an optional keyword used to preserve the data in an existing array when you change the size of the last dimension.
Syntax (multi-dimensional)
ReDim [Preserve] ArrayName(Dimension1[,Dimension2][,Dimension3])
ReDim [Preserve] ArrayName(LowerBound1 To UpperBound1 [,LowerBound2 To UpperBound2][, LowerBound3 To UpperBound3])
Where ArrayName is the array variable name; Dimension1 is the size, in elements, of the array's first dimension; Dimension2 is the size, in elements, of the array's second dimension (optional); Dimension3 is the size, in elements, of the array's third dimension (optional). LowerBound and UpperBound are the upper and lower bounds of the respective array dimensions.
Preserve is an optional keyword used to preserve the data in an existing array when you change the size of the last dimension.
Remarks
The ReDim statement is used to resize an array that has already been explicitly declared using the Dim, Private, or Public statement. You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array.
If you use the Preserve keyword, you can only resize the last array dimension and you can't change the number of dimensions at all. If your array has two dimensions, you can only change the number of elements in the second (last) dimension and still preserve the contents of the array. If you make an array smaller than it was, data in the eliminated elements are lost.
When you use Preserve, you can change the size of the array only by changing the upper bound; attempting to change the lower bound results in an error. Element values are lost when ReDim is used without Preserve.
The ReDim statement destroys the existing array and creates another array from scratch. Therefore, if the Preserve keyword is not used, all values in the array are lost.
Limitations
Variables may not be used as size placeholders when initially declaring an array. For example, Dim MyArray(VariableName) is illegal.
Arrays are limited to three dimensions. For more information on arrays, see Array Variables.
Example
' Creates a 1-dimension array with 5 elements
Dim MyArray(1 To 4)
' Resizes the array by adding 5 more elements
ReDim MyArray(1 To 9)
' Illegal, results in an error
ReDim MyArray(0 To 9)
Last modified date: 12/03/2024