Filter till end of the month VBA

Hello friends! Today we’ll be learning how to filter for end of the month in VBA. Easy to follow steps. I have attached Excel file to used in this blog post.

Data

TypeDate
A116-07-2022
A217-07-2022
A318-07-2022
A419-07-2022
A510-07-2022

Code

Sub Filter()
Dim Database As Worksheet
Dim StDate As Date
Dim EnDate As Date
'-----Define Sheet name
Set Database = Worksheets("Sheet1")
'------Start Date
StDate = Application.WorksheetFunction.EoMonth(Date, -1) + 1
'------End Date
EnDate = Application.WorksheetFunction.EoMonth(Date, 0)
'------Last Row
LastRow = Database.Cells(Rows.Count, 1).End(xlUp).Row
'MsgBox StDate
'-----Filtering for this month
With Database
.AutoFilterMode = False
With .Range("A1:B" & LastRow)
.AutoFilter Field:=2, Criteria1:=">=" & CLng(StDate), Operator:=xlAnd, Criteria2:="<=" & CLng(EnDate)
End With
End With
End Sub

Download Excel file here

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries

Leave a Reply

Your email address will not be published. Required fields are marked *