VBA – Yes No Message Box (Msgbox)
Yes No Message Box in VBA
Here is code to present the user with a Yes/No Message box and have the ability to run code based on the response.
It works by setting a variable equal to the message box response and evaluating the variable with an IF…THEN statement.
The code is commented in locations for changing the message text displayed, where to place code for a Yes response, and No response.
To retrieve a user inputted string, see my post about the InputBox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Sub YesNoMessageBox() Dim Answer As String Dim MyNote As String 'Place your text here MyNote = "Do you agree?" 'Display MessageBox Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???") If Answer = vbNo Then 'Code for No button Press MsgBox "You pressed NO!" Else 'Code for Yes button Press MsgBox "You pressed Yes!" End If End Sub |
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Did you find this VBA tutorial useful? Then share it with your friends and colleagues: