BantamPCI, here is something that I use. I gathered some code from various places plus figured some out on my own. There are three proc's here. One for turning the Autofilter on and off, one for sorting by date and the other for deleting selected records. Mine uses a date range entered by the user as opposed to being set to look back wards 6 years, but I believe that there is some good you might be able to gather from it. You'll need to make sheet name and probably some other changes. Hope it helps.
Bill
Option Explicit
'--------------------------------------
Sub MonthEntriesSheetAutoFilterOff()
Sheets("MonthEntries").Select
Selection.AutoFilter
Range("A1").Select
End Sub
'--------------------------------------------------
Sub SortByDateForFiltering()
Application.StatusBar = "Sorting By Work Date "
Application.ScreenUpdating = False
Sheets("MonthEntries").Select
MonthEntriesSheetAutoFilterOff
Range("A:F").Sort _
Key1:=Range("E1"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom
End Sub
'--------------------------------------
Sub DateFilterToDeleteOldTimeSheetRecords()
' Select them by sorting and then filtering by date, _
then run code to delete visible filtered old records.
Dim StartDate As String
Dim EndDate As String
Dim MonthEntries As Worksheet 'has all entries for numerous months
Dim LastRow As Long
Dim DatesSelected As Range
MsgBox "IMPORTANT: This procedure will delete time sheet entry " _
& "records. Verify your date range when entering. " _
& Chr(13) & Chr(13) _
& "If you want to EXIT this procedure, select OK " _
& "to clear this message, then select OK or CANCEL from " _
& "either the Beginning Date or Ending Date input box."
'-----------------------------------------
'sort by date
Sheets("MonthEntries").Select 'sheet with all records no matter month
SortByDateForFiltering 'proc above. Sorts on col E.
'-----Now get date ranges----------------------------
StartDate = Application.InputBox("Enter the Start Date in MM-DD-YYYY format." _
& " To exit, select OK or CANCEL without entering anything.", _
"Beginning Date", Type:=3)
If StartDate = CStr(False) Then 'CANCEL selected
MsgBox "You have chosen to close this procedure."
Exit Sub
End If
If StartDate = "" Or StartDate = " " Then 'OK selected
MsgBox "You have chosen to close this procedure."
Exit Sub
End If
'----------------------------
EndDate = Application.InputBox("Enter the Ending Date in MM-DD-YYYY format." _
& " To exit, select OK or CANCEL without entering anything.", _
"Ending Date", Type:=3)
If EndDate = CStr(False) Then 'CANCEL selected
MsgBox "You have chosen to close this procedure."
Exit Sub
End If
If EndDate = "" Or EndDate = " " Then 'OK selected
MsgBox "You have chosen to close this procedure."
Exit Sub
End If
'still need to add code to make sure the date is in DATE format
'----------------------------------------------
''*********using test sheet named MonthEntries
'now filter
Sheets("MonthEntries").Activate
ActiveSheet.Range("A1").End(xlDown).AutoFilter Field:=5, _
Criteria1:=">=" & StartDate, _
Criteria2:="<=" & EndDate
'is filtering correctly,
'-------------------------------------------------------
'do not want to delete headers from row 1.
'NOTE: making rows to be deleted visible, then deleting them inside With stmnt
Set DatesSelected = ActiveSheet.UsedRange.Offset(1, 0) _
.SpecialCells(xlCellTypeVisible)
With DatesSelected 'OK to use range variable name here
.EntireRow.Delete
End With
'----------------------------------------------
Columns("A:F").AutoFit
Range("A1").Select
'------------------------------------------------
'remove filtering
Sheets("MonthEntries").Select
Selection.AutoFilter
End Sub
Bookmarks