Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA MsgBox Yes No: A Practical Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

A Yes/No message box in Excel VBA is created with the MsgBox function using the vbYesNo or vbYesNoCancel button constant, and you read the user's choice by comparing the return value to vbYes or vbNo. The shortest working pattern is: If MsgBox("Continue?", vbYesNo) = vbYes Then followed by the action you want, with an Else branch for No. This guide shows the exact syntax, where to put the code, how to handle Cancel, and the mistakes that make the box behave unexpectedly.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Labels can differ slightly by Excel version and region.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep your code.
  • Basic familiarity with the VBA Editor (Alt+F11 on Windows, Option+F11 on Mac).

Step-by-Step

  1. Open the VBA Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). If nothing happens, enable the Developer tab first and click Visual Basic there.
  2. Insert a module. In the Project Explorer, right-click your workbook name, choose Insert > Module. Code in a standard module can be run from the Macros dialog; code in a sheet module only runs from events on that sheet.
  3. Write the basic Yes/No prompt. Type this into the module:
    Sub AskToContinue()
        Dim answer As VbMsgBoxResult
        answer = MsgBox("Do you want to continue?", vbYesNo, "Confirm")
        If answer = vbYes Then
            MsgBox "You chose Yes."
        Else
            MsgBox "You chose No."
        End If
    End Sub
    The second argument vbYesNo supplies the Yes and No buttons. The third argument is the title bar text.
  4. Run it. Place the cursor inside the subroutine and press F5, or go back to Excel and use Developer > Macros, select AskToContinue, and click Run. You should see a box with Yes and No.
  5. Capture the result correctly. MsgBox returns a number, not text. Compare against the built-in constants vbYes (6) and vbNo (7). Never compare to the strings "Yes" or "No" — that test always fails because the return value is numeric.
  6. Add Cancel if the user needs an escape route. Use vbYesNoCancel instead. It returns vbCancel (2) when the user closes the box with the X or clicks Cancel. Handle it explicitly:
    Select Case MsgBox("Delete the row?", vbYesNoCancel + vbQuestion, "Confirm")
        Case vbYes
            ActiveCell.EntireRow.Delete
        Case vbNo
            ' do nothing, keep going
        Case vbCancel
            Exit Sub
    End Select
  7. Choose an icon and default button. Combine constants with a plus sign: vbYesNo + vbQuestion shows a question mark, vbYesNo + vbExclamation a warning. Add vbDefaultButton2 to make No the default so a stray Enter does not confirm a destructive action. The full set is vbYesNo + vbQuestion + vbDefaultButton2.
  8. Test the No path. Run the macro and click No. Confirm that nothing destructive happened and that your Else branch behaves as intended. Then test Cancel if you used vbYesNoCancel.

Common Mistakes

  • Comparing to text. If MsgBox("Go?", vbYesNo) = "Yes" never evaluates true. Use = vbYes.
  • Forgetting the parentheses when assigning. answer = MsgBox("Go?", vbYesNo) needs brackets. Calling MsgBox as a statement without brackets is fine only when you ignore the return value.
  • Using vbOKCancel by habit. That gives OK and Cancel, not Yes and No. The constants are not interchangeable.
  • Ignoring Cancel. With vbYesNoCancel, treating anything that is not Yes as No can silently skip work the user expected to happen. Branch on all three values.
  • Putting the code in the wrong place. A macro in a worksheet module will not appear in the Developer > Macros list unless it is Public, and it will not run from a button on another sheet.
  • Assuming the box blocks everything. MsgBox is modal to Excel, but it does not stop other applications, and it does not pause a running loop unless the call is inside that loop.

Tips

  • Keep prompts short and specific: "Delete row 12?" beats "Are you sure?".
  • Use vbDefaultButton2 for anything that deletes, overwrites, or sends data.
  • Store the result in a variable typed as VbMsgBoxResult so the editor offers the constants as you type.
  • If you need a text input as well as a Yes/No, MsgBox cannot do it; use InputBox for the text and a separate MsgBox to confirm.
  • For repeated prompts inside a loop, add an option to stop asking, or the user will click Yes dozens of times.
  • Name your macro descriptively in the Sub line so the Macros dialog is readable.

Troubleshooting

The macro does not appear in the Macros list. Check that the code is in a standard module, that the Sub is not marked Private, and that macros are enabled for the workbook. If the file came from elsewhere, Excel may block macros until you allow them in the Trust Center.

Nothing happens when you press F5. The cursor must be inside the subroutine. If the editor asks you to choose a macro, pick the one you want to run.

The box appears but the code does not react to the click. You are probably comparing to text or to the wrong constant. Print the value with Debug.Print answer and check the Immediate window: Yes returns 6, No 7, Cancel 2.

The prompt shows the wrong buttons. Recheck the constant name. vbYesNoCancel and vbYesNo are different, and adding an icon constant with a comma instead of a plus breaks the expression.

Your workbook closes without saving after a prompt. A Yes/No box does not save anything by itself. Add ThisWorkbook.Save in the Yes branch if saving is the intended action.

The code runs twice. You may have the macro attached to a button and also triggered by a worksheet event. Check the sheet module for event procedures such as Worksheet_Change.

Frequently Asked Questions

What number does Yes return in a VBA MsgBox?

Yes returns 6, which is the constant vbYes. No returns 7 (vbNo), Cancel returns 2 (vbCancel), and OK returns 1 (vbOK).

Can I change the button text from Yes and No to something custom?

Not with MsgBox. The built-in buttons are fixed. For custom labels you would need a small user form with your own buttons, which is more work but gives full control.

How do I make No the default button?

Add vbDefaultButton2 to the button argument, for example MsgBox "Delete?", vbYesNo + vbDefaultButton2. Pressing Enter then chooses No.

Why does my MsgBox return a value I did not expect when the user clicks the X?

Closing the box with the X returns the same value as Cancel when Cancel is available. With vbYesNo only, the X returns vbNo. Handle Cancel explicitly if the distinction matters.

Does MsgBox work on Excel for Mac?

Yes, the MsgBox function and the vbYesNo constants work in Excel for Mac. The VBA Editor shortcut differs (Option+F11), and some older dialog behaviours vary, but the return values are the same.

Can I use MsgBox to get typed input?

No. MsgBox only returns which button was pressed. Use InputBox to collect text or a number, then confirm it with a Yes/No MsgBox if needed.