I have some code that filters a large data set, then selects visible cells, and copy & pastes the range elsewhere.
Sub Filterstuff()
' Select & Filter data
Sheets("Main").Select
Lastrow = ActiveSheet.Range("A2").End(xlDown).Row
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.AutoFilter
' Filter for things
ActiveSheet.Range("A1:AU" & Lastrow).AutoFilter Field:=39, Criteria1:="words"
ActiveSheet.Range("A1:AU" & Lastrow).AutoFilter Field:=43, Criteria1:= _
"<>*wordswords*"
' Find the first unfiltered cell
Range("A1").Select
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
ActiveCell.Offset(1, 0).Select
Loop
' If there are no unfiltered cells, exit
If ActiveCell.Row = Lastrow + 1 Then
Exit Sub
' Else paste results normally
Else
Range(Selection, Selection.Offset(0, 47)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
' Paste to bottom
Sheets("PasteSheet").Select
countrows = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A" & countrows + 1).Select
ActiveSheet.Paste
End If
' Return to Main and unfilter
Sheets("Main").Select
Cells.Select
ActiveSheet.ShowAllData
Selection.AutoFilter
End Sub
My issue is located in the code block meant to exit the sub if everything gets filtered out and there are no resulting rows with data after filtering. The relevant code begins at the commented section "Find the first unfiltered cell".
This code finds the first unhidden row, and checks if it is after the last row of data in the data set. My issue is that it is exceedingly slow. My data set can be 100,000+ rows and looping through it using ActiveCell.Offset(1, 0).Select
takes forever.
How can I re-tool this code to exit the sub if everything gets filtered out?