To get the current date in Excel VBA, use the Date function for the system date or the Now function if you also need the current time. For example, writing Range("A1").Value = Date will place today's date into cell A1. These functions pull directly from your computer's system clock, making them the most efficient way to timestamp entries, create dynamic reports, or calculate date differences within a macro.

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 familiarity with opening the Visual Basic Editor (VBE).

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard. This opens the VBE window where all your VBA code resides.
  2. Insert a New Module: In the top menu, go to Insert > Module. This creates a blank space where you can write your custom functions and macros, ensuring they are available to the entire workbook.
  3. Declare Your Subroutine: Type Sub GetCurrentDate() at the top of the module. This tells Excel that you are starting a new macro named "GetCurrentDate".
  4. Define a Variable: To store the date before putting it in a cell, declare a variable using the Date data type. Type Dim todayDate As Date. Using the specific Date type instead of a generic Variant is a best practice that improves memory efficiency and prevents type-mismatch errors.
  5. Assign the Date Value: Use the Date function to capture the system date. Type todayDate = Date. If you need both the date and the exact time, use todayDate = Now instead.
  6. Output the Date to a Cell: Specify where the date should go. For example, Range("A1").Value = todayDate. You can also use ActiveCell.Value = todayDate to place the date wherever your cursor is currently located.
  7. Format the Date (Optional): By default, VBA uses your system's regional settings. To force a specific format (like YYYY-MM-DD), use the Format function. Change your output line to: Range("A1").Value = Format(todayDate, "yyyy-mm-dd").
  8. Close and Run: Press Alt + Q to return to Excel. To run your code, press Alt + F8, select "GetCurrentDate", and click Run.

Common Mistakes

One of the most frequent errors is confusing the Date function with the DateValue function. The Date function returns the current system date, whereas DateValue is used to convert a string (like "January 1, 2024") into a formal date object. If you use DateValue without a string argument, your code will crash with an "Argument not optional" error.

Another common pitfall is the "Date Format Trap." VBA internally handles dates in a US-centric format (MM/DD/YYYY) regardless of your local settings. When you pass a date as a string to a cell or another function, Excel may misinterpret the day and month. To avoid this, always assign dates to cells as Date objects rather than strings, or use the Format function explicitly to remove ambiguity.

Users also frequently forget to declare their variables. While VBA allows "implicit declaration" (not using Dim), this often leads to typos that create new, empty variables instead of updating existing ones. This results in the code running without errors, but the date simply never appearing in the cell.

Tips

To make your date-getting macros more powerful, combine them with DateAdd. For example, if you need to get the date for exactly one month from today, use DateAdd("m", 1, Date). This is essential for creating automated invoice due dates or project deadlines.

If you are working with large datasets, avoid putting Range("A1").Value = Date inside a loop that runs thousands of times. Every time VBA interacts with a worksheet cell, it slows down. Instead, store the date in a variable once at the start of your macro and reference that variable inside the loop.

For those needing high precision, use the Timer function for elapsed time, but stick to Now for the calendar date and clock time. If you are sharing the workbook globally, consider using the ISO 8601 format (YYYY-MM-DD) via the Format function to ensure users in different countries don't confuse the day and month.

Troubleshooting

If your date appears as a five-digit number (e.g., 45285) instead of a readable date, this is not a VBA error. Excel stores dates as serial numbers (the number of days since January 1, 1900). To fix this, you can either change the cell format in the Excel UI to "Short Date" or add a line of VBA code to format the cell: Range("A1").NumberFormat = "m/d/yyyy".

If you receive a "Type Mismatch'" error, check the variable you are using to store the date. If you declared it as an Integer or String but tried to assign the Date function to it, VBA may struggle. Ensure your variable is declared as Dim myDate As Date.

If the date is incorrect (e.g., it shows yesterday's date), check your computer's system clock and time zone settings. VBA does not have its own internal clock; it is a mirror of the operating system's time. If the system clock is wrong, the VBA output will be wrong.

If the macro does not run at all, ensure your workbook is saved as an .xlsm (Excel Macro-Enabled Workbook) or .xlsb (Excel Binary Workbook). Standard .xlsx files cannot store VBA code and will strip your macros upon saving.

Frequently Asked Questions

How do I get only the year from the current date?

Use the Year() function. For example, myYear = Year(Date) will return the four-digit year as an integer.

Can I get the date from a specific cell instead of the system?

Yes. Instead of using the Date function, assign the cell value to a variable: myDate = Range("B2").Value. Ensure the cell actually contains a date to avoid errors.

What is the difference between Date and Now?

Date returns only the current date (e.g., 2025-12-05), while Now returns both the date and the current system time (e.g., 2025-12-05 14:30:05).

How do I get the last day of the current month?

You can use the DateSerial function. A common trick is to get the first day of the next month and subtract one day: DateSerial(Year(Date), Month(Date) + 1, 0).

How do I subtract two dates to find the number of days between them?

Simply subtract one date variable from another. For example, daysDiff = Date2 - Date1. The result is a number representing the total days.

Can I use VBA to get the date in a different time zone?

VBA does not have a built-in time zone converter. You must manually add or subtract time using DateAdd. For example, to add 5 hours for a different zone, use DateAdd("h", 5, Now).