I've got a simple problem:
- I've got a set of data, which I'm sifting through and adding into an array upon criteria match
- Issue is, I don't know how many matches there might be, so I need the array to be of unspecified size.
- The second index of the array is static.
In an (pseudo-language) example:
if <matched criteria> = True {
i = i + 1
array( i, 1 ) => "John Doe" ' name
array( i, 2 ) => "New York" ' location
array( i, 3 ) => "02. 08. 1992" ' birthdate
}
Issue is, in vba you have to kind of pre-declare the arrays (especially with Option Explicit
enabled). My thought process was to declare an array, that would start with first index at 0
and I would gradually ReDim
it upon need.
Here is an simplified example of my code:
Dim cell as Range
Dim arr(0, 1 to 3) as String
Dim i As Integer: i = 0
For each cell in Range("A1:A100")
If criteria_match(cell) = True Then
arr(i, 1) = Cells(cell.row, 4)
arr(i, 2) = Cells(cell.row, 5)
arr(i, 3) = Year(Cells(cell.row, 6))
i = i + 1
ReDim Preserve arr(i, 1 to 3)
End If
Next cell
Issue is, this throws an exception: