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 Dim AnArray() As Integer 'AnArray is resized to 10 elements ReDim AnArray(10) 'AnArray is resized to 5 elements ReDim AnArray(5)
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.
ReDim Preserve matrix(1,2)
- Related topics
- Dim. For more information on using arrays, see Arrays in Functions and Array Indexing.