Excel VBA Now Function: Step-by-Step Guide
In this article
The Excel VBA Now function returns the current date and time as a Variant of subtype Date, and you use it by writing Now (or Now()) in a macro, then assigning the result to a cell, a variable, or a message box. It is the quickest way to stamp a workbook with the exact moment a macro runs, and it requires no arguments and no special references. The steps below show how to insert it, format it, and avoid the usual pitfalls.
What You'll Need
- Excel for Windows or Excel for Mac with macros enabled.
- Access to the Visual Basic Editor (VBE). On Windows press Alt+F11; on Mac use Tools > Macro > Visual Basic Editor, or the Developer tab if you have enabled it.
- A workbook saved as .xlsm (macro-enabled) if you want to keep your code.
- Basic familiarity with inserting a module and running a macro.
Menu labels in Excel and the VBE can differ slightly by version, platform, and region, so if a command is not exactly where described, look for the closest equivalent.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or use Tools > Macro > Visual Basic Editor (Mac). The VBE opens in a separate window.
- Insert a standard module. In the VBE menu, choose Insert > Module. A blank code window appears. If you already have a module, you can use it instead.
- Start a new subroutine. Type
Sub ShowNow()and press Enter. The VBE automatically addsEnd Sub. - Write a line that uses Now. Between the Sub and End Sub lines, type
MsgBox Now. This displays the current date and time in a message box when the macro runs. - Run the macro. Press F5 (or Run > Run Sub/UserForm). A message box appears with the date and time at the moment of execution. Click OK to close it.
- Write Now to a cell instead. Replace the MsgBox line with
Range("A1").Value = Now. Run the macro again; cell A1 on the active sheet now holds the date and time. - Format the result. The cell may show a serial number or a short date. To control the display, set the NumberFormat property, for example
Range("A1").NumberFormat = "yyyy-mm-dd hh:mm:ss". Run the macro; the cell now shows a full date and time. - Store Now in a variable. Declare a variable with
Dim stamp As Date, then assignstamp = Now. You can reusestamplater in the same macro without calling Now again, which keeps a single consistent timestamp. - Use Now in a calculation. For example,
Range("B1").Value = DateDiff("n", stamp, Now)returns the number of minutes between the stored stamp and the current moment. This is useful for timing how long a process takes. - Save the workbook as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). A normal .xlsx file will drop the macro when saved.
Common Mistakes
- Expecting Now to update automatically. Now is evaluated only when the macro runs. It does not tick like a clock; a cell written once keeps that value until the macro runs again.
- Confusing Now with Date or Time.
Datereturns only the date,Timereturns only the time, andNowreturns both. Use the one that matches what you need. - Forgetting the parentheses. Both
NowandNow()work in VBA, but mixing styles can confuse readers. Pick one and stay consistent. - Assigning to a text-formatted cell. If the target cell is formatted as Text, the date may appear as a serial number or not format correctly. Set the NumberFormat before or after writing the value.
- Using Now inside a loop without thinking. Each call returns a slightly later time. If you need one timestamp for the whole run, capture Now once in a variable and reuse it.
- Saving as .xlsx. The macro disappears on save. Always use .xlsm for workbooks that contain VBA.
Tips
- Use
Format(Now, "yyyy-mm-dd hh:mm:ss")when you need a text string rather than a date value, for example in a log file name. - Capture the timestamp at the start of a long macro so all records share the same time.
- Combine Now with
DateAddto calculate deadlines, such asDateAdd("d", 7, Now)for one week from now. - For date-only stamps, use
Dateinstead of Now to avoid confusing readers with a time that looks like midnight. - Test macros in a copy of the workbook before running them on important data.
Troubleshooting
If the macro does not run, check that macros are enabled in the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows; on Mac, look under Excel > Preferences > Security). If you see a compile error, confirm the line is inside a Sub and that you have not mistyped Now as now in a context where VBA expects a variable. If the cell shows a number like 45123.5 instead of a date, apply a date NumberFormat. If the message box shows an unexpected date format, that reflects your system's regional settings; use Format to force a specific pattern. If the value never changes, remember that Now is static until the macro runs again. If the workbook will not save the macro, save as .xlsm. If you get a "Sub or Function not defined" error, check for a typo in the function name and make sure you are not inside a worksheet event module where the code may need to be placed differently.
Frequently Asked Questions
Does Now update automatically in Excel VBA?
No. Now returns the date and time at the moment the line executes. It does not refresh on its own; you must run the macro again to get a new value.
What is the difference between Now, Date, and Time in VBA?
Now returns both date and time, Date returns only the date, and Time returns only the time. All three are built-in VBA functions.
Can I use Now in a cell formula instead of VBA?
Yes, the worksheet function =NOW() works in a cell and updates when the workbook recalculates. The VBA Now function is separate and runs only when your macro runs.
How do I format the result of Now?
Either set the cell's NumberFormat property, or wrap Now in the Format function, for example Format(Now, "dd/mm/yyyy hh:mm").
Why does Now show a serial number in my cell?
The cell is likely formatted as General or Text. Apply a date or custom date-time format to display it as a readable date.
Can I get a timestamp that does not change when I recalculate?
Yes. Write the value from a macro (for example Range("A1").Value = Now) and it stays fixed until you run the macro again. A worksheet =NOW() formula, by contrast, recalculates.