To get the current day in Excel VBA, use the Date function for the current system date or the Now function if you need both the date and the specific time. For example, writing Range("A1").Value = Date will instantly place today's date into cell A1. This allows you to automate timestamps, create dynamic reports, and manage date-based calculations without manual entry.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- The Developer tab enabled in the Excel Ribbon (if not visible, right-click any tab, select Customize the Ribbon, and check the Developer box).
- Basic access to the Visual Basic for Applications (VBA) editor.
Step-by-Step
- Open the VBA Editor: Launch your Excel workbook. Press
Alt + F11on your keyboard. This keyboard shortcut opens the Visual Basic Editor window, which is where all your macros and scripts are written. - Insert a New Module: In the Project Explorer pane on the left, right-click on your workbook name (e.g., VBAProject (Book1)). Navigate to
Insert > Module. This creates a blank workspace where you can write your custom functions and subroutines. - Create a Subroutine: Type
Sub GetCurrentDay()at the top of the module. This defines the start of your macro. Give it a descriptive name so you know exactly what the code does when you look at it later. - Declare a Variable: While not strictly required for simple tasks, it is best practice to declare a variable to hold the date. Type
Dim todayDate As Date. This tells Excel to reserve memory specifically for a date-formatted piece of information. - Assign the Current Date: Use the
Datefunction to capture the system date. TypetodayDate = Date. TheDatefunction pulls the current date from your computer's system clock. - Output the Date to a Cell: To make the date visible in your spreadsheet, assign the variable to a specific cell. Type
Range("A1").Value = todayDate. You can change "A1" to any cell or named range in your workbook. - Close the Subroutine: Type
End Subto signal the end of the code block. - Run the Macro: You can run the code by clicking the green "Run" arrow in the toolbar or by returning to Excel and pressing
Alt + F8, selecting "GetCurrentDay," and clicking Run.
Common Mistakes
One of the most frequent errors is confusing the Date function with the Now function. Date returns only the date (e.g., 2023-10-27), whereas Now returns the date and the current time (e.g., 2023-10-27 14:30:05). If you use Now but only want the date, your cell may display a time stamp that disrupts your formatting or sorting. Always choose the function that matches your specific data requirement.
Another common mistake is neglecting the cell format. VBA may send the date to the cell correctly, but if the cell is formatted as "Text" or a generic "Number," the date might appear as a five-digit serial number (like 45226). This is because Excel stores dates as numbers counting from January 1, 1900. To fix this, you must ensure the cell format is set to "Date" via the Home tab in the Excel Ribbon.
Users also frequently forget to save their workbooks as .xlsm (Excel Macro-Enabled Workbook) files. If you save a file containing VBA code as a standard .xlsx file, Excel will strip all the code from the workbook upon saving, and your hard work will be permanently lost. Always verify the file extension before closing the application.
Tips
To make your date output more professional, use the Format function. Instead of simply assigning the date, you can control exactly how it looks. For example, Range("A1").Value = Format(Date, "dddd, mmmm dd, yyyy") will output the date as "Friday, October 27, 2023". This is incredibly useful for creating automated headers or email subject lines within your macros.
If you are working with large datasets, avoid putting the Date function inside a loop that runs thousands of times. Calling the system clock repeatedly can slightly slow down execution. Instead, assign the current date to a variable once at the beginning of your script, and then reference that variable throughout the rest of the code.
For those needing to calculate dates relative to today, VBA makes it easy with the DateAdd function. To find the date for tomorrow, use DateAdd("d", 1, Date). To find the date from exactly one month ago, use DateAdd("m", -1, Date). This prevents you from having to manually add or subtract integers from the date variable, which can sometimes lead to errors during leap years or month-end transitions.
Troubleshooting
If your code is not running, first check if macros are enabled. Excel often disables macros by default for security reasons. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure that "Disable all macros with notification" is selected. This will prompt you to "Enable Content" via a yellow bar at the top of the screen when you open the file.
If you receive a "Type Mismatch" error, it is likely because you are trying to assign a date to a variable declared as a String or an Integer. Ensure your variable is declared as Dim myVar As Date. If you are pulling a date from a cell to use in VBA, use CDate(Range("A1").Value) to explicitly convert the cell value into a date format that VBA recognizes.
If the date is appearing as a number (e.g., 45000), this is not a VBA error but a formatting issue. Select the cell in Excel, go to the Home tab, click the Number Format dropdown, and select "Short Date." If you want the VBA code to handle the formatting automatically, add the line Range("A1").NumberFormat = "mm/dd/yyyy" immediately after assigning the value.
Frequently Asked Questions
How do I get only the year from the current day?
You can use the Year() function. For example, myYear = Year(Date) will extract only the four-digit year from today's date.
Can I get the current day of the week?
Yes, use the WeekdayName() function combined with Weekday(). Example: dayName = WeekdayName(Weekday(Date)) will return "Monday", "Tuesday", etc.
What is the difference between Date and Now?
Date returns the current system date without a time component. Now returns the current system date and the current system time.
How do I put the current date in a message box?
Use the MsgBox function: MsgBox "Today's date is " & Date. This will pop up a small window displaying the current date to the user.
How do I find the last day of the current month?
The easiest way is to find the first day of the next month and subtract one day. Use DateSerial(Year(Date), Month(Date) + 1, 0) to get the last day of the current month.
Does VBA Date work across different time zones?
The Date and Now functions pull from the local system clock of the computer running the file. If the file is shared across different time zones, each user will see their own local date.