In case you have no idea how large an array needs to be,
then you may create dynamic arrays that does not have a preset number of elements.
They are declared with a blank set of parentheses.
For example,
Dim MyArray( ) As IntegerBefore you can use the array, you must use the ReDim (for re-dimension) statement to tell VBA how many elements the array has.
ReDim MyArray(NumElements)When you redimension an array by using ReDim, you wipe out all values currently stored in the array.
ReDim Preserve MyArray(NumElements)So suppose MyArray currently has a size of 10 and contains 10 values. After executing the preceding statement with NumElements greater than 10, the first 10 elements remain intact and the array has room for additional elements up to the number specified by NumElements.
Dim I, MyArray() As Integer ' Declare variable and array variable. ReDim MyArray(5) ' Allocate 6 elements. For I = 0 To UBound(MyArray) MyArray(I) = I ' Initialize array. Next IThe next statement resizes the array without saving the contents of the elements.
ReDim MyArray(10) ' Resize to 11 elements. For I = 0 To UBound(MyArray) MyArray(I) = I ' Initialize array. Next IThe following statement resizes the array but saves the contents of the elements.
ReDim Preserve MyArray(15) ' Resize to 16 elements.