Excel VBA: Compare Dates to Today (Step-by-Step)
In this article
You compare a date to today in Excel VBA by using the built-in Date function inside a condition such as If Range("A2").Value < Date Then. That one line is the whole trick: Date returns today's date as a true date serial, so comparisons work correctly as long as your cells actually hold dates rather than text. The steps below show how to build that check safely, handle blank cells and text, and avoid the time-of-day trap that catches most people.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer; on Mac, Excel > Preferences > Ribbon & Toolbar).
- A workbook with a column of dates. The steps assume dates in column A starting at A2, so adjust the range to match your sheet.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and running a macro.
- Dates stored as real dates, not text. If a cell is left-aligned by default, it is probably text and will not compare correctly.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook and choose Insert > Module. A blank module appears in the code window.
- Write the basic comparison. Paste this into the module:
Sub CompareToToday() Dim lastRow As Long, i As Long lastRow = Cells(Rows.Count, "A").End(xlUp).Row For i = 2 To lastRow If IsDate(Cells(i, "A").Value) Then If Cells(i, "A").Value < Date Then Cells(i, "B").Value = "Past" ElseIf Cells(i, "A").Value = Date Then Cells(i, "B").Value = "Today" Else Cells(i, "B").Value = "Future" End If Else Cells(i, "B").Value = "Not a date" End If Next i End SubThis loops down column A, checks each entry, and writes a label in column B. - Understand the
Datefunction.Datereturns only the calendar date, with the time portion set to midnight. That is whyRange("A2").Value = Dateis true for a cell containing today's date even if the underlying value has a time stamp of 14:30 — the comparison is against the date serial, not the clock. If you want to be explicit, wrap the cell value inInt()to strip any time:If Int(Cells(i, "A").Value) < Date Then. - Run the macro. Place the cursor inside the Sub and press F5, or use Run > Run Sub/UserForm. Switch back to the sheet to see the results in column B.
- Use it as a condition instead of a loop. If you only need a single check, skip the loop:
If Range("A2").Value < Date Then MsgBox "That date has passed." End IfThis is the pattern to reuse anywhere — in a button click, a Worksheet_Change event, or a validation routine. - Compare with a fixed offset if needed. To flag anything older than 30 days, use
If Range("A2").Value < Date - 30 Then. VBA treats dates as numbers, so arithmetic likeDate + 7gives next week's date. - Format the output. If you write a date back to a cell, set its number format so it displays correctly:
Cells(i, "C").NumberFormat = "yyyy-mm-dd". Otherwise Excel may show a serial number. - Save as a macro-enabled workbook. Use File > Save As and choose .xlsm, or the code will be discarded when you close the file.
Common Mistakes
- Comparing text to a date. A cell that looks like a date but is stored as text will never equal
Date. Test withIsDate()first, or convert withCDate()before comparing. - Using
Nowinstead ofDate.Nowincludes the current time, so a cell holding today's date at midnight will compare as less thanNow. UseDatefor date-only comparisons. - Forgetting the time component. If your cells contain date-and-time values,
= Datecan fail for today's entries after midnight. Wrap inInt(). - Hard-coding the range. A fixed
Range("A2:A10")breaks when rows are added. Find the last row dynamically as shown above. - Ignoring blank cells. An empty cell evaluates to zero, which is earlier than any real date, so blanks get flagged as "Past". Check for
Len(Cells(i, "A").Value) > 0before comparing. - Regional date formats. If your system uses dd/mm/yyyy, typing a literal like
#3/4/2025#in code is interpreted month-first in VBA regardless of your regional settings. UseDateSerial(2025, 4, 3)to be unambiguous.
Tips
- Use
DateDiff("d", Range("A2").Value, Date)to get the number of days between a date and today — handy for "days overdue" columns. - For conditional formatting driven by VBA, you can set a formula like
=A2<TODAY()directly in the rule instead of looping; it updates automatically. - Turn off screen updating while looping large ranges:
Application.ScreenUpdating = Falseat the start andTrueat the end. - Name your macros descriptively and assign them to a button (Developer > Insert > Button) so users can run them without opening the editor.
- Test on a copy of the workbook first; a loop that writes to the wrong column is easy to fix but annoying to undo.
Troubleshooting
If nothing happens when you run the macro, check that the code is in a standard module (not a sheet module) and that macros are enabled — Excel shows a yellow security bar if they are blocked. If every row shows "Not a date", your cells are text; select the column, use Data > Text to Columns and finish, or convert with CDate in code. If today's date is flagged as "Past", your cells contain a time component — apply Int(). If you get a "Type mismatch" error, a cell holds an error value such as #N/A; guard with IsError() before comparing. If results look shifted by one day, check your system clock and regional settings, since Date follows the computer's date.
Frequently Asked Questions
How do I compare a date to today in a single VBA line?
Use If Range("A2").Value < Date Then. Date is a built-in function returning today's date.
Why does my comparison treat today as yesterday?
The cell likely contains a time stamp. Wrap it in Int() to strip the time before comparing to Date.
Can I compare against a date in another cell?
Yes. Use If Range("A2").Value < Range("D1").Value Then, provided D1 holds a real date.
How do I highlight dates older than today?
Use conditional formatting with the formula =A2<TODAY() applied to your range; no VBA is required.
Does Date work on Mac Excel?
Yes, Date and date arithmetic behave the same in VBA on Windows and Mac.
How do I count how many dates are in the past?
Loop the range and increment a counter when Cells(i, "A").Value < Date is true, or use Application.WorksheetFunction.CountIf(Range("A2:A100"), "<" & Date).