The Filter feature in Excel helps us to filter data by following specific conditions. Often, we have to Filter the data in our Excel worksheet based on certain Dates. Besides the Filter feature, we can also make use of Excel VBA to filter data. In this article, we’ll show you the effective ways to apply VBA code to filter data by date in Excel.
To illustrate, we are going to use a sample dataset as an example. For instance, the below dataset represents the Order ID, Product, and Delivery Date.
VBA Code to Filter Data by Date in Excel: 4 Examples
1. VBA Code to Filter Data between Dates in Excel
Sometimes, we want to filter the data in our Excel worksheet based on a certain period of time. In the following dataset, we have a Start Date and an End Date. Here, we would like to apply the VBA code to filter for the data present in the period between these two dates only. Therefore, follow the steps below to perform the task.
- First, select the sheet and right-click on the mouse.
- Now, select View Code.
- As a result, the VBA window will pop out and a dialog box will appear.
- After that, copy the following code and paste it into the box.
Sub BetweenDates() Dim sDate As Long Dim EDate As Long sDate = Range("F5").Value EDate = Range("G5").Value Range("D4:D10").AutoFilter 1, ">=" & sDate, xlAnd, "<=" & EDate End Sub
- Subsequently, press the F5 key to run the code.
- Next, close the VBA window.
- Finally, you’ll get the data present in that mentioned period.
Read More: How to Filter Last 30 Days of Date in Excel
2. Filter Data by Exact Date with Excel VBA
However, we can also filter for data present on a specific date. In the below dataset, we have set an exact date in cell F5. So, follow the process to apply the VBA Code to filter data by Exact Date in Excel.
- In the beginning, select the View Code after right-clicking on the mouse.
- Consequently, the VBA window will appear and a dialog box will emerge.
- Now, copy the code and paste it there.
Sub FilterByExactDate() Dim ExactDate As Date Dim sDate As String Dim EDate As Long ExactDate = DateSerial(2022, 2, 5) EDate = ExactDate Range("D4:D10").AutoFilter Range("D4:D10").AutoFilter Field:=1, Criteria1:=">=" & EDate, _ Operator:=xlAnd, Criteria2:="<" & EDate + 1 End Sub
- Afterward, press the F5 key.
- Close the VBA window.
- Eventually, it’ll return the data prevailing on that specified date.
3. Apply VBA Code to Filter for Data before Specified One
Additionally, we may face some cases where we have to filter for data existing before a certain date. In this example, we have a specific date in cell F5. Hence, learn the following steps to carry out the operation.
- First of all, select the sheet.
- Then, right-click on the mouse and select View Code.
- Now the VBA window will emerge and a dialog box will appear.
- Next, copy the below code and paste it into the dialog box.
Sub BeforeDate() Dim sDate As Long sDate = Range("F5").Value Range("D4:D10").AutoFilter 1, "<" & sDate End Sub
- After that, press the F5 key and close the VBA window.
- In the end, it’ll return the data prevailing before that specified date.
Read More: How to Use Custom Date Filter in Excel
4. Filter for Data after Specified Date Using VBA Code
In our last example, we will show how to filter for data present after a specified date. Therefore, learn the process for applying the VBA code to filter data by Date in Excel.
- Firstly, go to Sheet ➤ View Code.
- Consequently, a dialog box will appear in the pop-up VBA window.
- Then, copy the below code and paste it there.
Sub AfterDate() Dim sDate As Long sDate = Range("F5").Value Range("D4:D10").AutoFilter 1, ">" & sDate End Sub
- Afterward, press the F5 key as well as close the window.
- At last, it’ll return the desired data like it’s shown in the following picture.
Download Practice Workbook
Download the following workbook to practice by yourself.
Henceforth, you will be able to apply VBA code to filter data by Date in Excel following the above-described examples. Keep using them and let us know if you have any more ways to do the task. Don’t forget to drop comments, suggestions, or queries if you have any in the comment section below.