How to Create Message Box with Yes/No Responses in VBA

Creating a message box with Yes/No responses in Visual Basic for Applications (VBA) involves using the MsgBox function with specific parameters to create a button that, when clicked, will return a value that can be used in a program’s logic. The specific parameters used in the MsgBox function will determine the text of the message box, the type of buttons displayed, and the value returned when a button is clicked. By understanding how to use the MsgBox function, users can create message boxes with Yes/No responses that can be used to control the flow of a VBA program.


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 response
    If UserResponse = vbYes Then
      Range("C1") = Range("A1") * Range("B1")
    Else
      MsgBox "No Multiplication was Performed"
    End If

End 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 response
    If UserResponse = vbYes Then
      Range("C1") = Range("A1") * Range("B1")
    Else
      MsgBox "No Multiplication was Performed"
    End If

End Sub

When we run this macro, the following message box appears:

VBA msgbox yes or no

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.

x