January 28, 2020

Filter Data for Current Month or Year

How to automate filtering of data for current month, current year, last month or last year using VBA.
Here’s the complete VBA code:
Option Explicit

Sub showCurrentMonthData()
Sheet1.Range(“A1:C15”).AutoFilter field:=3, Criteria1:=xlFilterThisMonth, Operator:=xlFilterDynamic
End Sub

Sub showAllData()
On Error Resume Next
End Sub

Sub showCurrentYearData()
Sheet1.Range(“A1:C15”).AutoFilter field:=3, Criteria1:=xlFilterThisYear, Operator:=xlFilterDynamic

End Sub

Sub showLastYearData()
Sheet1.Range(“A1:C15”).AutoFilter field:=3, Criteria1:=xlFilterLastYear, Operator:=xlFilterDynamic

End Sub

Sub showLastMonthData()
Sheet1.Range(“A1:C15”).AutoFilter field:=3, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic

End Sub