VBA Code to Get Today's Date: Simple Guide and Examples
In this article
To get today's date in VBA, use the Date function. For example, the line Range("A1").Value = Date will place the current system date into cell A1 of your active worksheet. This function retrieves the date from your computer's system clock and returns it as a Date data type, which Excel recognizes and formats automatically based on your regional settings.
What You'll Need
- A Windows PC with Microsoft Excel installed.
- The Developer tab enabled in the Excel Ribbon.
- Basic access to the Visual Basic Editor (VBE).
Step-by-Step
- Open the Developer Tab: Launch Microsoft Excel. If you do not see the Developer tab in the top menu, right-click any existing tab (like Home or Insert), select "Customize the Ribbon," and check the box next to "Developer" in the right-hand column. Click OK.
- Launch the Visual Basic Editor: Click the "Developer" tab and then click the "Visual Basic" button on the far left. Alternatively, you can press
Alt + F11on your keyboard to open the editor immediately. - Insert a New Module: In the Project Explorer window (usually on the left), right-click on your workbook name (e.g., VBAProject (Book1)). Hover over "Insert" and select "Module." This creates a blank white space where your code will live.
- Write the Macro: Type the following code into the module window. This example creates a simple subroutine that puts today's date in a specific cell:
Sub InsertTodayDate() Range("A1").Value = Date End Sub - Run the Code: You can run the code while still in the editor by clicking inside the subroutine and pressing
F5, or by clicking the green "Run" arrow in the toolbar. Alternatively, return to Excel, click "Macros" on the Developer tab, select "InsertTodayDate," and click "Run." - Verify the Result: Check cell A1 of your active sheet. It should now display today's date in your local format (e.g., MM/DD/YYYY or DD/MM/YYYY).
Common Mistakes
One of the most frequent errors is confusing Date with Now. While Date returns only the current date, Now returns both the date and the current time. If you use Now and your cell is formatted as a "Short Date," it will look correct, but the underlying value contains a time stamp. This can cause issues if you later try to compare two cells for equality; two cells might both show "2023-10-27," but if one was recorded at 10:00 AM and the other at 2:00 PM using Now, VBA will treat them as different values.
Another common mistake is forgetting to specify the worksheet. Using Range("A1") refers to the ActiveSheet. If the user happens to be on a different sheet when the macro runs, the date will be placed in the wrong location. To avoid this, always qualify your range, such as Worksheets("Sheet1").Range("A1").Value = Date.
Some users attempt to use DateValue("Today"). This is incorrect because DateValue is designed to convert a string representation of a date into a date object. It does not recognize the word "Today" as a command; it expects a specific string like "January 1, 2024." Using the standalone Date function is the only direct way to pull the system clock's current date.
Tips
If you need the date in a specific format (such as "YYYY-MM-DD") regardless of the user's computer settings, use the Format function. For example: Range("A1").Value = Format(Date, "yyyy-mm-dd"). Note that using Format converts the date into a string. If you intend to perform date calculations later, it is better to insert the date using Date and then change the cell's number format via the Excel UI or VBA NumberFormat property.
To create a dynamic filename using today's date, you must format the date first. Windows does not allow forward slashes (/) in filenames. You can use fileName = "Report_" & Format(Date, "yyyy-mm-dd") & ".xlsx" to create a safe, sortable filename that avoids system errors.
For those building logs, consider using a variable to hold the date at the start of the macro. If a macro runs for several hours across midnight, using Date multiple times might result in different dates being recorded for the same process. Assigning Dim currentDate As Date: currentDate = Date at the beginning ensures consistency throughout the execution.
Troubleshooting
If the code does not run, first check if your workbook is saved as an "Excel Macro-Enabled Workbook" (.xlsm). Standard .xlsx files cannot store VBA code; if you save as .xlsx and close the file, your code will be permanently deleted. Always use the .xlsm or .xlsb format.
If you receive a "Compile Error: Variable not defined," check if you have Option Explicit at the top of your module. This is a best practice that requires all variables to be declared. If you used a variable like myDate = Date without first writing Dim myDate As Date, the code will fail. Add the declaration or remove Option Explicit (though declaring variables is highly recommended for stability).
If the date appears as a number (e.g., 45210) instead of a date, this is not a code error but a formatting issue. Excel stores dates as sequential serial numbers. To fix this, right-click the cell in Excel, select "Format Cells," and choose "Date" from the category list. You can also do this in VBA by adding Range("A1").NumberFormat = "mm/dd/yyyy" after inserting the value.
If the date is incorrect (e.g., it shows yesterday's date), check your computer's system clock and time zone settings. VBA pulls directly from the OS. If the system clock is wrong, the VBA output will be wrong.
Frequently Asked Questions
How do I get only the year from today's date?
Use the Year() function. For example, myYear = Year(Date) will return the four-digit integer of the current year.
Can I get the date for tomorrow or yesterday?
Yes, because Excel treats dates as numbers, you can simply add or subtract. Use Date + 1 for tomorrow and Date - 1 for yesterday.
What is the difference between Date and Now?
Date returns only the current system date. Now returns the current system date and the current system time.
How do I put the date in a message box?
Use the MsgBox function: MsgBox "Today's date is " & Date. This will pop up a small window displaying the date to the user.
Does this work on Mac versions of Excel?
Yes, the Date function is a standard part of VBA and works across both Windows and macOS versions of Excel, though the VBE interface may look slightly different.
How do I get the current month?
Use the Month() function, such as myMonth = Month(Date), which returns the month as a number from 1 to 12.