Table of Contents
A message box in VBA can be created by using the “MsgBox” function. This function allows the programmer to display a pop-up message on the screen and prompt the user for a response. To specifically ask for a yes or no response, the function can be modified by including the “vbYesNo” parameter. This will display two buttons, “Yes” and “No”, for the user to choose from. The response can then be captured and used in the program for further actions. By using the “MsgBox” function with the “vbYesNo” parameter, a simple and effective way to create a message box that asks for a yes or no response can be achieved in VBA.
VBA: Create Message Box with Yes/No Responses
You can use the following syntax in VBA to create a message box that allows a user to select Yes or No:
Sub MsgBoxYesNo()
'ask user if they want to multiply two cells
UserResponse = MsgBox("Do you want to multiply cells A1 and B1?", vbYesNo)
'perform action based on user responseIf UserResponse = vbYes Then
Range("C1") = Range("A1") * Range("B1")
Else
MsgBox "No Multiplication was Performed"
End IfEnd Sub
This particular macro creates a message box that asks the user if they want to multiply cells A1 and B1.
If the user clicks “Yes” then the two cells are multiplied and the result is shown in cell C1.
If the user clicks “No” then a new message box appears that tells the them no multiplication was performed.
Note that the statement vbYesNo is what inserts “Yes” and “No” buttons for the user to click.
The following examples shows how to use this syntax in practice.
Example: Create Message Box with Yes/No Responses
Suppose we have the following two values in cells A1 and B1 in our Excel sheet:

Suppose we would like to create a macro that shows a message box to the user and asks them whether they’d like to multiply the values in cells A1 and B1 or not.
We can create the following macro to do so:
Sub MsgBoxYesNo()
'ask user if they want to multiply two cells
UserResponse = MsgBox("Do you want to multiply cells A1 and B1?", vbYesNo)
'perform action based on user responseIf UserResponse = vbYes Then
Range("C1") = Range("A1") * Range("B1")
Else
MsgBox "No Multiplication was Performed"
End IfEnd SubWhen we run this macro, the following message box appears:

If we click Yes, then the macro will multiply the values in cells A1 and B2 and display the result in cell C1:


The message box tells us that no multiplication was performed since we clicked No in the previous message box.
Cite this article
stats writer (2024). How would you create a message box in VBA that asks for a yes or no response?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-would-you-create-a-message-box-in-vba-that-asks-for-a-yes-or-no-response/
stats writer. "How would you create a message box in VBA that asks for a yes or no response?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-would-you-create-a-message-box-in-vba-that-asks-for-a-yes-or-no-response/.
stats writer. "How would you create a message box in VBA that asks for a yes or no response?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-would-you-create-a-message-box-in-vba-that-asks-for-a-yes-or-no-response/.
stats writer (2024) 'How would you create a message box in VBA that asks for a yes or no response?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-would-you-create-a-message-box-in-vba-that-asks-for-a-yes-or-no-response/.
[1] stats writer, "How would you create a message box in VBA that asks for a yes or no response?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How would you create a message box in VBA that asks for a yes or no response?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
