保存之前,请以是/否/取消提示信息框。是,调用宏,否继续保存,取消退出子

时间:2018-12-17 21:17:54

标签: excel vba excel-vba if-statement msgbox

我已经成功地将“自定义保存”宏另存为带日期戳。我只想有一个消息框,要求有人尝试手动保存时运行它。我本质上需要“是”才能运行宏,“否”可以正常保存,而“取消”可以退出子程序。

但是,每当我归档> 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

1 个答案:

答案 0 :(得分:1)

您需要将子过程的参数中的“取消”设置为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