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 Subtype: 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 & Gotchas

  • Microsoft’s documentation for ReDim states that if the array has been passed by reference to a procedure, you can’t redimension it within the procedure ...

Get VBScript in a Nutshell 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.