nbozzy
asked on
user-defined function for last saved date/time not updating
Following this posting's instructions, I have a Public Function in Module1, and a formula in a cell. However, the time is not updating every time I save the file....it's stationary at the time I first saved. Help?
P.S. In the cell, I have: =Doc_Saved()
Thanks!
P.S. In the cell, I have: =Doc_Saved()
Thanks!
Public Function Doc_Saved() As Date
'returns date & time last saved
Application.Volatile
Doc_Saved = Format$(ActiveWorkbook.BuiltinDocumentProperties("Last Save Time"), "dd/mm/yyyy hh:mm")
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Patrick:
I just tried F9, and that doesn't update my spreadsheet, either....??
I just tried F9, and that doesn't update my spreadsheet, either....??
nbozzy,
Please post a sample file.
In any event, forcing a recalc at close seems odd, as your users will never get a chance to see it. You could try adding this to the ThisWorkbook module:
Patrick
Please post a sample file.
In any event, forcing a recalc at close seems odd, as your users will never get a chance to see it. You could try adding this to the ThisWorkbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook
.Save
Application.Calculate
.Save
End With
End Sub
Patrick
ASKER
Then is there a way to force recalculation after the user saves? Here is file...
Staffing-Calendar-Input.xls
Staffing-Calendar-Input.xls
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This thing is stuck! I make your change above, and time STILL shows my original save.....??
Staffing-Calendar-Input.xls
Staffing-Calendar-Input.xls
nbozzy,
Why did you disable events in your Workbook_Open event?!?
Patrick
Why did you disable events in your Workbook_Open event?!?
Patrick
ASKER
oh, crap! I never enabled them!!!
However, after adding the enableEvents=True line, then saving and closing the file, there is no difference when I re-open. Save time still doesn't change.
However, after adding the enableEvents=True line, then saving and closing the file, there is no difference when I re-open. Save time still doesn't change.
Your problem is that you had this line in your Workbook_Open event:
Since you disabled events, Before_Save is not firing.
1) Remove that line from Workbook_Open
2) In the VB Editor, hit Ctrl+G to get the Immediate window, and in it type Application.EnableEvents=T rue and then hit Enter
The BeforeSave event will now fire and get your formula to update as expected.
Application.EnableEvents = False
Since you disabled events, Before_Save is not firing.
1) Remove that line from Workbook_Open
2) In the VB Editor, hit Ctrl+G to get the Immediate window, and in it type Application.EnableEvents=T
The BeforeSave event will now fire and get your formula to update as expected.
And also be sure that you have macros enabled :)
ASKER
I do have macros enabled, but this is too weird. Still won't update. Tell me if it updates for you....
Staffing-Calendar-Input.xls
Staffing-Calendar-Input.xls
It updates for me
ASKER
Ok, I'm clueless. I rebooted my PC, did a SaveAs with a new name, verified macros are enabled, and that time STILL doesn't update. What should I do??
I've asked some other Experts to drop in and have a look.
Works for me too, Nancy.
hmmmmm. Put a Break point on the ThisWorkbook.Save statement in the Workbook_BeforeSave event. Hit save and advise that the breakpoint is hit and macro paused, or not.
Dave
hmmmmm. Put a Break point on the ThisWorkbook.Save statement in the Workbook_BeforeSave event. Hit save and advise that the breakpoint is hit and macro paused, or not.
Dave
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's what I was thinking, Thomas - that the event is not even firing.
Dave
Dave
ASKER
Thanks, everyone. The BeforeSave event was firing twice (?) and the Function never ran. For some reason, I commented out the line "ThisWorkbook.Save" in BeforeSave() and now it runs correctly.
I liked the debugging "msgbox" approach that nutsch and TommyS suggested!
Thanks to all!
I liked the debugging "msgbox" approach that nutsch and TommyS suggested!
Thanks to all!
It looks like Application.Volatile is not working as I would expect it to work.
This will work:
Public Function Doc_Saved(ByVal Dummy As Variant) As Date
'returns date & time last saved
Application.Volatile
Doc_Saved = ActiveWorkbook.BuiltinDocu mentProper ties("Last Save Time")
End Function
And then pass a volatile function to the UDF:
=Doc_Saved(NOW())
Kevin
This will work:
Public Function Doc_Saved(ByVal Dummy As Variant) As Date
'returns date & time last saved
Application.Volatile
Doc_Saved = ActiveWorkbook.BuiltinDocu
End Function
And then pass a volatile function to the UDF:
=Doc_Saved(NOW())
Kevin
Using break-points are also a solution / alternative to putting messageboxes in.
Enjoy
Dave
Enjoy
Dave
Oops missed that one. ThisWorkbook.Save actually calls the BeforeSave event so it must have been confusing it. As long as you don't set Cancel to True, then the workbook will be automatically saved after the BeforeSave function runs.
LOOOOOOOOOOP??
Try
Try
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
application.enableevents=false
ThisWorkbook.Save
Application.Calculate
application.enableevents=true
End Sub
When Patrick calls, everybody answers it seems.
its like we're an army!
lol
Dave
lol
Dave
When anyone posts in the Regulars thread we help.
ASKER
You're all fantastic. I love this website!!! Thanks to all.
If you're using beforesave, why bother with the UDF at all? Just write the time to the cell in the event code.
ASKER
Good point, rorya! The process evolved that way because I needed to force a recalculate....thanks for the slap back to reality!
ASKER