I have a table with around 100k rows and 40 columns.
I need to copy some of the rows to another workbook based an array with strings that match column values.
cond_list = ["value1", "value2", "value3" ...]
This condition can match 5k rows or more.
I tried a simple solution to use AutoFilter and copy visible cells:
' Filter source data
src_wks.ListObjects("Table1").Range.AutoFilter _
Field:=src_wks.ListObjects("Table1").ListColumns("Column1").Index, _
Criteria1:=cond_list, Operator:=xlFilterValues
' Copy and paste
src_wks.UsedRange.SpecialCells(xlCellTypeVisible).Copy
dst_wks.Range("A1").PasteSpecial Paste:=xlPasteValues
Filtering takes a fraction of a second, but then execution of this line takes more than 10 minutes. I have to run this code like 20 times so it is unacceptable.
src_wks.UsedRange.SpecialCells(xlCellTypeVisible).Copy
I tried to modify the code following this comment: https://stackoverflow.com/a/22789329/7214068
I tried to copy whole data first and then remove hidden rows:
' Copy and Paste whole table
dst_wks.UsedRange.Offset(1, 0).Value = ""
addr = src_wks.UsedRange.Address
dst_wks.Range(addr).Value = src_wks.UsedRange.Value
' Filter data
dst_wks.ListObjects("Table1").Range.AutoFilter _
Field:=dst_wks.ListObjects("Table1").ListColumns("Column1").Index, _
Criteria1:=cond_list, Operator:=xlFilterValues
' Remove rest
Application.DisplayAlerts = False ' Suppress "delete row?" promt
Dim i, numRows As Long
numRows = dst_wks.UsedRange.Rows.Count
For i = numRows To 1 Step -1
If (dst_wks.Range("A" & i).EntireRow.Hidden = True) Then
dst_wks.Range("A" & i).Delete
End If
Next i
Application.DisplayAlerts = True
Copying whole data takes less than two seconds. But then it again hangs on for loop and takes more than 10 minutes.