Smart Systems for Home

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

Excel VBA MsgBox Yes No Cancel: Step-by-Step Guide

· 4 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 VBA MsgBox with Yes, No, and Cancel buttons is created by calling MsgBox with the vbYesNoCancel constant and then testing the returned value against vbYes, vbNo, or vbCancel. The message box itself only displays the buttons; your code must branch on the result. This guide shows the exact syntax, a working example, and the mistakes that make the buttons appear but do nothing.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows, Option+F11 on Mac).
  • A workbook saved as .xlsm or .xlsb if you want to keep the macro.
  • Basic familiarity with inserting a module and running a macro.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or Option+F11 (Mac). If the Developer tab is not visible, you can still open the editor with the shortcut.
  2. Insert a standard module. In the editor, choose Insert > Module. A blank code window appears. (Menu labels can differ slightly by Excel version and platform.)
  3. Start a new subroutine. Type Sub ConfirmAction() and press Enter. The editor adds End Sub automatically.
  4. Declare a variable to hold the answer. Add Dim answer As VbMsgBoxResult on the next line. This type holds the constants vbYes, vbNo, and vbCancel.
  5. Call MsgBox with the three buttons. Write:
    answer = MsgBox("Do you want to continue?", vbYesNoCancel + vbQuestion, "Confirm")
    The first argument is the message text, the second combines the button set and icon, and the third is the title bar text.
  6. Branch on the result. Add a Select Case block:
    Select Case answer
    Case vbYes
        MsgBox "You chose Yes."
    Case vbNo
        MsgBox "You chose No."
    Case vbCancel
        MsgBox "You chose Cancel."
    End Select
  7. Run the macro. Press F5 while the cursor is inside the subroutine, or use Run > Run Sub/UserForm. The message box appears with Yes, No, and Cancel buttons.
  8. Test each button. Click Yes, then run again and click No, then Cancel. Each should show the matching follow-up message. If nothing happens, see Common Mistakes below.
  9. Replace the placeholder actions. Swap the demo MsgBox lines for your real code, such as saving a sheet, deleting a row, or exiting the subroutine. Use Exit Sub in the Cancel case if the user should abort.

Common Mistakes

  • Calling MsgBox without capturing the return value. MsgBox "Continue?", vbYesNoCancel shows the buttons but discards the answer, so no branch runs. Always assign the result to a variable.
  • Using the wrong constant. vbYesNo gives only Yes and No. vbOKCancel gives OK and Cancel. Only vbYesNoCancel gives all three.
  • Comparing to numbers instead of constants. vbYes is 6, vbNo is 7, and vbCancel is 2, but using the named constants keeps code readable and avoids errors.
  • Forgetting that the X button returns vbCancel. Closing the dialog with the close button is treated as Cancel, so handle that case.
  • Combining constants with the wrong operator. Use + or Or to combine button and icon constants, for example vbYesNoCancel + vbExclamation.
  • Putting the code in the wrong place. A standard module is correct for a general macro. Code in a worksheet module only runs from events, not from Alt+F8.

Tips

  • Keep the message text short and specific so the user knows what Yes and No will do.
  • Use vbQuestion, vbExclamation, or vbCritical to signal the severity of the choice.
  • Set a default button with vbDefaultButton1, vbDefaultButton2, or vbDefaultButton3 so Enter picks the safer option.
  • For a simple confirmation with only Yes and No, vbYesNo is less confusing than three buttons.
  • Store the result in a VbMsgBoxResult variable rather than an Integer for clarity.
  • If you need custom button labels, MsgBox cannot do it; use a small UserForm instead.

Troubleshooting

  • Compile error: Expected: expression. Check that the MsgBox call is inside a Sub and that quotes are balanced. A missing closing parenthesis is a common cause.
  • Buttons appear but nothing happens. The return value is not assigned. Change the line to answer = MsgBox(...) and add the Select Case block.
  • Macro does not appear in the list. The code may be in a sheet module or declared Private. Move it to a standard module and remove Private if present.
  • Macro security blocks it. Enable macros for the file through Excel's Trust Center, or save the workbook in a trusted location. Only enable macros in files you trust.
  • Wrong branch runs. Confirm you compare against vbYes, vbNo, and vbCancel, not vbOK or vbRetry.
  • Dialog appears behind Excel. This is rare; click the Excel taskbar icon to bring it forward.

Frequently Asked Questions

What does vbYesNoCancel return when the user closes the box?

It returns vbCancel (value 2), the same as clicking Cancel.

Can I change the button text to something other than Yes, No, and Cancel?

No. MsgBox uses fixed system labels. For custom labels, build a UserForm with your own buttons.

How do I make Enter default to No instead of Yes?

Add vbDefaultButton2 to the style argument: vbYesNoCancel + vbQuestion + vbDefaultButton2.

Does this work on Excel for Mac?

Yes, the same constants and syntax work in VBA on Mac. The Visual Basic Editor shortcut is Option+F11.

Why does my code skip the Select Case?

The variable likely holds the result of a different call, or the MsgBox line was not assigned. Check that the assignment line runs before the Select Case.

Can I use MsgBox in Excel for the web?

No. Excel for the web does not run VBA macros, so MsgBox is not available there.