I'm trying to make sure that a range has visible rows before I act on those visible rows (delete them) because if I try to act on a filtered range when there aren't any visible rows there, I'll get an error.
Dim lastrow As Integer
Dim ws as Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
ws.Range("$A$8").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
'DELETE PART CLASSES ROWS
ActiveSheet.Range("$O$7:$O$" & lastrow & "").AutoFilter Field:=1, Criteria1:= _
Array("CONS", "MISC", "PFG", "PRT", "TOTE", "="), _
Operator:=xlFilterValues
Range("$A$8").Select
Do
If ActiveCell.SpecialCells(xlCellTypeVisible) Then
ActiveSheet.Range("$O$8:$O$" & lastrow).SpecialCells(xlCellTypeVisible).EntireRow.DELETE
Exit Do
End If
While ActiveCell.Row < lastrow
Range("$O$8").AutoFilter Field:=1
Next
The code block was working fine until I added the Do-If-While nested loops. I now get the Compile error: Next without For
What am I doing wrong?
Thank you.