Redim (statement)


Redim [Preserve] variablename (subscriptRange) [As type],...


Redimensions an array, specifying a new upper and lower bound for each dimension of the array.


The variablename parameter specifies the name of an existing array (previously declared using the Dim statement) or the name of a new array variable. If the array variable already exists, then it must previously have been declared with the Dim statement with no dimensions, as shown in the following example:

    Dim a$() 'Dynamic array of strings (no dimensions yet)

Dynamic arrays can be redimensioned any number of times.

The subscriptRange parameter specifies the new upper and lower bounds for each dimension of the array using the following syntax:

  [lower To] upper [,[lower To] upper]...

If lower is not specified, then 0 is used (or the value set using the Option Base statement). A runtime error is generated if lower is less than upper. Array dimensions must be within the following range:

  –32768 <= lower <= upper <= 32767


The type parameter can be used to specify the array element type. Arrays can be declared using any fundamental data type, user-defined data types, and objects.

Re-dimensioning an array erases all elements of that array unless the Preserve keyword is specified. When this keyword is specified, existing data in the array is preserved where possible. If the number of elements in an array dimension is increased, the new elements are initialized to 0 (or empty string). If the number of elements in an array dimension is decreased, then the extra elements will be deleted. If the Preserve keyword is specified, then the number of dimensions of the array being re-dimensioned must either be zero or the same as the new number of dimensions.


This example uses the FileList statement to re-dim an array and fill it with filename strings. A new array is then re-dimmed to hold the number of elements found by FileList, and the FileList array is copied into it and partially displayed.

Sub Main()
  Dim fl$()
  FileList fl$,"*.*"
  count = Ubound(fl$)
  Redim nl$(Lbound(fl$) To Ubound(fl$))
  For x = 1 to count
    nl$(x) = fl(x)
  Next x
  MsgBox "The last element of the new array is: " & nl$(count)
End Sub

See Also

Dim (statement); Public (statement); Private (statement); ArrayDims (function); LBound (function); UBound (function).

More information