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

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard. This opens the window where you write and manage your VBA macros.
  2. 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.
  3. 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
  4. Format the Date: If you want the date to appear in a specific format (e.g., "DD-MM-YYYY"), use the Format function. Replace the code in your module with:
    Sub InsertFormattedDate()
        Range("A1").Value = Format(Date, "dd-mmm-yyyy")
    End Sub
  5. Create a Date Calculation: To find a date in the future or past (e.g., 7 days from now), use the DateAdd function:
    Sub CalculateFutureDate()
        Dim futureDate As Date
        futureDate = DateAdd("d", 7, Date)
        Range("A2").Value = futureDate
    End Sub
  6. 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 Now when you only need the date. Now includes 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 the DateSerial(year, month, day) function to ensure accuracy.
  • Ignoring Cell Formatting: Expecting the VBA Format function to change the cell's permanent format. The Format function often converts the date into a string. To keep it as a date value but change how it looks, use Range("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. Use ActiveCell to 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 Now and 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 DateDiff function 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:

  1. 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.
  2. 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.
  3. 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.
  4. Verify Regional Settings: If dates are swapping months and days, check your Windows Region settings. Use the DateSerial function 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.