Excel:强制在保存上重新计算

发布于 2025-01-21 10:45:47 字数 929 浏览 3 评论 0原文

我有一个简单的宏,可以返回当前文档的最后保存日期:

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

enter image description here


Try 2: Inspired by Skin

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清秋悲枫 2025-01-28 10:45:47

正如我所看到的,您要使用最后保存的值保存文档。这是一个鸡肉和鸡蛋问题。在保存之前,您不会得到最后保存的值...!

首先,我建议将application.volatile添加到您当前功能...

Function LastSavedTimeStamp() As Date
    Application.Volatile
    
    LastSavedTimeStamp = Int(ActiveWorkbook.BuiltinDocumentProperties("Last Save Time"))
End Function

..这样,当保存文档并随时计算运行时,它将迫使重新计算。

然后,在您的thisworkbook对象中,添加以下代码...

Public DO_NOT_CALCULATE As Boolean

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    Dim objWorksheet As Worksheet

    If Not DO_NOT_CALCULATE Then
        For Each objWorksheet In ThisWorkbook.Worksheets
            objWorksheet.Calculate
        Next
        
        DO_NOT_CALCULATE = True
        
        ThisWorkbook.Save
    Else
        DO_NOT_CALCULATE = False
    End If
End Sub

将在初始保存之后再次保存工作簿,强迫重新计算以确保您的单元格具有正确的值。

有点丑陋,但应该对您有用。

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 ...

Function LastSavedTimeStamp() As Date
    Application.Volatile
    
    LastSavedTimeStamp = Int(ActiveWorkbook.BuiltinDocumentProperties("Last Save Time"))
End 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 ...

Public DO_NOT_CALCULATE As Boolean

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    Dim objWorksheet As Worksheet

    If Not DO_NOT_CALCULATE Then
        For Each objWorksheet In ThisWorkbook.Worksheets
            objWorksheet.Calculate
        Next
        
        DO_NOT_CALCULATE = True
        
        ThisWorkbook.Save
    Else
        DO_NOT_CALCULATE = False
    End If
End Sub

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.

窗影残 2025-01-28 10:45:47

您只需要在工作簿模块的Beforesave事件子过程中调用此功能即可。

这是粘贴的代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    LastSavedTimeStamp
End Sub

You just need to put a call to this function in the BeforeSave event sub procedure of the work book module.

enter image description here

Here's the code to paste in:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    LastSavedTimeStamp
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文