VBA MsgBox Yes/No: How to Get and Handle the Answer

VBA & macros · Updated November 18, 2026

To show Yes and No buttons in VBA and read the user’s choice, use vbYesNo and assign the result of MsgBox to a variable. Compare that result with vbYes or vbNo to decide what the macro should do next.

What a Yes/No MsgBox returns

MsgBox is a VBA function: it displays a dialog and returns a value representing the button the user clicked. With the Yes/No button set, the return value is either vbYes or vbNo. These are named VBA constants, so use them in your code rather than relying on their underlying numbers.

The distinction matters because vbYesNo configures which buttons appear; it is not the answer itself. The returned value tells the macro which one the person chose. For example, assigning the function’s result to a variable lets you use an If...Then...Else statement to branch to separate actions.

A basic prompt does not require a title or an icon. The first argument is the text to display, and the second argument specifies the buttons. In Excel, a macro can use this return value to confirm a decision before changing cells, saving data, or continuing a process.

Basic syntax and a working example

The general syntax is MsgBox(prompt, buttons, title), with optional arguments for a help file and help context. The full VBA syntax also uses square brackets to show optional arguments in documentation; do not type those brackets into a call. This example asks whether to continue and reports the answer:

Sub AskYesOrNo()
    Dim answer As VbMsgBoxResult

    ' Show Yes and No buttons, with a question icon and a title.
    answer = MsgBox("Do you want to continue?", _
                    vbYesNo Or vbQuestion, "Confirm action")

    If answer = vbYes Then
        MsgBox "You chose Yes."
    ElseIf answer = vbNo Then
        MsgBox "You chose No."
    End If
End Sub

VbMsgBoxResult is the appropriate data type for a message box result. In this example, the call pauses while the dialog is open. After a button is clicked, execution resumes and the comparison determines which branch runs. The second message boxes are only demonstrations; in a real macro, replace them with the actions that suit each choice.

Before running a macro that changes a workbook, save a copy first. VBA macros cannot be undone with Ctrl+Z, so a confirmation prompt is not a substitute for a backup.

Choose the buttons, icon, and default

The second argument combines options using Or. The button group is the important choice for collecting a Yes/No answer. You can also add an icon or set which button is selected by default when the dialog opens.

  • vbYesNo displays Yes and No buttons.
  • vbQuestion displays a question-mark icon. It changes the appearance, not the answer values.
  • vbDefaultButton1 selects the first button as the default; vbDefaultButton2 selects the second.
  • vbYesNoCancel displays Yes, No, and Cancel, and therefore supports a separate Cancel result.

For a potentially destructive action, make No the default so pressing Enter does not immediately confirm the change. With Yes first and No second in a Yes/No dialog, vbDefaultButton2 selects No. The following example asks before clearing a range; the deletion happens only after an explicit Yes:

Sub ConfirmClearContents()
    Dim answer As VbMsgBoxResult

    ' Make No the default before a destructive action.
    answer = MsgBox("Clear the contents of A2:D20?", _
                    vbYesNo Or vbQuestion Or vbDefaultButton2, _
                    "Confirm clear")

    If answer = vbYes Then
        ThisWorkbook.Worksheets("Sheet1").Range("A2:D20").ClearContents
    Else
        MsgBox "Nothing was cleared.", vbInformation, "No changes made"
    End If
End Sub

ClearContents removes cell contents, including formulas, but leaves formatting in place. Change the worksheet name and range to match the workbook, and verify that the target is correct before running the macro.

Use the answer to control macro flow

A prompt is most useful when the answer governs a meaningful next step. Put the action that should happen on Yes inside the Yes branch, and use the No branch to exit, skip the action, or continue with a safe alternative. This keeps the decision and the action close together and makes the code easier to review.

For example, a workbook might ask whether to create a report. If Yes is selected, the macro can call a separate report procedure; if No is selected, it can leave the workbook unchanged. A direct call to another procedure is often clearer than repeating a long block of code inside the message-box routine.

Sub AskToBuildReport()
    Dim answer As VbMsgBoxResult

    ' Ask before calling the procedure that builds the report.
    answer = MsgBox("Create the monthly report now?", _
                    vbYesNo Or vbQuestion Or vbDefaultButton2, _
                    "Create report")

    If answer = vbYes Then
        BuildMonthlyReport
    ElseIf answer = vbNo Then
        Exit Sub
    End If
End Sub

This example assumes that a separate procedure named BuildMonthlyReport exists in the project. If it does not, VBA will report that the sub or function is not defined. A prompt does not create or validate the procedure it calls.

Yes/No versus Yes/No/Cancel

