Smart Systems for Home

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

VBA Excel Timestamp in GMT: A How-To Guide

· 7 min read · Home Tech

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

To write a GMT/UTC timestamp in Excel with VBA, use the Windows API or the worksheet formula approach: the simplest reliable method is Format(Now, "yyyy-mm-dd hh:mm:ss") only if your PC clock is set to UTC — otherwise convert local time with the difference between Now and UTCNow-equivalent values. Excel's built-in Now returns local time only, so the practical approach is to subtract the local time zone offset from Now, or to call the Windows GetSystemTime API, which returns UTC directly. The steps below cover both, plus a formula-only fallback for machines where macros are blocked.

What You'll Need

  • Excel on Windows (the API method uses Windows system calls; on Mac Excel those declarations will not work — use the offset method instead).
  • A workbook saved as .xlsm (macro-enabled) so your code is kept.
  • Macro security set to allow macros in trusted locations, or the file added to a Trusted Location.
  • Basic familiarity with the VBA editor: press Alt+F11 to open it, Ctrl+G for the Immediate window.
  • The VBA project's Option Explicit at the top of modules is recommended so typos in variable names are caught.

Menu and dialog labels in Excel and the VBA editor can differ slightly by version, device and region, so if a name does not match exactly, look for the closest equivalent.

Step-by-Step

  1. Open the VBA editor. In Excel press Alt+F11. In the Project Explorer, right-click your workbook and choose Insert > Module. A blank code module opens.
  2. Add Option Explicit at the top of the module. This forces you to declare variables and prevents silent bugs such as a misspelled function name returning an empty string.
  3. Write the offset-based function first, because it works on both Windows and Mac Excel and needs no API declarations:
    Function GMTStamp() As String
        Dim offset As Double
        offset = (Now - Date) - (GetUTC - Date)
        GMTStamp = Format(Now - offset, "yyyy-mm-dd hh:mm:ss") & " UTC"
    End Function
    This is a sketch: GetUTC is not a built-in VBA function, so the practical version is the API method in the next step, or the formula method further down. Do not paste a function that calls a name you have not defined — it will fail to compile.
  4. Use the Windows API for true UTC. In the same module, at the very top (above any Sub or Function), add:
    #If VBA7 Then
        Private Type SYSTEMTIME
            wYear As Integer
            wMonth As Integer
            wDayOfWeek As Integer
            wDay As Integer
            wHour As Integer
            wMinute As Integer
            wSecond As Integer
            wMilliseconds As Integer
        End Type
        Private Declare PtrSafe Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
    #Else
        Private Type SYSTEMTIME
            wYear As Integer
            wMonth As Integer
            wDayOfWeek As Integer
            wDay As Integer
            wHour As Integer
            wMinute As Integer
            wSecond As Integer
            wMilliseconds As Integer
        End Type
        Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
    #End If
    The #If VBA7 block keeps the code working in both 32-bit and 64-bit Office; PtrSafe is required for 64-bit.
  5. Add the function that returns the stamp:
    Function GMTNow() As String
        Dim st As SYSTEMTIME
        GetSystemTime st
        GMTNow = Format(DateSerial(st.wYear, st.wMonth, st.wDay), "yyyy-mm-dd") & " " & _
                  Format(TimeSerial(st.wHour, st.wMinute, st.wSecond), "hh:mm:ss") & " UTC"
    End Function
    GetSystemTime returns Coordinated Universal Time, so no offset maths is needed and daylight saving changes are handled by the operating system.
  6. Test it in the Immediate window. Press Ctrl+G, type ?GMTNow and press Enter. Compare the result with a known UTC source. If the hour is wrong by a whole number of hours, your PC clock or time zone setting is the cause, not the code.
  7. Write the stamp into a cell. In a normal Sub, for example:
    Sub StampRow()
        Dim r As Long
        r = ActiveCell.Row
        Cells(r, "A").Value = GMTNow
    End Sub
    Assign it to a button (Developer tab > Insert > Button) or call it from a worksheet event. If you want a stamp that does not change when the sheet recalculates, write the text value as above rather than using a volatile formula.
  8. Store as text, not as a date, if the stamp is for logging. A string like 2025-03-14 09:21:07 UTC is unambiguous. If you need to sort or subtract, store the numeric UTC value instead (for example DateSerial(...) + TimeSerial(...)) and format the cell with a custom format such as yyyy-mm-dd hh:mm:ss "UTC".
  9. Formula-only fallback. If macros are blocked, Excel has no built-in UTC function. You can subtract your offset manually: put the offset in a cell (for example -5 or +1) and use =NOW()-offset/24, formatted as yyyy-mm-dd hh:mm:ss. This does not adjust for daylight saving automatically, so update the offset when clocks change.
  10. Save and secure. Save as .xlsm. Keep the module free of anything that writes outside the workbook, and sign or trust the file rather than lowering macro security globally.

