Multi-demensional arrays supported in VBA (version 6.5) only recognize fixed dimensions meaning for example an array predetermined to be 3 x 3 or 8 x 10 in size. There exists a VBA command called ReDim that will dynamically alter the size of a declared array,
but it will only do so for the last outer dimension. Thus for an array of 8 (rows) x 10 (columns) in size, the ReDim statement can alter that array to a size of let's say 8 x 11 or 8 x 20 but it cannot alter the number of rows the array contains.
This makes working with dynamic arrays in VBA rather difficult.
I came across a very elegant solution to this VBA (version 6.5) limitation posted on
StackOverflow by a gentlemen named IIya Kurnosov
and his sample code, posted below, works quite nicely.
Hope it helps
Option Explicit
Public Sub TestMatrixResize()
Const MAX_D1 As Long = 2
Const MAX_D2 As Long = 3
Dim arr() As Variant
InitMatrix arr, MAX_D1, MAX_D2
PrintMatrix "Original array:", arr
ResizeMatrix arr, MAX_D1 + 1, MAX_D2 + 1
PrintMatrix "Resized array:", arr
End Sub
Private Sub InitMatrix(a() As Variant, n As Long, m As Long)
Dim i As Long, j As Long
Dim StringArray() As String
ReDim a(n)
For i = 0 To n
ReDim StringArray(m)
For j = 0 To m
StringArray(j) = i * (m + 1) + j
Next j
a(i) = StringArray
Next i
End Sub
Private Sub PrintMatrix(heading As String, a() As Variant)
Dim i As Long, j As Long
Dim s As String
Debug.Print heading
For i = 0 To UBound(a)
s = ""
For j = 0 To UBound(a(i))
s = s & a(i)(j) & "; "
Next j
Debug.Print s
Next i
End Sub
Private Sub ResizeMatrix(a() As Variant, n As Long, m As Long)
Dim i As Long
Dim StringArray() As String
ReDim Preserve a(n)
For i = 0 To n - 1
StringArray = a(i)
ReDim Preserve StringArray(m)
a(i) = StringArray
Next i
ReDim StringArray(m)
a(n) = StringArray
End Sub