ReDim Statement

See AlsoVEZ4MS                 Example7VD00X7>Low

Used at the procedure level2W6W4TC to declare dynamic-arrayYPCGZO variables and allocate or reallocate storage space.

Syntax

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

Remarks

The ReDim statement has these parts:

Part                 Description

 

Preserve          Preserves the data in an existing array when you resize the last dimension.

varname           Name of a variable.

subscripts        Dimensions of an array.  You can declare multiple dimensions.  The syntax of subscripts is described below.

As type            Reserved word43US84 used to declare the data type3GYXY7 of a variable.  The type may be Integer, Long, Single, Double, Currency, String (for variable-length strings), String * length (for fixed-length strings), Variant8PHEAW3, a user-defined type, or an object type9DGMAH.  Use a separate As type clause for each variable being defined.

 

The argument subscripts has the following syntax:

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

The To reserved word provides a way to indicate both the lower and upper bounds of an array variable's subscripts.  The following statements are equivalent if there is no Option Base statement:

  ReDim A(8,3)

  ReDim A(0 To 8, 0 To 3)

  ReDim A(8, 0 To 3)

 

Array subscripts can be negative.  To can be used to specify any range of subscripts between -32,768 and 32,767, inclusive.  For example:

  ReDim A(-4 To 10)

  ReDim B(-99 To -5, -3 To 0)

 

If you use a subscript that is greater than the specified maximum or smaller than the specified minimum, an error occurs.

The ReDim statement is usually used to size or resize a dynamic array that has already been formally declared using a Global or Dim statement with empty parentheses (without dimension subscripts).  If you first declare a dynamic array using a Global or Dim statement and include no dimension subscripts, the maximum number of array dimensions you can later specify with ReDim is eight.  If your array requires more than eight dimensions, you can use the ReDim statement  within a procedure to initially declare a local dynamic-array variable.  Your array can then have as many as 60 dimensions.

You can use the ReDim statement repeatedly to change the number of elements in an array.  However, you can't use ReDim to change the number of dimensions in an array.  For example, if you declare an array consisting of two dimensions (for example, ReDim A(10,10)), you can't use ReDim later to change it to an array with three dimensions. (for example, ReDim A(12,12,12,) ).  Similarly, you can't declare an array of Integers and later use ReDim to change the array to another data type.

If you use the Preserve reserved word, you can resize only the last array dimension.  For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you cannot resize the first one. You can only change the size of the last dimension and still preserve the contents of the array.  The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.

  ReDim X(10, 10, 10)

  ...

  ReDim Preserve X(10, 10, 15)

 

When variables are initialized, numeric variables are initialized to 0, Variant variables to Empty1L2JEZ4.  Variable-length strings are initialized as zero-length strings, and fixed-length strings are filled with ANSI zeros (Chr(0)).  The fields of user-defined type variables are initialized as if they were separate variables.


See Also

Dim StatementLANDIM

Global StatementTDN897

Option Base StatementQH5EUA

Static StatementCC4JTI


ReDim Statement Example

The example uses the ReDim statement to dynamically resize an array while a procedure is running.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

  Dim TestArray() As Integer       ' Declare dynamic array.

  Dim I, Msg, Size                 ' Declare variables.

  Size = Int(100 * Rnd + 1)        ' Generate random number.

  ReDim TestArray(Size)            ' Make array have Size elements.

  For I = 1 To Size                ' Index for number of elements.

    TestArray(I) = Rnd             ' Put number in each element.

  Next I

  ReDim TestArray(Size * 10)       ' Make array 10 times larger.

  For I = 1 To Size * 10           ' Index for number of elements.

    TestArray(I) = Rnd             ' Put number in each element.

  Next I

  Msg = "This demo created a dynamic array of " & Size

  Msg = Msg & " elements and filled each with a random number.  "

  Msg = Msg & "It then resized the array to " & (Size * 10)

  Msg = Msg & " elements, again filling each with a random number.  "

  Msg = Msg & "Finally, it restored the array back to "

  Msg = Msg & Size & " elements."

  MsgBox Msg                       ' Display message.

End Sub