Add a Confirmation Message Box to a macro?

n2lectual

Board Regular
Joined
Mar 18, 2008
Messages
80
I have a macro that needs a confirmation before running with a YES and NO button.

User clicks macro button, Message box ask "ARE YOU SURE?"
If yes macro runs, if no message box and macro exits.

What code do I need to insert and where within the macro?
Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Code:
response = MsgBox("Are You Sure?",vbyesno)
 
If response = vbno then 
    Msgbox("Macro Ending")
    exit sub
End If
 
rest of code here.
 
Upvote 0
Does this not work for showing userforms from button click events?
I have a button on a worksheet and I want to provide a confirmation MsgBox to ensure that the user wishes to load data to that specific worksheet. However, whether I press YES or NO, the userform still shows. Can someone clarify this for me?

Thanks.
Joe.

Code:
Private Sub CommandButton1_Click()
 
    Dim Ans As Boolean
 
    Ans = MsgBox("Do you wish to add a new patient to the " & _
        ActiveSheet.Name & " list?", vbYesNo, "Add a new patient?")
        
    If Ans = vbNo Then Exit Sub
    
    frmNewPat.Show False
    
End Sub
 
Upvote 0
Sorry for my rather terse response above but I was about to dash off.

You can also use

Code:
Dim ans As VbMsgBoxResult

which may make the code more understandable when you revisit it sometime in the future. But declaring ans as an Integer works just as well.
 
Upvote 0
Sorry for my rather terse response above but I was about to dash off.

You can also use

Code:
Dim ans As VbMsgBoxResult

which may make the code more understandable when you revisit it sometime in the future. But declaring ans as an Integer works just as well.

That is valuable information! Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top