VBA & Macro Problems

How to Filter Dates in the Last N Days Using Excel VBA

John WilsonJohn Wilson Nov 3, 2026 869 views

Question details

The user wants to create an Excel VBA macro to filter rows where dates in a specific column fall within the previous N days (such as 50, 60, 90, or 120 days) from the current date.

How to Filter Dates in the Previous 50, 60, 90, or 120 Days Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Writing an automated script to filter datasets dynamically based on a rolling date window relative to today.
Observed behavior
Using CurrentRegion or converting the date parameters to Long values within the macro causes the AutoFilter to return incorrect data or an empty result set.
Before you start

Verify that your target column contains valid Excel serial dates rather than text strings, and ensure your dataset has a distinct header row before running the macro.

Solution 1Recommended

Use EntireColumn with Direct Date Criteria

Applying the AutoFilter to the entire column and using dynamic date criteria avoids the data truncation pitfalls of CurrentRegion and the formatting errors of Long conversions.

Instead of converting date variables to Long values or relying on CurrentRegion (which might miscalculate range boundaries if there are blank rows), applying AutoFilter directly to the entire target column guarantees accurate date filtering. By using the 'Date' function in VBA, you can easily set a criteria window.

1
Open the VBA Editor

Press Alt + F11 in your Excel workbook to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Right-click on your workbook name in the Project Explorer, select 'Insert', and click 'Module'.

3
Create the Filtering Subroutine

Paste a subroutine that accepts an integer N. Target your specific column (e.g., Range("$F$1").EntireColumn) and apply the AutoFilter method. Set Criteria1 to ">=" & Date - N, Operator to xlAnd, and Criteria2 to "<=" & Date.

4
Run with Specific Days

Create a calling macro (such as Sub FilterLast50) that executes your main subroutine and passes the desired number of days (e.g., 50, 60, or 90) as the argument.

Use EntireColumn with Direct Date Criteria
Date Format Consistency: Using the standard VBA 'Date' function natively aligns with Excel's date filtering system, eliminating the need for complex data type conversions.

Filter Dates and Run VBA Macros Natively in WPS Office

WPS Office provides robust support for VBA macros and advanced filtering functions, allowing you to run your Excel VBA scripts seamlessly while managing large datasets with ease.

  1. 1. Install WPS Office: Download and install WPS Office, then open your macro-enabled workbook (.xlsm).
  2. 2. Access the Developer Tab: Navigate to the Developer tab on the ribbon to access the built-in VBA Editor.
  3. 3. Run Your Macro: Paste your date-filtering macro and execute it exactly as you would in Microsoft Excel.
  4. 4. View Filtered Results: Return to your spreadsheet to view the accurately filtered dates applied directly to your column.
Fully compatible with Microsoft Excel VBA syntax (.xlsm files)Lightweight application with ultra-fast loading timesAdvanced AutoFilter capabilities for complex date filteringFamiliar spreadsheet interface with zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA filter return an empty dataset when filtering dates?

This usually happens if the dates in your column are formatted as text instead of valid Excel serial dates, or if you converted the dates to Long values in VBA before applying the filter. Ensure your dates are valid and use standard VBA Date functions without type conversion.

Can I apply the AutoFilter starting from row 2 if I have headers?

Yes. When applying the .AutoFilter method to an EntireColumn (like column F), Excel generally identifies the first row as the header. If you encounter issues, you can specify the exact range starting from your header, for example: Worksheets("Sheet1").Range("F1:F1000").AutoFilter.

What happens if there are no dates in the past 50 days?

If no dates in your dataset match the criteria (falling between today and 50 days ago), the AutoFilter will hide all data rows, leaving only the header row visible on your worksheet. It will not cause a runtime error.