Kurch said:
I want to be able to have the "Last Save" date shown in my header or footer.
I'd also like to know how to have the last save date show in a cell.
How can this be done?
You could use something like this in the ThisWorkbook code module
(CTRL-click in the workbook's title bar and choose View Code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Sheets("Sheet1").Range("A1") = Now
End Sub
but that will cause an incorrect value to be entered if the user cancels
the save.
A better way would be (again in the ThisWorkbook code module):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim wsSheet As Worksheet
For Each wsSheet In ActiveWindow.SelectedSheets
wsSheet.PageSetup.LeftFooter = _
FileDateTime(Me.FullName)
Next wsSheet
End Sub
Note that this could be misleading, too, since you could save the
workbook at 4:00, print it, showing a Last Save time as 4:00, then make
extensive changes and print the file with the Last Save time still
showing 4:00.
You can use FileDateTime in a User Defined Function to return the time
to a cell, too. In a regular code module (enter the VBE with OPT-F11 and
choose Insert/Module):
Public Function LastSaved() As Variant
Application.Volatile
With ThisWorkbook
If .Name = .FullName Then
LastSaved = CVErr(xlErrNA)
Else
LastSaved = CStr(FileDateTime(.FullName))
End If
End With
End Function
Usage:
=LastSaved()
Note that this will return #N/A when entered into a workbook that hasn't
been saved yet. On saving, it won't update until a calculation is
performed in the sheet, either caused by an entry or by pressing F9 or
CMD-= (or by closing and reopening the file if Calculation is set to
automatic). To be sure a printout has the right time, enter this in the
ThisWorkbook code module (instead of the above - you can only have one
Workbook_BeforePrint event macro):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim wsSheet As Worksheet
For Each wsSheet In ActiveWindow.SelectedSheets
wsSheet.Calculate
wsSheet.PageSetup.LeftFooter = _
LastSaved
Next wsSheet
End Sub