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

by Allen Wyatt
(last updated August 10, 2013)

5

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.

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

Spell-Checking from the Keyboard

If you hate to take your hands from the keyboard, even to right-click on a word, you'll love the information in this tip. ...

Discover More

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Establishing a FLOOR and CEILING

Excel includes a surprising number of functions you can use to round your data. Two such functions are FLOOR and CEILING, ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Macro, while Running, Stops Excel from Responding

When running a macro, have you ever seen Excel appear to stop responding? This can be frustrating, but there are a couple of ...

Discover More

Clean Up Your Macro List

Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.

Discover More

Running a Macro when a Worksheet is Deactivated

When you change from one worksheet to another, you may want to have Excel automatically run a macro for the worksheet you are ...

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}] 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 two more than 4?

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.