Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Displaying the "Last Modified" Date.

Displaying the "Last Modified" Date

Written by Allen Wyatt (last updated November 8, 2021)
This tip applies to Excel 2007, 2010, and 2013


10

If you look at the properties stored with a workbook, you will notice that Excel maintains quite a bit of information concerning the file. One of the items is a date and time that is simply noted as "Modified." Many people refer to this as the "last modified" date, but it really reflects the last time the workbook was saved.

If you want to use this date in your workbook (perhaps in a header or footer), you can do so by using the BuiltinDocumentProperties property (that almost sounds redundant). The following macro will add the proper date to the header of your document:

Sub MyHeader1()
    Dim sLMD As String

    On Error Resume Next

    sLMD = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
    If Err = 440 Then
        Err = 0
        sLMD = "Not Set"
    End If
    sLMD = Left(sLMD, 8)
    ActiveSheet.PageSetup.LeftHeader = "Last Saved: " & sLMD
End Sub

There are a number of items to note in this macro. First of all, it attempts to determine the last date the workbook was saved. If that information cannot be determined, then it sets the header to "Not Set."

Notice that there is some error handling done in this macro. The reason is that Excel will return an error if a particular document property (BuiltinDocumentProperties in this case) is not set. The error needs to be intercepted and handled, which is done here.

There is another item to note here. In some versions of Excel, the Err value returned if the property is not set is not really 440 (as shown here), but some other odd number, such as -2147467259. This is very bizarre, indeed. Why the 440 value (which is the proper error code) would be returned in one circumstance and not in another, I don't know. (Perhaps some other Excel guru will know the answer.) If you have this problem, there are two approaches you can take. First, you can replace the 440 value with the other value (-2147467259). The second option, assuming you have already saved the workbook at least once, is to use a different macro. The following reads the "last modified" attribute from the file itself and stores that info in the header:

Sub MyHeader2()
    Dim fs As Variant
    Dim f As Variant
    Dim sLMD As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(ActiveWorkbook.Path & "\" & _
      ActiveWorkbook.Name)
    sLMD = Left(f.DateLastModified, 8)
    ActiveSheet.PageSetup.LeftHeader = "Last Modified: " & sLMD
End Sub

Regardless of which macro you use, remember that the macro, once run, will set the left header to the desired information. That information will not change again until you run the macro again. Thus, if you always want an up-to-date date in the header, then you should either run the macro periodically (perhaps right before printing) or set it up to run whenever you open your document.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7764) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Displaying the "Last Modified" Date.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Developing Style Families

Styles, as implemented in Word, represent a powerful way to help you easily standardize your formatting tasks. When ...

Discover More

Using an Exact Number of Digits

Excel allows you to format numeric data in all sorts of ways, but specifying a number of digits independent of the ...

Discover More

Reference Shortcut

Need to modify how a cell reference, in a formula, is constructed? The shortcut described in this tip will help you step ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Deleting Zero Values from a Data Table

Want to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can ...

Discover More

Reversing Cell Contents

Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...

Discover More

Converting Strings to Numbers

When working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 6?

2022-09-06 09:59:17

J. Woolley

For more on this subject, see https://excelribbon.tips.net/T011099_Last_Saved_Date_in_a_Footer.html


2022-09-05 15:15:53

J. Woolley

Here are three ways to get the active workbook's last modified (saved) date using functions available in My Excel Toolbox:
=FileLastDate()
=INDEX(ListDocProperties(,,TRUE),12,2)
=VBAResult("ActiveWorkbook.BuiltinDocumentProperties(12)")
Notice "Last Save Time" is the 12th item in BuiltinDocumentProperties.
See https://sites.google.com/view/MyExcelToolbox


2018-03-13 13:03:54

Brian

Rather than looking in the properties to see when a workbook was last saved and then storing it somewhere in the workbook, I store the date and time in the workbook when it is saved.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = False
Worksheets("Main").Range("Date_Saved") = Format(Now, "mm-dd-yyyy")
Worksheets("Main").Range("Time_Saved") = Format(Now, "hh:mm AM/PM")
End Sub

Note that this code needs to be stored in 'ThisWorkbook'


2018-03-13 10:44:30

Brian

Rather than looking in the properties to see when a workbook was last saved and then storing it somewhere in the workbook, I store the date and time in the workbook when it is saved.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = False
Worksheets("Main").Range("Date_Saved") = Format(Now, "mm-dd-yyyy")
Worksheets("Main").Range("Time_Saved") = Format(Now, "hh:mm AM/PM")
End Sub

Note that this code needs to be stored in 'ThisWorkbook'


2018-03-12 14:33:43

Scott

I use the VBA function FileDateTime(PathName As String ) where pathname is the path\filename

Sub LastSaved()
If ActiveWorkbook.path = "" Then
MsgBox "Active Workbook has not been saved"
Else
MsgBox FileDateTime(ActiveWorkbook.path & "\" & ActiveWorkbook.Name)
End If
End Sub


2013-10-04 14:55:04

Bryan

Kim, if you see my comment from August 12, I give a better solution to the Left issue. If you want the time, then just use sLMD = Format(sLMD, "dd/mm/yy h:mm:ss AM")


2013-10-03 12:15:08

Kim

Hi --

I noted that the setting of 8 characters for the LMD in the original code cut-off part of the date (giving, for example, 10/3/201, leaving off the "3" from the year). I extended it to an excessive 25 characters which gives me both the date and the time.

Also, I wanted to be able to see this on some of my workbooks, but not have it printed in the headers, so I set it up to put the date/time in cell A1, usually an empty location in my workbooks.

The resulting code follows:

Sub LastSaved()
Dim sLMD As String

On Error Resume Next

sLMD = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
If Err = 440 Then
Err = 0
sLMD = "Not Set"
End If
sLMD = Left(sLMD, 25)
Range("A1").Value = "Last Saved: " & sLMD
End Sub

Thanks -- this was a great tip! When developing new reports for the department, there are often multiple versions.

Kim


2013-08-12 08:58:15

Bryan

Interesting side note, MyHeader1 actually gives me a time once an AutoSave has been created. MyHeader2 will only work once the file has been saved for real.


2013-08-12 08:35:24

Bryan

@jws500 I think it's fairly common practice to use Err <> 0. The only reason why you would use something else is if you wanted to trap multiple errors in different ways. A lot of times if I know there is one type of error I will encounter, I will trap it and then have an Else condition which pauses the macro for any other error. That way if there's some error condition I never knew could happen, I can deal with it when I get there (this is obviously more of a debugging solution, not something you'd want to distribute).

As for Allen's use, I don't understand the point of the Err = 0 line. I don't think it does anything, and if it does, I don't think it changes anything about the execution of the rest of the macro. Also, using the left portion of a date string is a terrible way to do it. 1/1/2013 has 8 characters, but 12/12/2013 has 10, and your macro won't work. You should use the Format function instead.

I think in this case a better way to handle it would be:

Sub MyHeader1()
Dim sLMD As String

On Error Resume Next
sLMD = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
On Error GoTo 0

If Len(sLMD) = 0 Then
sLMD = "Not Set"
Else
sLMD = Format(sLMD, "d/m/yyyy")
End If

ActiveSheet.PageSetup.LeftHeader = "Last Saved: " & sLMD
End Sub

It's probably not the best way, but it's cleaner and easier to understand.

There's also no reason why you couldn't add appropriate error handling and formatting to the second macro as well (although with the above fixes in place, the second macro is just redundant, aside from its cryptic and limited teachings about creating a FileSystemObject).


2013-08-11 11:42:10

jws500

Thanks for a useful tip. I've done something similar, but without the error checking. I will add error handling to my macro.

Question: Is there a reason you test explicitly for a single error number rather than something like:
If Err <> 0 Then


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.