Common Mistakes

  • Using Now and calling it GMT. Now is local time. On a machine set to a non-UTC zone the stamp will be wrong by hours.
  • Forgetting PtrSafe. On 64-bit Office, an old Declare Sub without PtrSafe raises a compile error.
  • Putting Declare statements inside a Sub. They must be at module level, before any procedure.
  • Mixing Date and text. Concatenating a date value without Format gives the regional short-date form, which may be misread by other systems.
  • Relying on the offset method across daylight saving changes. A hard-coded offset is wrong twice a year in many regions.
  • Naming a variable Date or Time. These are reserved and will break your code.

Tips

  • Use ISO 8601 order (yyyy-mm-dd hh:mm:ss) so stamps sort correctly as text and are unambiguous across regions.
  • Append the literal UTC or Z so nobody mistakes the value for local time.
  • For audit trails, write the stamp as a static value, not a formula; formulas recalculate and overwrite history.
  • Keep one module for time functions and reuse it across workbooks by exporting and importing the module.
  • If the workbook is shared, note that macros and API calls behave the same for every user, but each user's clock must be correct for the stamp to be trustworthy.

Troubleshooting

Compile error on the Declare line. Check the #If VBA7 block is closed with #End If and that the declarations sit above all procedures. In 64-bit Office, PtrSafe is mandatory.

The stamp is hours off. Check Windows date and time settings, including the time zone and automatic daylight saving. The API returns whatever the operating system reports.

Macro will not run. The file may be blocked (check the yellow bar and choose to enable content), or the folder may not be a Trusted Location. Prefer adding a trusted folder over disabling macro security.

Mac Excel. The kernel32 declaration does not exist on macOS. Use the formula method with an offset, or use Now and convert with a fixed offset, accepting that daylight saving is not handled.

If that did not work, strip the code back: test ?Now in the Immediate window, then test a one-line function returning Format(Now, "yyyy-mm-dd hh:mm:ss"). Once that works, add the UTC layer. This isolates whether the problem is the API declaration or your cell-writing code.

Frequently Asked Questions

Does Excel have a built-in UTC or GMT function?

No. Worksheet functions return local time. You need VBA, a manual offset, or a Power Query transformation to get UTC.

Why does my stamp change when I reopen the file?

You used a formula such as NOW() in the cell. Write the value from VBA as text or as a static number so it stays fixed.

Will the API method work on 32-bit and 64-bit Office?

Yes, if you use the conditional compilation block with PtrSafe for VBA7 and the plain declaration for older versions.

How do I handle daylight saving time?

Use GetSystemTime, which returns UTC and is unaffected by daylight saving. A hard-coded offset will be wrong part of the year.

Can I get milliseconds?

SYSTEMTIME includes wMilliseconds, but Excel's date type is coarser. Append the millisecond field as text if you need it in a log.

Is a text stamp or a real date better?

Text is better for logs and cross-system exchange; a real date value is better if you need to sort, filter or subtract times inside Excel.