Name

ReDim Statement

Syntax

ReDim [Preserve] varname(subscripts)_
                 [, varname(subscripts)] ...
Preserve

Use: Optional

Type: Keyword

Preserves the data within an array when changing its single or its last dimension.

varname

Use: Required

Data Type: Any

Name of the variable.

subscripts

Use: Required

Number of elements and dimensions of the array, using the following syntax:

                           upper [, upper] . . .

where upper is the upper bound of a particular array dimension.

Description

Used within a procedure to resize and reallocate storage space for a dynamic array.

Rules at a Glance

  • A dynamic array is created using a Private, Public, or Dim statement with empty parentheses. Only dynamic arrays created in this manner can be resized using the ReDim statement. There is no limit to the number of times you can redimension a dynamic array.

  • Use of the Preserve keyword allows you to retain the current values within the array, but it also places several limitations on how the Redim statement can be used:

    • Only the last dimension of an array can be resized.

    • The number of dimensions can’t be changed.

    • Only the upper bound of the array can be changed.

  • If you reduce either the number of elements of the array or the number of dimensions in the array, data in the removed elements is permanently lost, irrespective of the use of the Preserve keyword.

Programming Tips and Gotchas

  • You can pass an array by reference to a procedure, redimension it within the procedure, and return the modified array to the calling program. This is illustrated ...

Get VBScript in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.