gpt4 book ai didi

excel - 如何在 excel VBA 中仅在 "Save-as"而不是在正常的 "Save"上运行宏

转载 作者:行者123 更新时间:2023-12-04 22:12:44 25 4
gpt4 key购买 nike

我有一份工作表格,我每天都会更新并通过电子邮件发送出去。我从一个主表格(“Passdown Report 3rd Shift”)打开,并在每天离开之前另存为按日期分隔的单独副本(“Passdown Report 3rd Shift 2022-01-19”)。
表格中填充了基于日期和工作簿中其他工作表的刺激自动更新的公式,因此我添加了一个宏来将范围内的所有公式转换为其值。
我想在将表单保存为每日文件之前运行此宏,但不是在我只是更新和保存主文件时。那是我能做的吗?

最佳答案

在关闭工作簿之前运行宏

  • 预计您SaveAs作为备份。
  • 只有在关闭备份时,IF(StrComp...将记录一个不同的文件名,您的宏将运行,备份将在关闭前再次保存。
  • 这有点笨拙,但它应该确保您原件的安全。
  • BeforeSave 的问题是你可以做If SaveAsUI = True Then但你也可能不小心做了SaveAs在原件上并将其“销毁”。我认为这太冒险了。

  • Option Explicit

    Private ClosingBackup As Boolean

    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Const sFileName As String = "Passdown Report 3rd Shift.xlsm"

    If Not ClosingBackup Then
    With Me
    ' Compare if you're closing the original or the backup.
    If StrComp(.Name, sFileName, vbTextCompare) <> 0 Then ' backup
    MyMacro
    ClosingBackup = True
    ' Here it will again call this sub but will exit because
    ' 'ClosingBackup' is set to True ('If Not ClosingBakup Then').
    .Close SaveChanges:=True
    'Else ' ClosingBackup = False i.e. closing the original; do nothing
    End If
    End With
    'Else ' ClosingBackup = True; closing the backup which was saved; do nothing
    End If

    End Sub

    关于excel - 如何在 excel VBA 中仅在 "Save-as"而不是在正常的 "Save"上运行宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71747005/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com