SoFunction
Updated on 2025-04-08

VBS Tutorial: VBScript Statement-ReDim Statement

ReDim statement

Declare dynamic array variables and allocate or reallocate storage space at the process level.

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

parameter

Preserve

Data is retained when changing the size of the last dimension of the existing array.

varname

Variable name, following the standard variable naming convention.

subscripts

The dimension of an array variable, up to 60-dimensional arrays can be declared.subscriptsThe parameter syntax format is as follows:

upper [,upper] . . .

The lower bound of the array is always zero.

illustrate

ReDimStatements are usually used to specify or modify the size of dynamic arrays that have been used with empty brackets.PrivatePublicorDimThe statement (no dimension subscript) has been officially declared. Can be reusedReDimStatement changes the array dimensions and number of elements.

If usedPreserveKeywords can only adjust the size of the last dimension of the array, and cannot change the dimension of the array. For example, if the array has only one dimension, the size of the array can be modified because the dimension is the last and only one dimension. However, if the array has two or more dimensions, you can only change the size of the last dimension and preserve the array contents.

This example shows how to increase the termination dimension of the dynamic array without erasing the data present in the array.

ReDim X(10, 10, 10). . .ReDim Preserve X(10, 10, 15)

carefulIf the array is reduced, data in the excluded elements will be lost.

When variable is initialized, the numeric variable is initialized to 0 and the string variable is initialized to a zero-length string (""). Before using variables that reference objects, you must useSetThe statement assigns an existing object to the variable. Before object assignment, declared object variables have specific values ​​Nothing.