Excel:强制在保存上重新计算
我有一个简单的宏,可以返回当前文档的最后保存日期:
Function LastSavedTimeStamp() As Date
LastSavedTimeStamp = INT(ActiveWorkbook.BuiltinDocumentProperties("Last Save Time"))
End Function
当我手动强迫单元重新计算时,这可以正常工作,但是当我保存工作表时该单元格不会重新计算 - 这就是什么我想。是否有一种方法可以确保每次保存文档时都可以运行上面的宏,以便自动更新最后的保存日期。
PS。这只有在启用宏时才起作用。很好。
即这就是我想要的
尝试2:受皮肤启发
I have a simple macro that returns the date of the last save of the current document:
Function LastSavedTimeStamp() As Date
LastSavedTimeStamp = INT(ActiveWorkbook.BuiltinDocumentProperties("Last Save Time"))
End Function
This works fine when I manually force the cell to re-calcutate, but the cell doesn't re-calculate when I save the sheet - which is what I want. Is there a way to ensure that the macro above is run everytime the document is saved, such that the date of last save is automatically updated.
PS. This will only work when macros are enabled. That is fine.
I.e. this is what I want
Try 2: Inspired by Skin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我所看到的,您要使用最后保存的值保存文档。这是一个鸡肉和鸡蛋问题。在保存之前,您不会得到最后保存的值...!
首先,我建议将
application.volatile
添加到您当前功能.....这样,当保存文档并随时计算运行时,它将迫使重新计算。
然后,在您的
thisworkbook
对象中,添加以下代码...将在初始保存之后再次保存工作簿,强迫重新计算以确保您的单元格具有正确的值。
有点丑陋,但应该对您有用。
The problem that you have, as I see it, is that you want to save the document with the last saved value. This a chicken and egg issue though. You won't get the last saved value until it's saved ...!
To trick it, firstly, I suggest adding
Application.Volatile
to your current function ..... that way, it will force the recalculation when the document is saved and anytime a calculation runs.
Then, in your
ThisWorkbook
object, add the below code ...That will save the workbook again after the initial save forcing the recalculation to make sure your cell has the correct value.
It's a bit ugly but it should work for you.
您只需要在工作簿模块的Beforesave事件子过程中调用此功能即可。
这是粘贴的代码:
You just need to put a call to this function in the BeforeSave event sub procedure of the work book module.
Here's the code to paste in: