
Tips.Net > ExcelTips Home > Macros > VBA Examples > Displaying the "Last Modified" Date
Summary: One of the properties that Excel maintains for a workbook is the date it was last modified or saved. This tip explains how you can access that date and place it in a header or footer for a worksheet. (This can only be done with a macro.) (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
If you look at the Properties dialog box for 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 file 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 MyHeader()
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 MyHeader()
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.
Tip #2285 applies to Microsoft Excel versions: 97 2000 2002 2003
Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
Check out ExcelTips Premium today!
It doesn't matter if you are a beginner or expert, the ExcelTips archives are the fastest way to improve your productivity. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site