VBA MsgBox (Yes/No)

Publication Date :

Blog Author :

Download FREE VBA MsgBox (Yes/No) Excel Template and Follow Along!
VBA Message Box Yes or No Excel Template.xlsm

Table Of Contents

arrow

Excel VBA MsgBox (Yes/No)

In VBA, using the message box, we can create a yes no msgbox, used to record user input based on clicking 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 one must declare variable as an integer.

In VBA coding, we often need to collect the input values from the users to perform some tasks. One of such tasks is collecting the users' yes or no responses. 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 based on them. Then, we can run the VBA code.

For example, look at the below image of the MsgBox in VBA.

VBA Message Box Continue.png

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.

🤖 Supercharge Excel Skills with the ChatGPT & AI for Microsoft Excel Course

Ready to take Excel to the next level? ChatGPT & AI for Microsoft Excel — teaches you how to harness the power of ChatGPT and AI tools to automate tasks, build smart formulas, and streamline data analysis in Excel—perfect for analysts, managers, and anyone working with data. Includes hands-on demos and real-world examples to boost your productivity.

Learn More →

How to Work with MsgBox Yes/No Response?

Example #1 - Copy and Paste based on Response

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 SubVBA Message box Example 1

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, the IF statement evaluates the response given through the message box. For example, if the message box result is vbYes, it will copy the range A1 to A2 and paste it into cell C1.

   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 it into cell E1.

Else Range("A1:A2").Copy Range("E1") End If

We have entered a few values in cells A1 and A2 now.

VBA Message box Example 1-1

Now, we will run the code using the F5 key, or through the run option, a message box will appear in front of us and ask for our response.

VBA Message box Example 1-2

If we click "Yes," it will copy the range A1 to A2 and paste it into the C1 cell. Now, we will click on "Yes" and see the result.

VBA Message box Example 1-3

So, it has performed the task assigned if the response is YES.

Now again, we will run the code.

yes or no Example 1-4

This time we will select No and see what happens.

yes or no Example 1-5

Yes, it performed the task assigned in the code i.e.

Else Range("A1:A2").Copy Range("E1")

ChatGPT & AI For Microsoft Excel Course
Learn to automate workflows, enhance data analysis, and create intelligent Excel models using ChatGPT and AI tools. Gain hands-on skills in Excel and prompt engineering, earn a recognized certification, and boost efficiency across reporting, modeling, and decision-making—perfect for analysts, finance teams, and data-driven professionals.
Learn More →

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.

yes or no Example 2

If the response from the message box is NO, it will display the message box saying, â€śYou have selected not to hide the sheets.”

yes or no Example 1-6

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

It works the same as the hide sheet code. If yes, it will unhide. If not, it will not unhide.

CHAT GPT an AI