Redim Statement

Redimension an array at run-time.

Syntax
Redim [Preserve] arrayName (subscripts)

Parameters

Preserve
Optional. Specifies to preserve the previous contents of the array. If omitted, the array will be cleared.
arrayName
Name of the array variable; follows standard variable naming conventions. The array must have already been declared.
Maximum element number of the array variable
Specifies the new maximum element number of the array variable. You must supply the same number of dimensions as when the variable was declared. The subscripts syntax is as follows
(ubound1, [ubound2], [ubound3])
ubound1, ubound2, ubound3 each specify the maximum upper bound for the associated dimension. The elements in each dimension of an array are numbered from 0 and the available number of array elements is the upper bound value + 1.
When specifying the upper bound value, make sure the number of total elements is within the range shown below:

Others than String String
Local variable 2,000 200
Global Preserve variable 4,000 400
Global variable and module variable 100,000 10,000

Description
Use Redim to change an array’s dimensions at run time. Use Preserve to retain previous values.

The array variable declared by Byref cannot use Redim.

Frequent Redim will decrease the speed of program execution. Especially, we recommend using the minimum of Redim for the global preserve variables.

See Also
UBound

Redim Statement Example

Integer i, numParts, a(0)

Print "Enter number of parts "
Input numParts

Redim a(numParts)

For i=0 to UBound(a)
    a(i) = i
Next

' Redimension the array with 20 more elements
Redim Preserve a(numParts + 20)

' The first element values are retained
For i = 0 to UBound(a)
    Print a(i)
Next