Good Morning All,
I am trying to use the below code to filter out a range of dates. I would like to have it so I can filter out the current year, last year then 3 years ago. Which is set up in tab "Control" But when I set these Value's and the Macro gets to the filter portion it gives me the error "Run-time error '1004': AutoFilter method of Range class failed.
Any help on this would be most appreciated.
Thank you.
Sub Macro1()
'
' Macro1 Macro
'
Dim endRow As Long
Dim rangeOne As Range, rangeTwo As Range
'
Sheets.Add.Name = "Control"
Sheets("CONTROL").Range("A1") = "CURRENT DATE:"
Sheets("CONTROL").Range("B1") = Date
Sheets("CONTROL").Range("B2").Value = "=DATE(YEAR(B1)-1,MONTH(B1),DAY(B1))"
Sheets("CONTROL").Range("B3").Value = "=DATE(YEAR(B1)-2,MONTH(B1),DAY(B1))"
Sheets("CONTROL").Range("B4").Value = "=DATE(YEAR(B1)-3,MONTH(B1),DAY(B1))"
Year0 = Sheets("CONTROL").Range("B1").Value
Year1 = Sheets("CONTROL").Range("B2").Value
Year2 = Sheets("CONTROL").Range("B3").Value
Year3 = Sheets("CONTROL").Range("B4").Value
Sheets("Report").Select
endRow = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("$A$1:$N$" & endRow).AutoFilter Field:=3, Criteria1:=">=" & Year0, Operator:=x1And, Criteria2:="<=" & Year1
End Sub
Bookmarks