Excel VBA: Confirmation on Pressing CommandButton

后端 未结 2 974
自闭症患者
自闭症患者 2021-01-12 06:33

Upon pressing my CommandButton, I would like to have a pop-up that asks \"These changes cannot be undone. It is advised to save a copy before proceeding. Do you wish to proc

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 06:58

    You can use a message box, but that is somewhat limited. You can rephrase the question slightly to use the vbYesNoCancel buttons, since Save As is not an optional button on Message Box.

    Then you can work with the result of the message box button-click:

    Dim mbResult as Integer
    mbResult = MsgBox("These changes cannot be undone. Would you like to save a copy before proceeding?", _
     vbYesNoCancel)
    
    Select Case mbResult
        Case vbYes
            'Modify as needed, this is a simple example with no error handling:
            With ActiveWorkbook
                If Not .Saved Then .SaveAs Application.GetSaveAsFilename()
            End With
        Case vbNo
            ' Do nothing and allow the macro to run
    
        Case vbCancel
            ' Do NOT allow the macro to run
            Exit Sub
    
    End Select
    

提交回复
热议问题