Excel VBA MsgBox (Yes/No)
In VBA, using the message box we can create a yes no msgbox which is used to record user input based on the click on yes or no, the syntax to make a yes no message box is as follows variable = MsgBox(“Text”, vbQuestion + vbYesNo + vbDefaultButton2, “Message Box Title”) where variable must be declared as an integer.
Often in VBA coding, we need to collect the input values from the users to perform some tasks and one of such tasks to collect the Yes or No response from users. By using VBA MsgBox Yes No method, we can write the code to proceed further in the code.
In certain situations, we may need to present a Yes or No option in front of the user to give their response, and based on that response. We can actually run the VBA code.
For example, look at the below image of the MsgBox in VBA.
If the user says Yes, “we can write code to perform a specific task,” and if the user says “No,” we can write code to perform another set of tasks.
How to Work with MsgBox Yes/No Response?
Example #1 – Copy and Paste based on Response
For example, look at the below code.
Code:
Sub MessageBox_Yes_NO_Example1() Dim AnswerYes As String Dim AnswerNo As String AnswerYes = MsgBox("Do you Wish to Copy?", vbQuestion + vbYesNo, "User Repsonse") If AnswerYes = vbYes Then Range("A1:A2").Copy Range("C1") Else Range("A1:A2").Copy Range("E1") End If End Sub
Explanation:
The above has declared the variable as String i.e.
Dim AnswerYes As String
In the next line, we have assigned the value through a message box asking, “Do you wish to copy?”.
AnswerYes = MsgBox("Do You Wish to Copy?", vbQuestion + vbYesNo, "User Repsonse")
Now IF statement evaluates the response given through the message box. If the message box result is vbYes then it will copy the range A1 to A2 and paste in cell C1.

4.9 (1,353 ratings) 35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
If AnswerYes = vbYes Then Range("A1:A2").Copy Range("C1")
If the response given by the message box is No, then it will copy the range A1 to A2 and paste in cell E1.
Else Range("A1:A2").Copy Range("E1") End If
Ok, I have entered few values in cell A1 and A2 now.
Now I will run the code using the F5 key, or through the run option, a message box will appear in front of me and asks for my response.
If I click on Yes, it will copy the range A1 to A2 and paste in C1 cell. Now I will click on Yes and see the result.
So it has performed the task assigned if the response is YES.
Now again, I will run the code.
This time I will select No and see what happens.
Yes, it performed the task assigned in the code i.e.
Else
Range("A1:A2").Copy Range("E1")
Example #2 – Hide & Unhide Sheets Based on the Response
The below code will hide all the sheets except the active sheet if the response is yes.
Code:
Sub HideAll() Dim Answer As String Dim Ws As Worksheet Answer = MsgBox("Do you Wish to Hide All?", vbQuestion + vbYesNo, "Hide") If Answer = vbYes Then For Each Ws In ActiveWorkbook.Worksheets If Ws.Name <> ActiveSheet.Name Then Ws.Visible = xlSheetVeryHidden Next Ws ElseIf Answer = vbNo Then MsgBox "You have selected not to hide the sheets", vbInformation, "No Hide" End If End Sub
The above code will hide all the worksheets except the sheet we are in right now if the response from the message box is YES.
If the response from the message box is NO, it will display the message box saying, “You have selected not to hide the sheets.”
Similarly, the below code will unhide the sheet if the response is Yes.
Code:
Sub UnHideAll() Dim Answer As String Dim Ws As Worksheet Answer = MsgBox("Do you Wish to Unhide All?", vbQuestion + vbYesNo, "Hide") If Answer = vbYes Then For Each Ws In ActiveWorkbook.Worksheets Ws.Visible = xlSheetVeryHidden Next Ws ElseIf Answer = vbNo Then MsgBox "You have selected not to Unhide the sheets", vbInformation, "No Hide" End If End Sub
This works exactly the same as the hide sheet code; if yes, it will unhide. If no, it will not unhide.
Recommended Articles
This has been a guide to VBA Message Box. Here we discuss how to create Yes or No response in the excel VBA using MsgBox along with practical examples and a downloadable excel template. Below you can find some useful excel VBA articles –
- 35+ Courses
- 120+ Hours
- Full Lifetime Access
- Certificate of Completion