To insert the current date in Excel using VBA, use the Date function for the date only or Now for the date and time. For example, Range("A1").Value = Date will place today's date into cell A1. This allows you to automate timestamps, generate dynamic reports, and perform 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).
- A basic understanding of how to open the Visual Basic Editor (VBE).
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This opens the window where you write and manage your VBA macros. - Insert a New Module: In the top menu, go to Insert > Module. This creates a blank sheet where your code will reside, ensuring it is available to the entire workbook.
- Write the Date Insertion Code: To simply put today's date in a specific cell, type the following code:
Sub InsertTodayDate() Range("A1").Value = Date End Sub - Format the Date: If you want the date to appear in a specific format (e.g., "DD-MM-YYYY"), use the
Formatfunction. Replace the code in your module with:Sub InsertFormattedDate() Range("A1").Value = Format(Date, "dd-mmm-yyyy") End Sub - Create a Date Calculation: To find a date in the future or past (e.g., 7 days from now), use the
DateAddfunction:Sub CalculateFutureDate() Dim futureDate As Date futureDate = DateAdd("d", 7, Date) Range("A2").Value = futureDate End Sub - Run the Code: Close the VBE window. In Excel, press
Alt + F8, select the macro name (e.g.,InsertTodayDate), and click Run.
Common Mistakes
- Confusing Date with Now: Using
Nowwhen you only need the date.Nowincludes the current system time, which can cause issues if your cell is formatted only for dates or if you are comparing two dates for equality. - Incorrect Date Literals: Writing dates as strings like
"12/05/2026". Depending on the user's regional settings, VBA may confuse the day and month. Always use#Month/Day/Year#(e.g.,#December 5, 2026#) or theDateSerial(year, month, day)function to ensure accuracy. - Ignoring Cell Formatting: Expecting the VBA
Formatfunction to change the cell's permanent format. TheFormatfunction often converts the date into a string. To keep it as a date value but change how it looks, useRange("A1").NumberFormat = "mm/dd/yyyy". - Hard-coding Cell References: Using
Range("A1")for everything. If the user is on a different sheet or cell, the code will still target A1. UseActiveCellto apply the date to the currently selected cell.
Tips
- Use DateSerial for Stability: When creating a specific date,
DateSerial(2026, 12, 5)is the safest method because it avoids all regional date format conflicts. - Dynamic Timestamps: Use
Nowand set the cell's NumberFormat to"yyyy-mm-dd hh:mm:ss"to create a precise audit trail of when a macro was executed. - Calculating Differences: Use the
DateDifffunction to find the number of days, months, or years between two dates. For example:daysBetween = DateDiff("d", startDate, endDate). - Variable Declaration: Always declare your date variables using
Dim myDate As Date. This ensures VBA treats the data as a date object rather than a generic variant, which improves performance and reduces errors.
Troubleshooting
If the code does not work as expected, try these steps in order:
- Check Macro Settings: If the macro won't run, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure "Disable all macros with notification" is selected. You must "Enable Content" when the yellow bar appears at the top of the sheet.
- Verify File Extension: Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm). Standard .xlsx files cannot store VBA code and will delete your macros upon saving.
- Check for Type Mismatch: If you get a "Type Mismatch" error, ensure you aren't trying to perform date math on a cell that contains text. Use
IsDate(Range("A1").Value)to verify the cell contains a valid date before running calculations. - Verify Regional Settings: If dates are swapping months and days, check your Windows Region settings. Use the
DateSerialfunction to bypass regional interpretation entirely.
Frequently Asked Questions
How do I get only the year from a date in VBA?
Use the Year() function. For example, currentYear = Year(Date) will return the four-digit year of today's date.
How can I find the last day of the current month?
The most reliable way is to find the first day of the next month and subtract one day: lastDay = DateSerial(Year(Date), Month(Date) + 1, 0).
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 convert a string to a date in VBA?
Use the CDate() function. For example, myDate = CDate("2026-12-05") converts the text string into a formal Date variable that VBA can use for calculations.
How do I add months to a date?
Use the DateAdd function with the "m" interval: newDate = DateAdd("m", 3, Date) will return the date exactly three months from today.