I'm on Excel 2010, on an admittedly very large sheet (400k rows X 20 columns).
My code aims to:
- load the entire sheet into an array
- examine every row for a certain criteria
- rows which qualify are copied to another array
- finally return the second array back to another sheet
- the second array will end up being roughly 90% of the original
I wrote a definition of two variable arrays as variants And tried to initialize them by copying the sheet's content twice.
first copy works, but by the second one I hit an error of "Out of memory".
Any ideas if there's a workaround? or is this just a limitation of VBA/ Excel.
Is there a way to not pre-define / initialize the destination array, and instead, let it "grow" with every successful qualification of the criteria? (On a scale of this magnitude).
Sub CopyPending()
Dim LastRow As Long
Dim LastCol As Integer
Dim AllRange() As Variant
Dim CopyRange() As Variant
Dim i As Long
Dim x As Long
Dim z As Long
LastCol = 21
LastRow = ActiveSheet.UsedRange.Rows.Count
AllRange = Range(Cells(2, 1), Cells(LastRow, LastCol)).Value
CopyRange = Range(Cells(2, 1), Cells(LastRow, LastCol)).Value ''' ERROR TRIGGER
i = 1
x = 1
z = 1
For i = LBound(AllRange) To UBound(AllRange) - 1
If AllRange(i, 7) = "TestCriteria" Then
For z = 1 To LastCol
CopyRange(x, z) = AllRange(i, z)
Next z
x = x + 1
End If
Next i
With Sheets(2)
.Range(.Cells(2, 1), .Cells(x, LastCol)).Value = CopyRange
End With
End Sub