What I am trying to do is filter 2nd column(B) for every row for every sheet and delete if the condition matches; ">11/12/2019" in case of the following code.
The code would run into 1004 on the first sheet where the condition was matched.
Option Explicit
Sub TooTiredToNameThis()
Dim MySheet As Worksheet, MyRange As Range
Dim LastRow As Long, LastCol As Long
Dim ActSheet As Integer
Application.DisplayAlerts = False
For ActSheet = 1 To Sheets.Count
Set MySheet = ThisWorkbook.Worksheets(ActSheet)
With MySheet
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
'LastCol = .Range("B" & .Columns.Count).End(xlToLeft).Column
Set MyRange = .Range(.Cells(1, 2), .Cells(LastRow, 2)) '2 was LastCol
End With
With MyRange
.AutoFilter Field:=1, Criteria1:=">12/11/2019"
.SpecialCells(xlCellTypeVisible).Resize(.Rows.Count).Rows.Delete
End With
With MySheet
.AutoFilterMode = False
If .FilterMode = True Then
.ShowAllData
End If
End With
Next
Application.DisplayAlerts = True
End Sub
Sample Data:
Blah |
Date |
AnotherBlah |
BlahAgain |
Woof |
WoofAgain |
WoofWoof |
LastOne |
Blah1 |
10/12/2019 |
AnotherBlah1 |
BlahAgain1 |
Woof1 |
WoofAgain1 |
WoofWoof1 |
LastOne1 |
Blah2 |
09/12/2019 |
AnotherBlah2 |
BlahAgain2 |
Woof2 |
WoofAgain2 |
WoofWoof2 |
LastOne2 |
Blah3 |
13/12/2019 |
AnotherBlah3 |
BlahAgain3 |
Woof3 |
WoofAgain3 |
WoofWoof3 |
LastOne3 |
Can you please help me spot what went wrong and why?
Bookmarks