VBA Excel Timestamp in GMT: A How-To Guide
In this article
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+F11to open it,Ctrl+Gfor the Immediate window. - The VBA project's
Option Explicitat 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
- 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. - Add
Option Explicitat 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. - Write the offset-based function first, because it works on both Windows and Mac Excel and needs no API declarations:
This is a sketch: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 FunctionGetUTCis 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. - Use the Windows API for true UTC. In the same module, at the very top (above any
SuborFunction), add:
The#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#If VBA7block keeps the code working in both 32-bit and 64-bit Office;PtrSafeis required for 64-bit. - 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 FunctionGetSystemTimereturns Coordinated Universal Time, so no offset maths is needed and daylight saving changes are handled by the operating system. - Test it in the Immediate window. Press
Ctrl+G, type?GMTNowand 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. - Write the stamp into a cell. In a normal
Sub, for example:
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.Sub StampRow() Dim r As Long r = ActiveCell.Row Cells(r, "A").Value = GMTNow End Sub - Store as text, not as a date, if the stamp is for logging. A string like
2025-03-14 09:21:07 UTCis unambiguous. If you need to sort or subtract, store the numeric UTC value instead (for exampleDateSerial(...) + TimeSerial(...)) and format the cell with a custom format such asyyyy-mm-dd hh:mm:ss "UTC". - 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
-5or+1) and use=NOW()-offset/24, formatted asyyyy-mm-dd hh:mm:ss. This does not adjust for daylight saving automatically, so update the offset when clocks change. - 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
Nowand calling it GMT.Nowis 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 oldDeclare SubwithoutPtrSaferaises a compile error. - Putting
Declarestatements inside aSub. They must be at module level, before any procedure. - Mixing
Dateand text. Concatenating a date value withoutFormatgives 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
DateorTime. 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
UTCorZso 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.