I am trying to delete filtered rows from an Excel workbook with multiple worksheets. I used a dynamic range because each sheet doesn't have the same number of rows.
I have tried to use the If
statement to make the code loop through some specific sheets but it won't work. The code doesn't move to other sheets without ws.Activate
.
Sub DeletAnalyst2()
'Declare variables
Dim ws As Worksheet, startcell As Range, lastrow As Long, lastcol As Long
'Set Objects
Condition = Application.InputBox(prompt:="Please type the condition text:")
For Each ws In ThisWorkbook.Worksheets
ws.Activate
If (ws.Name <> "Presentation") And (ws.Name <> "Sheet6") And (ws.Name <> "sheet11") And (ws.Name <> "PrefTracks") And (ws.Name <> "AnalystNeeds") And (ws.Name <> "Post-Preference") And (ws.Name <> "Post Preference Grid") Then
Set startcell = Range("A1")
'Find last row and column of cells
lastrow = Cells(ws.Rows.Count, startcell.Column).End(xlUp).Row
lastcol = Cells(startcell.Row, ws.Columns.Count).End(xlToLeft).Column
'select dynamic range
Range(startcell, Cells(lastrow, lastcol)).Select
'AutoFilter technique
'ws.Range(startcell, ws.Cells(lastrow, lastcol))
Range(startcell, Cells(lastrow, lastcol)).AutoFilter Field:=1, Criteria1:=Condition
'deleting filtered
Selection.Offset(2).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
ActiveSheet.ShowAllData
End If
Next
End Sub
I expect code to loop through some sheets but not all sheets.