I'm populating an array based on whether or not criteria across a worksheet are met. The thing is, I don't know how long the array is, as it could be larger than the dataset itself (will have duplicates of some entries). What I have is something along the lines of:
Sub ArrayTest
Dim MyArray as Variant
Dim i as Long
Dim index as Long
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).row
ReDim myarray(index)
For i = 1 to lastrow
If (something) then
index = index + 1
ReDim Preserve recordArrray(Index)
recordArrray(Index) = range("A" & i)
End If
Next
End Sub
However, once index
reaches lastrow
, I get a subscript out of range
error. I can avoid this by simply replacing the ReDim
line with ReDim myarray(1 to 100000000).
Obviously, this is not an ideal solution.
Can I create an array without having to define what the upper bound will be before it is created?