In Excel VBA, use the built-in FileDateTime function to read a file’s last-modified date and time. For the workbook containing the macro, pass ThisWorkbook.FullName; the returned value represents the timestamp of the saved file on disk, not the time of the most recent unsaved edit.
Read the current workbook’s timestamp
This example displays the last-modified date and time for the workbook that contains the code. In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste the procedure into the module. On a Mac, open the Visual Basic Editor with Option+F11 or use Tools > Macro > Visual Basic Editor, depending on your keyboard settings and Excel build. The VBA is the same, but the file path supplied to VBA follows the host operating system.
Save a copy of the workbook before running any macro. VBA actions cannot be undone with Ctrl+Z (or Command+Z on Mac). This particular procedure only reads the file timestamp and shows a message; it does not change workbook contents.
Sub ShowWorkbookLastModified()
Dim targetPath As String
Dim modifiedAt As Date
' An unsaved workbook has no disk file to inspect.
If Len(ThisWorkbook.Path) = 0 Then
MsgBox "Save the workbook before checking its file date."
Exit Sub
End If
targetPath = ThisWorkbook.FullName
On Error GoTo FileError
modifiedAt = FileDateTime(targetPath)
MsgBox "Last modified: " & _
Format$(modifiedAt, "yyyy-mm-dd hh:nn:ss")
Exit Sub
FileError:
MsgBox "The file date could not be read: " & Err.Description
End Sub
For example, if the file’s recorded timestamp is November 3, 2026 at 4:25 PM, the formatted message is Last modified: 2026-11-03 16:25:00. In VBA date formats, nn means minutes; use it rather than mm, which represents the month in a date format.
What FileDateTime returns
FileDateTime(path) is a VBA function that returns a Variant containing a date value for the specified file’s last modification. It reads file-system information; it does not inspect the workbook’s cells, calculate a date from a worksheet, or determine who made the change. The returned value can be assigned to a variable declared as Date, displayed with Format$, or placed in a worksheet cell.
Because the result is a date value rather than merely a display string, VBA can also compare it with another date or use it in date calculations. Formatting changes how the date appears, not the underlying timestamp. For instance, a short date format may hide the time, while a custom format such as yyyy-mm-dd hh:nn:ss makes both parts visible.
The timestamp generally reflects the file system’s record for the saved file. The resolution and behavior can depend on the storage system, so seconds shown by a format do not guarantee that every drive or cloud service records changes to the nearest second. File systems, sync tools, and server policies can affect what timestamp is reported.
Choose the correct workbook and path
The path argument determines which file VBA checks. For a macro stored in the workbook being checked, ThisWorkbook.FullName is often the clearest choice: ThisWorkbook means the workbook containing the running code, and FullName includes its folder and file name. It avoids accidentally reading another workbook simply because a user clicked into it.
ActiveWorkbook.FullName instead refers to whichever workbook is currently active. That distinction matters when a macro is stored in PERSONAL.XLSB, an add-in, or a separate controller workbook. If the active workbook changes, the target may change too. A practical rule is:
- Use
ThisWorkbook.FullNameto check the workbook that stores the macro. - Use
ActiveWorkbook.FullNamewhen the macro is intentionally meant to check the workbook the user selected. - Use a specific full path when checking a different known file, and make sure that path exists and is accessible.
A newly created workbook that has not been saved has no full file path, which is why the example checks ThisWorkbook.Path first. A workbook saved once has a path even if later edits have not yet been saved. In that case, FileDateTime still reports the disk file’s current timestamp, not the pending changes in Excel’s memory.
Show the date in a worksheet cell
If the goal is to display the file timestamp on a worksheet rather than in a message box, assign the date value to a cell and apply a number format. For example, the following procedure writes the timestamp of the workbook containing the macro into cell A1 on the active sheet. It checks for an unsaved workbook before trying to read the file.
Save a copy before running this macro as well: it changes a worksheet cell, and VBA changes cannot be reversed with Ctrl+Z or Command+Z.
Sub WriteWorkbookModifiedDate()
Dim modifiedAt As Date
If Len(ThisWorkbook.Path) = 0 Then
MsgBox "Save the workbook before checking its file date."
Exit Sub
End If
On Error GoTo FileError
modifiedAt = FileDateTime(ThisWorkbook.FullName)
With ActiveSheet.Range("A1")
.Value = modifiedAt
.NumberFormat = "yyyy-mm-dd hh:mm:ss"
End With
Exit Sub
FileError:
MsgBox "The file date could not be read: " & Err.Description
End Sub
This writes a snapshot when the macro runs; it does not create a continuously updating value. Also, saving the workbook after the macro writes A1 changes the workbook file’s modification timestamp. The cell therefore records the timestamp read before that save, not the timestamp produced by the save that follows. This timing detail is important if the cell is intended as a record of the latest saved version.
File timestamp versus workbook properties
A file’s last-modified timestamp and a workbook’s document properties are different kinds of information. The file-system timestamp says when the stored file was last changed according to its storage system. Workbook properties such as title, author, or other document metadata describe information saved within the Office file. They are not interchangeable, and a workbook property should not be assumed to equal the operating system’s last-modified time.
Similarly, a timestamp does not identify the editor, describe which cells changed, or prove that a particular person made an edit. Anyone or any process that saves or copies a file may affect its timestamp. If an audit trail or a record of changes is required, a single file date is not a substitute for version history, controlled logging, or an appropriate document-management system.
Another common misconception is that a worksheet formula can always retrieve the file’s operating-system modification time. Ordinary Excel worksheet functions do not provide a general-purpose way to read arbitrary file-system metadata. VBA can do so through functions such as FileDateTime, but macros must be permitted to run, and the file must be accessible to the computer running Excel.
Unsaved edits, saving, and automatic updates
Excel keeps an open workbook in memory and writes changes to its file when the workbook is saved. Until then, a macro reading FileDateTime sees the timestamp of the last saved disk version. This explains why editing a cell and immediately running the read-only example may show an earlier time. Save first if you want the timestamp to reflect the latest workbook contents already written to disk.
Trying to update a cell with the file’s latest modification time every time the workbook saves creates a timing problem: writing the timestamp into the workbook is itself a change that must be saved. The time recorded before saving cannot be the timestamp of that later save. A second save may then change it again. A cell can instead show a “checked at” time or the file timestamp observed when the macro ran, but those labels should not imply that it is an exact, self-updating record of the final save.
For a simple display, run the macro on demand after saving. For a workflow that needs history, consider storing dated entries in a separate log or using the file’s version-history facility where available. Choose the approach based on whether you need a convenient status display or a dependable record of multiple versions.
Windows, Mac, and cloud-file considerations
In Excel for Microsoft 365 on Windows, the example uses a normal Windows file path supplied by Excel through ThisWorkbook.FullName. Avoid hard-coding a user-specific path when you can use the workbook’s own full path; folder names and drive letters differ between computers. For network locations, the user running Excel must have access to the file and permission to read its metadata.
Excel for Mac also supports VBA, but paths and file access can differ from Windows. Let Excel provide the path through FullName rather than assuming a Windows-style drive path such as C:\Folder\Book.xlsm. Macro availability and permissions may also be affected by Excel’s security settings and macOS privacy controls. Excel for the web does not run VBA macros, so this method requires the desktop Excel application.
With OneDrive, SharePoint, or another synchronized location, the path and timestamp VBA reads may describe the local synchronized file or the storage provider’s representation of it. Sync delays, offline edits, uploads, and server-side versioning can make a cloud interface’s displayed time differ from the local file timestamp. If the authoritative time matters, check the timestamp or version history in the service that owns the file rather than assuming a local VBA result is the cloud’s definitive record.
Common errors and how to troubleshoot
FileDateTime can raise an error if the supplied path is missing, misspelled, inaccessible, or points to a file that cannot currently be read. The example uses a basic error handler so the user sees a message instead of an unhandled VBA error. For a production macro that processes many files, handle errors per file and record which paths failed rather than stopping an entire batch at the first problem.
- Workbook has never been saved: check that its path is not empty, then save it before reading its file date.
- Wrong workbook is checked: confirm whether the code should use
ThisWorkbookorActiveWorkbook. - Path or permissions issue: confirm that the file exists and that the current user can access its folder.
- Time appears missing: use a date-and-time number format; the timestamp may be present but hidden by the display format.
- Time differs from another display: check whether the other display refers to a cloud version, a synchronized copy, or a different time zone or storage system.
Do not interpret the displayed time as proof of the exact moment a person clicked Save. It is a file-system timestamp with the precision and handling of the storage environment, and applications or synchronization processes can affect it.
Frequently Asked Questions
What is the VBA function for a file’s last-modified date?FileDateTime(path) returns the date and time recorded for the specified file. Pass a full path, such as ThisWorkbook.FullName for the workbook containing the macro.
How do I get the last modified date of the active workbook?
Use FileDateTime(ActiveWorkbook.FullName) when the active workbook is the intended target. Check that it has been saved and has a valid path first.
Does FileDateTime include the time?
Yes. It returns a date value that includes a time component when the file system provides one. Use a date-and-time display format, such as yyyy-mm-dd hh:nn:ss in VBA’s Format$ function, to show it clearly.
Will it show edits I have not saved?
No. It reads the saved file’s metadata on disk, so unsaved edits in the open workbook do not change the timestamp that the function reads.
Can I use this in Excel for the web?
No. Excel for the web does not execute VBA. Open the workbook in desktop Excel for Windows or Mac and allow the macro to run, subject to your organization’s macro-security policies.
Related
- Excel Split Cell Content Into Columns: Methods That Work
- Excel Split Cell Contents by Delimiter
- Excel Split Cell Based on Character: Methods That Work
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Date and Time Stamp: Add Fixed TimestampsTo add a fixed date-and-time stamp with Excel VBA, assign Now to a cell and apply a date-time…
- Excel VBA InputBox: Ask for User InputIn Excel VBA, use InputBox to display a simple prompt and collect a response, or use Application.InputBox when…
- Excel VBA Runtime Error 91: Causes and FixesExcel VBA runtime error 91 means your code tried to use an object variable that does not currently…
- Excel VBA: Get the Current Workbook File Path EasilyIn Excel VBA, use ThisWorkbook.Path to get the folder path of the workbook that contains the code, or…
- Excel VBA: Loop Through All Files in a FolderTo loop through files in a folder with Excel VBA, use the Dir function to get the first…
- VBA Loop Structures: How to Choose and Use Each TypeVBA has three main loop structures: For...Next for a known number of repetitions, For Each...Next for processing items…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.