I have data to filter based on the start date and end date.
The Start and End date have their own columns in, "Master Publisher Content."
Start date is in column G and End date is in column H.
I'm trying to filter and delete the rows of data based on a user-specified date range on the "Enter Info" tab, the user-specified start date is in cell C2 and the user-specified end date is in cell E2.
For example, if cell C2 is April 1st, 2019 and cell E2 is April 30th, 2019 I want to delete the rows of data that are not between these dates.
This code is just working with the end date. It says no cells were found.
Public Sub DeleteRowsWithAutofilterDates()
Dim wksData As Worksheet
Dim lngLastRow As Long
Dim rngData As Range
Dim StartDate As Range
Dim EndDate As Range
'Set references up-front
Set wksData = ThisWorkbook.Worksheets("Master Publisher Content")
Set StartDate = ThisWorkbook.Worksheets("Enter Info").Range("C2")
Set EndDate = ThisWorkbook.Worksheets("Enter Info").Range("E2")
'Identify the last row and use that info to set up the Range
With wksData
lngLastRow = .Range("G" & .Rows.Count).End(xlUp).Row
Set rngData = .Range("G2:G" & lngLastRow)
End With
'------------------------------------------------------------
Application.DisplayAlerts = False
With rngData
'Apply the Autofilter method to the first column of
.AutoFilter Field:=1, Criteria1:="<=EndDate"
'Delete the visible rows while keeping the header
.Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Rows.Delete
End With
'----------------------------------------------------------------
Application.DisplayAlerts = True
'Turn off the AutoFilter
With wksData
.AutoFilterMode = False
If .FilterMode = True Then
.ShowAllData
End If
End With
'Let the user know the rows have been removed
MsgBox "Damn son! Rows removed."
End Sub