I have an excel with multiple worksheets. Each worksheet contains either a graph or a table. I wrote a code that loops through the worksheets, detects whether it is a table or not (by checking cells A8 and A9 on the worksheet) and if so, apply an autofilter on that table. However, if I execute this code, the code works fine for the first worksheet, but returns an error message on the next run through the loop. I'm missing something, but cannot figure out what. Any help is appreciated.
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
With sheet
If Range("A8") = "Flag" And IsEmpty(Range("A9").Value) = False Then
Dim LastCol As Integer
LastColumn = Cells(8, Columns.Count).End(xlToLeft).Column
.Range("A8", Cells(8, LastColumn)).AutoFilter
End If
End With
Next sheet
End Sub