2013-01-18 104 views
1

我正在運行一個宏,而在Private Sub Workbook_BeforeClose(Cancel As Boolean) 但每當用戶點擊取消按鈕宏不應該運行。VBA Excel-如何使用MS Excel中的取消按鈕關閉?


我不停的代碼

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

末次
以下行但每當我按取消按鈕,它沒有顯示我任何MessageBox.Any幫助,將不勝感激。 enter image description here

+1

'取消'參數不說天氣取消被按下。它可以讓你的代碼覆蓋正在發生的關閉事件,將其設置爲true將停止關閉工作簿,離開它或將其設置爲false將允許關閉操作完成。 – NickSlash

+0

@NickSlash,有沒有辦法找到'Cancel'是否被按下? thaks的回覆:-) –

回答

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 
  • 輸出對話框:

enter image description here

一個消息的通常結構取消事件渦卷如下:

'--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 
+1

@Saikiran請看看第一個。它實際上是'覆蓋'通常的'Close_Before事件' – bonCodigo

+1

@ bonCondigo,嗨感謝您的迴應,但它對我無效.'let我用另一種方式告訴我我的需求:我只想顯示MessageBox當用戶點擊CANCEL按鈕時。 –

+0

@SaiKiranMandhala它似乎適合我,當我點擊取消按鈕..讓我更新屏幕.. :) – bonCodigo