Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Now Format: How to Display Current Date and Time

· 5 min read · Smart Home

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Using Excel VBA to format the Now function lets you display the current date and time in any custom format you need—like 'YYYY-MM-DD HH:MM:SS' or 'MM/DD/YYYY at h:mm AM/PM'—directly in cells, message boxes, or log files. This is essential for timestamps in automated reports, audit trails, or user interfaces. No external tools or add-ins are required; everything works with standard VBA functions in Excel.

What You'll Need

  • Microsoft Excel 2010 or later (32-bit or 64-bit)
  • Enable Developer tab in Excel (if not already visible)
  • Basic familiarity with opening the VBA editor (Alt+F11)
  • A workbook where you plan to run or store the macro

Step-by-Step

  1. Open Excel and press Alt+F11 to open the Visual Basic for Applications (VBA) editor.
  2. In the Project Explorer pane on the left, right-click your workbook name (e.g., VBAProject (YourWorkbook.xlsx)) and select Insert > Module. This creates a blank code window.
  3. Type or paste the following line into the module: Sub ShowFormattedNow(). This begins your subroutine.
  4. On the next line, type: Dim currentTime As String. This declares a variable to store your formatted timestamp.
  5. On the next line, type: currentTime = Format(Now, "yyyy-mm-dd hh:mm:ss"). This uses the Format function to convert Now() into a standardized ISO-style format. You can change the format string to suit your needs—see the Tips section for examples.
  6. On the next line, type: Range("A1").Value = currentTime. This writes the formatted time into cell A1 of the active worksheet. Replace "A1" with any cell reference you prefer.
  7. On the next line, type: End Sub to close the subroutine.
  8. Press F5 while your cursor is inside the subroutine to run it immediately. The current date and time will appear in cell A1 in your chosen format.
  9. To reuse this later, save your workbook as a Macro-Enabled Workbook (.xlsm) by going to File > Save As and selecting Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown.
  10. Optional: To trigger this automatically when opening the file, rename the subroutine to Private Sub Workbook_Open() and place it in the ThisWorkbook module instead of a standard Module. Then, close and reopen the workbook—your timestamp will populate automatically.

Common Mistakes

  • Using Now without parentheses: Now is a function and must be called as Now() in VBA. Omitting the parentheses can cause compile errors or unexpected behavior in some contexts.
  • Using incorrect format string syntax: Excel VBA uses specific codes like yyyy for four-digit year, not YYYY. Using uppercase Y or lowercase y incorrectly will return the wrong value or an error.
  • Forgetting to declare the variable: If you skip Dim currentTime As String, VBA may still run but can cause type mismatches if you later try to manipulate the value as text.
  • Writing to a protected cell: If the target cell is locked or the worksheet is protected, the code will error. Unlock cells or unprotect the sheet before running the macro.
  • Running the macro without saving as .xlsm: If you close the workbook without saving it as a macro-enabled file, the VBA code will be lost the next time you open it.

Tips

  • Use Format(Now(), "mm/dd/yyyy hh:mm AM/PM") for a familiar U.S. format with AM/PM.
  • Use Format(Now(), "dddd, mmmm dd, yyyy") to output "Wednesday, April 17, 2024".
  • To include milliseconds, use Format(Now(), "yyyy-mm-dd hh:mm:ss.000"). Note that Now() doesn’t capture milliseconds by default—this will show .000 unless you use a more precise source like Timer().
  • Use Format(Now(), "hhmmss") for a numeric-only timestamp like "143022" useful for filenames.
  • Combine with Date() if you only need the date: Format(Date(), "yyyy-mm-dd").
  • For international formats, use Format(Now(), "dd/mm/yyyy hh:mm") for European-style dates.
  • Always test your format string in a message box first: MsgBox Format(Now(), "your format") to preview before writing to a cell.

Troubleshooting

  • "Compile error: Invalid outside procedure" — You pasted code outside a Sub or Function. Ensure all your code is between Sub and End Sub.
  • "Run-time error '1004': Application-defined or object-defined error" — The cell reference (e.g., "A1") doesn’t exist on the active sheet, or the sheet is protected. Check spelling and unprotect the sheet via Review > Unprotect Sheet.
  • Format shows wrong year or month — You likely used YYYY instead of yyyy. VBA is case-sensitive for format codes. Use lowercase y, m, d for year, month, day.
  • Macro won’t run or is disabled — Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" temporarily for testing (revert to safer settings afterward). Alternatively, enable macros when prompted upon opening the file.
  • Time shows 12:00:00 AM — You used Date() instead of Now(). Date() returns only the date with no time component. Use Now() to include both.
  • Code runs but nothing appears in the cell — The cell might be formatted as text or has a custom number format overriding your string. Right-click the cell > Format Cells > choose "General" or "Text", then rerun the macro.

Frequently Asked Questions

Can I format Now() to include timezone info in Excel VBA?

No, VBA’s Now() returns the system local time only. It does not include timezone offsets. To add timezone data, you must manually append a string like " (EST)" or use Windows API calls, which are complex and not recommended for basic use.

Why does my time show as 24-hour format even when I use AM/PM?

You likely used "hh" instead of "h". Use "h" for 12-hour format without leading zero (e.g., 1:30 PM), and "hh" for leading zero (e.g., 01:30 PM). Both work with AM/PM.

Can I use Now() in a worksheet formula instead of VBA?

Yes, you can use =NOW() directly in a cell, but it updates every time the sheet recalculates. VBA lets you capture and store a static timestamp that won’t change unless the macro runs again.

What if I need precision beyond seconds?

Now() does not provide millisecond precision. For higher accuracy, use Timer() to get seconds since midnight as a decimal, then combine with Now(): Format(Now(), "yyyy-mm-dd hh:mm:ss") & "." & Right("000" & CStr((Timer - Int(Timer)) * 1000), 3).

Will this work in Excel Online or mobile apps?

No. VBA macros are not supported in Excel for the web, iOS, or Android. This method only works in desktop versions of Excel for Windows or macOS.

Can I format Now() to display only the date without time?

Yes. Use Format(Now(), "yyyy-mm-dd") or Format(Date(), "mm/dd/yyyy"). Date() is more explicit for date-only values and ignores time entirely.