Excel 保存前,在消息框中提示是/否/取消。是调用宏,否继续保存,取消退出子项

Excel 保存前,在消息框中提示是/否/取消。是调用宏,否继续保存,取消退出子项,excel,vba,if-statement,msgbox,Excel,Vba,If Statement,Msgbox,我已经有一个成功的“自定义保存”宏保存为带有日期戳。我只想让一个消息框在有人试图手动保存时运行它。我基本上需要“是”来运行宏,“否”来正常保存,以及“取消”来退出sub 但是,每当我选择file>save或ctrl+s时,它都会在不提示的情况下进行保存 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim answer As VbMsgBoxResult answer =

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

但是,每当我选择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

为了停止当前的保存操作,您需要将取消子过程的参数设置为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

为了停止当前的保存操作,您需要将取消子过程的参数设置为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

你没有告诉我们你的问题是什么。。。此外,还可以删除
ActiveWorkbook
,并更明确地显示要保存到哪个工作簿。您需要设置
Cancel=True
,以取消保存操作。这是sub参数中的布尔取消。您没有告诉我们您的问题是什么。。。此外,还可以删除
ActiveWorkbook
,并更明确地显示要保存到哪个工作簿。您需要设置
Cancel=True
,以取消保存操作。这是sub参数中的布尔取消。