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

Displaying the "Last Modified" Date

by Allen Wyatt
(last updated January 26, 2013)

2

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 (2285) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Removing Hyperlinks without a Macro, Take Two

Need to get rid of hyperlinks in a worksheet? Here's an easy way to do it without using a macro.

Discover More

Put Your Space Before or After?

When working with spacing between paragraphs, Word allows you to specify exactly how much space should be either before or ...

Discover More

ExcelTips Menu 2011 Archive (Table of Contents)

ExcelTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At the ...

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 (menu)

Simultaneous Scrolling

If you have two worksheets displayed at the same time, you might want those worksheets to remain visually "in sync" with each ...

Discover More

Pulling Cell Names into VBA

Excel allows you to define names that can refer to either ranges of cells or to constant information, such as formulas. If ...

Discover More

Making a Cell's Contents Italics within a Macro

You can use macros to process information in your worksheets. You may want to use that macro to apply the italic attribute to ...

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 8Mpixels. 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 0 + 7?

2013-01-30 06:39:27

Barry Fitzpatrick

Getting the username is covered in other Excel Tips articles (http://excel.tips.net/T007251_Inserting_the_Users_Name_in_a_Cell.html)

Regarding disabling of macros, however, if a user opens the worksheet with macros disabled there's not a lot that can be done about this. A workaround would be to save the spreadsheet with all cells protected with a password using the "BeforeSave" event. Only knowing the password or allowing macros to run (with "workbook_Open" event removing the protection using the password). However, shared spreadsheets often use protection to prevent Users messing around with the spreadsheet formulas, layout/structure, etc., so this might be inconvenient as the User accessible cells would have to be locked then unlocked, and vice versa when saving. Note: as the actual password would be held in plain text within the macros, you would have to separately password protect the macros as well.

BTW Excel security is not very strong and there are many Excel passwords cracking softwares on the Internet. So such measures will not prevent a determined hacker from accessing the spreadsheet and its macros. There are commercial protection softwares which are much stronger.


2013-01-29 16:58:59

Dorothy

Interesting tip, but is there a way to record each time the workbook was modified. Eg on a separate tab, cell A1 first save, A2 second save etc. It would be even better if it could include the user name with the saved time. How would this work if a person opened the workbook without enabling macros? eg I saved the workbook January 1. Unknown person opened without macros and saved January 5. I opened January 9th. When I opened on the 9th would it show the Jan 5 save?


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.