【问题标题】:Before save, prompt message box with yes/no/cancel. Yes calls Macro, No continues to save, Cancel exits sub保存前,提示消息框是/否/取消。是调用宏,否继续保存,取消退出子
【发布时间】:2019-05-18 06:51:55
【问题描述】:

我已经有一个成功的“自定义保存”宏来保存为带有日期戳。我只想让一个消息框在有人尝试手动保存时要求运行它。我基本上需要“是”来运行宏,“否”来正常保存,“取消”来退出子。

但是,每当我 file>save 或 ctrl+s 时,它只是在没有提示的情况下保存。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim answer As VbMsgBoxResult
    answer = MsgBox("Would you rather Save-As copy with date stamp?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "You are overwriting the document!")

    If answer = vbYes Then
        Call filesave
    ElseIf answer = vbNo Then
        ActiveWorkbook.Save
    Else
        Exit Sub
    End If
End Sub

【问题讨论】:

  • 你没有告诉我们你的问题是什么......也摆脱ActiveWorkbook并更明确地说明你想要保存哪个工作簿。
  • 您需要设置Cancel = True才能取消保存操作。这是子参数中的布尔取消。

标签: excel vba if-statement msgbox


【解决方案1】:

您需要将子过程的参数中的 Cancel 设置为 True 以停止当前的保存操作。

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

    Dim answer As VbMsgBoxResult
    answer = msgbox("Would you rather Save-As copy with date stamp?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "You are overwriting the document!")
    If answer = vbYes Then
        'Cancel the current standard save operation
        Cancel = True
        Call filesave
    ElseIf answer = vbNo Then
        'don't do anything; the standard save operation will proceed
    Else
        'Cancel the current standard save operation
        Cancel = True
    End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2013-01-13
    • 1970-01-01
    • 2017-08-31
    • 2015-09-06
    • 2020-10-26
    相关资源
    最近更新 更多