Excel VBA: Set a Date Variable (Step-by-Step)
In this article
To set a date variable in Excel VBA, declare it with Dim myDate As Date and assign a value using myDate = DateSerial(2025, 3, 15) or myDate = Date for today. The Date data type holds date and time values from January 1, 100 to December 31, 9999, and VBA converts valid date strings or serial numbers automatically. This guide shows the reliable ways to assign dates, avoid locale mix-ups, and handle common errors.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- Access to the VBA editor (Alt+F11).
- Basic familiarity with declaring variables and running macros.
Step-by-Step
- Open the VBA editor. Press Alt+F11 in Excel. In the Project Explorer, right-click your workbook and choose Insert > Module to add a standard module.
- Declare a Date variable. At the top of the module, type
Dim myDate As Date. UseDiminside a procedure for local scope, orPublicat module level if other modules need it. - Assign a literal date with DateSerial. Write
myDate = DateSerial(2025, 12, 31). DateSerial takes year, month, day in that order and is immune to regional date-format settings, so it is the safest way to hard-code a date. - Assign today's date. Use
myDate = Datefor the current date (time is midnight) ormyDate = Nowfor date and time. Both read the system clock. - Assign a date from a cell. If cell A1 holds a real date, use
myDate = Range("A1").Value. VBA converts the cell value to a Date if it is a valid date; if the cell contains text, usemyDate = CDate(Range("A1").Value)to force conversion, but only when the text is in a format VBA recognises. - Assign a date from a string with DateValue.
myDate = DateValue("2025-03-15")parses a date string using the system's short date settings. For predictable results, stick to ISO format (yyyy-mm-dd) or use DateSerial instead. - Assign a time with TimeSerial or TimeValue.
myDate = TimeSerial(14, 30, 0)sets 2:30 PM. Combine date and time withmyDate = DateSerial(2025, 3, 15) + TimeSerial(14, 30, 0). - Write the value back or use it. For example,
Range("B1").Value = myDatewrites the date to a cell. Format the cell as a date in Excel, because VBA writes the underlying serial number. - Run and test. Press F5 with the cursor inside the procedure, or use Run > Run Sub. Use
Debug.Print myDateand check the Immediate window (Ctrl+G) to confirm the value.
Common Mistakes
- Using ambiguous date strings.
"03/04/2025"means March 4 in the US and April 3 in much of the world. VBA interprets it using the system locale, so the same code can produce different dates on different PCs. Use DateSerial or ISO strings. - Forgetting that Date includes no time.
Datereturns midnight. If you need the current moment, useNow. - Assigning text without conversion.
myDate = "next Friday"fails at runtime. Only strings that parse as dates are accepted, and CDate raises a type mismatch error for anything else. - Assuming a cell holds a date. A cell formatted as text may contain a date-looking string. Check with
IsDate(Range("A1").Value)before assigning. - Declaring as Variant by accident.
Dim myDatewithoutAs Datecreates a Variant. It works, but you lose type checking and date-specific behaviour. - Ignoring the 1900 vs 1904 date system. Workbooks can use different date systems, which shifts serial numbers by four years. When copying dates between workbooks, assign the Date value rather than the serial number.
Tips
- Use
Option Explicitat the top of the module to catch undeclared variables. - Prefer
DateSerialover string literals in all production code; it removes locale dependence entirely. - Use
Format(myDate, "yyyy-mm-dd")when you need a string for a file name, message box, or SQL query. - To compare dates, compare the Date values directly:
If myDate > Date Then .... Avoid comparing formatted strings. - Use
DateAddandDateDifffor arithmetic:DateAdd("d", 30, myDate)adds 30 days. - When reading dates from cells,
CDateis safe only ifIsDatereturns True; otherwise handle the error.
Troubleshooting
Run-time error 13: Type mismatch. The value you are assigning is not a valid date. Check the source with IsDate and convert or clean the input first.
The date appears as a number in the cell. VBA writes the serial number; the cell format controls display. Set Range("B1").NumberFormat = "yyyy-mm-dd" or format the cell manually.
Dates are off by four years. The workbook is using the 1904 date system. Check File > Options > Advanced > When calculating this workbook, and align the setting between source and destination workbooks.
DateValue rejects a string. The string does not match the system's expected date format. Use DateSerial with explicit year, month, and day components instead.
The macro works on one PC but not another. Locale differences are the usual cause. Replace all string-based date assignments with DateSerial or ISO-format parsing.
If that did not work, step through the code with F8 and use Debug.Print to inspect the variable's value and type (TypeName(myDate)) at each stage.
Frequently Asked Questions
How do I set a date variable to today's date?
Use myDate = Date. For the current date and time, use myDate = Now.
Can I assign a date directly like myDate = #3/15/2025#?
Yes, the hash marks denote a date literal, but the literal is still interpreted using the system locale, so it can be ambiguous. DateSerial is safer.
Why does my date variable show a time?
You likely assigned Now or a value that includes a time component. Use Int(myDate) to strip the time, or assign with Date instead.
How do I put a date variable into a cell?
Assign it: Range("A1").Value = myDate. Then set the cell's number format to a date format so it displays correctly.
What is the difference between Date and DateSerial?
Date returns the current system date. DateSerial builds a date from year, month, and day numbers you supply, independent of locale.
Can a Date variable hold a time only?
Yes. A Date variable stores a serial number where the integer part is the date and the fractional part is the time. Assigning TimeSerial(9, 0, 0) gives a time-only value with date part zero.