1

我正在运行一个宏,Private Sub Workbook_BeforeClose(Cancel As Boolean) 但每当用户单击“取消”按钮时宏不应该运行。


我一直遵循代码行

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cancel = True Then
    MsgBox "You clicked on Cancel"
ElseIf Cancel = False Then
    Call SDA
End If

End Sub
但是,每当我按下取消按钮时,它都不会向我显示任何 MessageBox。任何帮助将不胜感激。在此处输入图像描述

4

1 回答 1

7

请试试这个:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Msg As String
Dim ireply As Integer
    If Not Me.Saved Then
        Msg = "Do you want to Save this book? "
        ireply = MsgBox(Msg, vbQuestion + vbYesNoCancel)
        Select Case ireply
            Case vbYes
                Me.Save
                Call SDA 
            Case vbNo
                Me.Saved = True
            Case vbCancel
                Cancel = True
                MsgBox "Cancelling...workbook close event!"
                Exit Sub
          End Select
    End If        
End Sub
  • 输出对话框:

在此处输入图像描述

消息框取消事件包装的通常结构如下:

'--Display MessageBox
    Dim intMsg as integer
    intMsg = MsgBox(strPrompt, vbYesNo, strTitle) 
    '--Check pressed button
    If iRet = vbNo Then
        MsgBox "NO!"
    Else
        MsgBox "Yes!"
    End If
于 2013-01-18T12:13:39.930 回答