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
| Type | Date |
| A1 | 16-07-2022 |
| A2 | 17-07-2022 |
| A3 | 18-07-2022 |
| A4 | 19-07-2022 |
| A5 | 10-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
