Visual Basic for Applications Reference

ReDim Statement Example

This example uses the ReDim statement to allocate and reallocate storage space for dynamic-array variables. It assumes the Option Base is 1.

  Dim MyArray() As Integer   ' Declare dynamic array.
Redim MyArray(5)   ' Allocate 5 elements.
For I = 1 To 5   ' Loop 5 times.
   MyArray(I) = I   ' Initialize array.
Next I

The next statement resizes the array and erases the elements.

  
    
      Redim
     MyArray(10)   ' Resize to 10 elements.
For I = 1 To 10   ' Loop 10 times.
   MyArray(I) = I   ' Initialize array.
Next I

The following statement resizes the array but does not erase elements.

  
    
      Redim
     Preserve MyArray(15)   ' Resize to 15 elements.