Choose vbYesNo when there are exactly two meaningful choices. A user who declines is choosing No; there is no separate Cancel button or third return value in that button group. This makes Yes/No suitable for a simple confirmation such as whether to proceed or skip an optional step.

Use vbYesNoCancel when stopping or postponing is meaningfully different from answering No. For instance, in a multi-step import, Yes might import the file, No might skip this file and continue, and Cancel might stop the whole import. Test all three possible results so the macro does not accidentally treat Cancel as permission to proceed.

Sub AskThreeChoices()
    Dim answer As VbMsgBoxResult

    ' Use a three-button group when Cancel has its own meaning.
    answer = MsgBox("Import this file?", _
                    vbYesNoCancel Or vbQuestion Or vbDefaultButton2, _
                    "Import choice")

    Select Case answer
        Case vbYes
            MsgBox "Import the file here."
        Case vbNo
            MsgBox "Skip this file here."
        Case vbCancel
            MsgBox "Stop the import here."
    End Select
End Sub

The example uses explanatory messages where application-specific import actions would go. A Yes/No dialog cannot distinguish a deliberate No from a separate request to cancel. If that distinction matters, add the Cancel button rather than trying to infer an extra answer from the two-button dialog.

Common mistakes and how to fix them

  • Comparing the answer with the wrong constant: compare a Yes/No result with vbYes and vbNo. Constants such as vbYesNo specify the button group; they are not substitutes for the user’s choice.
  • Forgetting to capture the return value: MsgBox can display a prompt without assigning its result, but the macro then has no variable to inspect. Use an assignment when the answer must affect what happens next.
  • Putting the button option in the wrong argument: the prompt text comes first and the button options come second. Passing vbYesNo as the prompt does not create Yes and No buttons.
  • Using parentheses incorrectly: when assigning a function result, use parentheses around the arguments, as in answer = MsgBox("Continue?", vbYesNo). When calling it without using its return value, write MsgBox "Message", vbInformation without wrapping the arguments in parentheses.
  • Assuming Yes/No offers a Cancel result: it does not. Choose vbYesNoCancel if the code needs to handle Cancel separately.
  • Using a Yes default for a risky action: consider setting vbDefaultButton2 when No is the second button. A safer default reduces the chance that an accidental Enter key confirms a consequential action.

Also keep the question specific. “Continue?” may be unclear if the user has not been told what continuing will do. A prompt such as “Delete the imported rows from Sheet1?” gives a more useful decision, especially when paired with a cautious default.

Where to put and run the VBA code

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste a procedure into the module. If the Developer tab is visible, Developer > Visual Basic also opens the editor. The Developer tab may need to be enabled in Excel options. Run a macro from the editor with the cursor in its procedure and press F5, or return to Excel and run it through the macro controls.

On Excel for Mac, open the editor from Developer > Visual Basic or use Option+F11; depending on the keyboard and system settings, you may also need to hold Fn. Menu names and keyboard behavior can differ slightly by Excel and macOS version. The VBA examples here use standard VBA syntax, though a workbook’s references, worksheet names, and available features can still affect whether a macro runs as intended.

Save a workbook containing macros in a macro-enabled format such as .xlsm. If Excel’s security settings block macros, use only workbooks and code you trust, and follow your organization’s security policies. A MsgBox prompt is not a security mechanism and cannot replace appropriate access controls or careful review of macro code.

Frequently Asked Questions

How do I get the Yes or No answer from MsgBox?
Assign the result to a variable, such as answer = MsgBox("Continue?", vbYesNo), then compare it with vbYes or vbNo. Declaring the variable as VbMsgBoxResult makes its purpose clear.

What value does vbYes return?
vbYes is the named return constant for clicking Yes, and vbNo is the named return constant for clicking No. Use these constants in comparisons rather than hard-coding their numeric values.

Can I add a Cancel button to a Yes/No MsgBox?
Not while using only the vbYesNo button group. Use vbYesNoCancel and add a separate Case vbCancel or equivalent branch if Cancel must have its own behavior.

How do I make No the default button?
Add vbDefaultButton2 to the buttons argument. In a Yes/No dialog, Yes is the first button and No is the second, so the second button becomes the default.

Can MsgBox Yes/No return text such as Yes or No?
No. The function returns a VbMsgBoxResult value, not the displayed button label as a string. Compare that result with vbYes or vbNo; convert it to text yourself only if another part of your code needs text.

Can a MsgBox choice be undone with Ctrl+Z?
No. Ctrl+Z does not undo a VBA macro’s changes. Save a separate copy before running code that edits or deletes data, and make the prompt and default button appropriate to the consequences.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.