Redim

From Wikipedia, the free encyclopedia

ReDim is a Visual Basic function used to resize arrays. By using this function you can add or remove space from an array.
This functionality can only be used on arrays that are declared without dimensions.

Can not use ReDim:
Dim MyArray(2)

Can use ReDim:
Dim MyArray()

Use the Preserve keyword to keep the data in your array intact.

The syntax is as follows:

ReDim MyArray(3)
ReDim Preserve MyArray(3)

For arrays of multiple dimensions, you can only ReDim the latest dimension after the array has been redimensioned once, and your initial declaration must match the number of dimensions you want to use.

This works:
Dim MyArray(,)
ReDim MyArray(1, 3)
ReDim MyArray(1, 4)

This does not work:
Dim MyArray()
ReDim MyArray(1, 3)

This does not work:
Dim MyArray(,)
ReDim MyArray(1, 3)
ReDim MyArray(2, 3)