Redimensioning an Array

The ReDim keyword will dynamically change the size of the specified array (see Arrays in Functions). To be able to resize an array it must be created as a dynamic vector, not a static array. This means that when the array is first declared, its number of elements must be left empty. Afterwards, the keyword ReDim can be used to resize it at anytime. If a number of elements is defined when the array is first declared, then it may not be resized. The following example demonstrates how to create and resize a dynamic array:

'AnArray is created without specifying a size
AnArray() As Integer
'AnArray is resized to 10 elements

'AnArray is resized to 5 elements


Arrays may be one, two, or multidimensional (up to 8 dimensions). ReDim can be used to change the number of elements within any array's dimensions, and it can also alter the number of dimensions, as the following example demonstrates:

Dim matrix() As Double
'Resized to be two-dimensional

ReDim matrix(1,2)

'Resized to be three-dimensional

ReDim matrix(1,2,3)

Array redefinitions using ReDim may occur at any point within the macro (SVB) program; all array declarations using Dim must occur at the beginning of the macro (SVB) program.

Note that when arrays are resized, the data that it currently is holding will be deleted. To retain an array's data when resizing it use the Preserve keyword as demonstrated here:

ReDim Preserve matrix(1,2)

Related topics. Dim. For more information on using arrays, see Arrays in Functions and Array Indexing.