Excel VBA MsgBox Response Values Explained
In this article
MsgBox returns a number that tells you which button the user clicked, so you can branch your code on that value. The return value is a member of the VbMsgBoxResult enumeration: vbOK (1), vbCancel (2), vbAbort (3), vbRetry (4), vbIgnore (5), vbYes (6), and vbNo (7). If you only display a message with no buttons (the default), the function returns vbOK (1) when the user clicks OK or presses Enter. To capture the response, assign the result to a variable: Dim answer As VbMsgBoxResult then answer = MsgBox("Continue?", vbYesNo + vbQuestion, "Confirm"). The key practical rule is that the return value reflects only the buttons you actually included in the style argument; a button that is not shown cannot be returned. This guide walks through the syntax, the style constants that control which buttons appear, how to test the result reliably, and the mistakes that cause a macro to behave as if the user clicked something they did not.
What You'll Need
- Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A macro-enabled workbook (.xlsm) if you want to save the code.
- Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and how to insert a module.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook, choose Insert > Module. Labels in the editor can differ slightly by Excel version and platform, but Insert > Module is consistent.
- Write a minimal MsgBox call. In the module, type
Sub TestMsg()on one line, thenMsgBox "Hello", thenEnd Sub. Place the cursor inside the procedure and press F5. A message box with a single OK button appears. This confirms the editor and macro security settings are working before you add response logic. - Declare a variable to hold the result. Change the procedure to
Dim ans As VbMsgBoxResultfollowed byans = MsgBox("Proceed?", vbYesNo + vbQuestion, "Confirm"). Using the VbMsgBoxResult type (rather than Integer) makes the code self-documenting and lets you compare against named constants. - Choose the button set with the style argument. The second argument controls which buttons appear: vbOKOnly (0), vbOKCancel (1), vbAbortRetryIgnore (2), vbYesNoCancel (3), vbYesNo (4), vbRetryCancel (5). Add an icon constant such as vbQuestion (32), vbExclamation (48), or vbInformation (64), and add vbDefaultButton2 (256) if you want the second button to be the default. Combine them with
+orOr. - Test the result with a Select Case block. After the MsgBox line, add
Select Case ans, thenCase vbYeswith the code you want to run,Case vbNowith the alternative, andEnd Select. Select Case is clearer than nested If statements when you have three or more possible responses. - Handle the cancel path explicitly. If you use vbYesNoCancel, add a
Case vbCancelbranch that exits the procedure cleanly, for exampleExit Sub. Without it, a Cancel click falls through and the macro may continue as if the user agreed. - Show the numeric value while debugging. Temporarily add
Debug.Print ansafter the MsgBox line and open the Immediate window (Ctrl+G on Windows, Control+G on Mac) to see the number returned. This is the fastest way to confirm which constant corresponds to the button the user pressed. - Run and verify each button. Press F5 and click every button in turn, checking that the correct branch executes. If a branch never runs, the button is probably not included in your style constant, so the value can never be returned.
- Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A standard .xlsx file discards the module when closed.
Common Mistakes
Comparing against the wrong constant. vbYes is 6 and vbNo is 7; swapping them silently reverses the logic. Always use the named constants rather than hard-coded numbers.
Expecting a button that was never shown. If the style is vbOKOnly, the function can only return vbOK. Code that tests for vbNo will never fire.
Ignoring the return value entirely. Calling MsgBox "Delete?" without assigning the result displays the prompt but gives the user no way to stop the macro. Assign the result and branch on it.
Using parentheses incorrectly. When you call MsgBox as a statement without capturing the result, do not wrap the arguments in parentheses: MsgBox "Hi", vbInformation is correct. Parentheses are required only when you assign the return value to a variable or use it in an expression.
Forgetting the Cancel branch. With vbYesNoCancel, a Cancel click returns vbCancel (2). If there is no Case for it, execution continues into whatever follows the Select Case block.
Confusing the button constants with the return constants. The style value vbYesNo (4) is not the same as the return value vbYes (6). One controls what is displayed; the other reports what was clicked.
Tips
Use vbDefaultButton2 or vbDefaultButton3 to make the safer choice the default, which reduces accidental confirmation of destructive actions. Keep prompts short and put the question in the title bar so the message body stays readable. For long procedures, wrap the MsgBox in a small helper function that returns a Boolean, for example Function ConfirmAction(msg As String) As Boolean that returns True only for vbYes; this keeps the main code free of Select Case blocks. Add vbInformation or vbExclamation icons sparingly, since too many icons train users to click through without reading. On Mac, the button order in the dialog may differ from Windows even though the returned constants are the same, so test on both platforms if the workbook is shared.
Troubleshooting
The macro does nothing when a button is clicked. Check that the variable is declared and assigned, and that the Select Case uses the same constant names. Add Debug.Print to see the actual value.
Compile error on the MsgBox line. Look for a missing comma between arguments or an unclosed quote. The prompt string must be first, followed by the style and title arguments separated by commas.
The message box does not appear at all. Macro security may be blocking the code. Check File > Options > Trust Center > Macro Settings, and confirm the workbook is in a trusted location. On Mac, check Excel > Preferences > Security.
The wrong branch runs. Print the returned value and compare it with the constant list. A common cause is combining style constants with + when one of them is a return constant by mistake.
The code works in one workbook but not another. The module may be in a different project, or the second workbook may be .xlsx. Save as .xlsm and confirm the procedure is in a standard module, not a sheet module, unless it is intentionally event-driven.
Frequently Asked Questions
What number does MsgBox return for Yes?
vbYes, which equals 6. No is 7, Cancel is 2, and OK is 1.
Can MsgBox return more than one value at once?
No. It returns a single value representing the one button the user clicked. To collect multiple answers, use separate prompts or a custom UserForm.
What happens if the user closes the box with the X button?
Closing with the X is treated as Cancel. If your style does not include a Cancel button, the result depends on the buttons shown; test this path if the action is destructive.
Do the return values differ between Windows and Mac?
No. The VbMsgBoxResult constants are the same on both platforms, although the visual button order can differ.
Why does my code compare to vbOK when I used vbYesNo?
vbYesNo shows Yes and No, so the return is vbYes or vbNo. vbOK is only returned when an OK button is present, such as with vbOKOnly or vbOKCancel.
Can I change the button captions?
Not with the built-in MsgBox function. Custom captions require a UserForm with your own command buttons.