VBA MsgBox Result: Return Values and Button Choices

VBA & macros · Updated November 18, 2026

In VBA, MsgBox returns a VbMsgBoxResult value that identifies which response button the user selected. Store that return value and compare it with constants such as vbYes, vbNo, or vbCancel to make a macro respond to the choice.

What the MsgBox result represents

A message box is more than a way to display information: when it offers response buttons, it can pause a macro and return the user’s selection. The result is an enumeration named VbMsgBoxResult. Its members have numeric values, but VBA code is easier to read and maintain when it uses the named constants rather than unexplained numbers.

The most commonly encountered result constants are:

  • vbOK = 1
  • vbCancel = 2
  • vbAbort = 3
  • vbRetry = 4
  • vbIgnore = 5
  • vbYes = 6
  • vbNo = 7

These values describe the button clicked; they do not describe the set of buttons displayed. That distinction is a frequent source of confusion. For instance, vbYesNo requests a dialog with Yes and No buttons, while vbYes is the result to test when the user chooses Yes. The result is not the button-layout constant.

How the buttons and return values fit together

The second argument of MsgBox controls which buttons appear, along with optional icons and other display settings. Its button-layout constants include vbOKOnly, vbOKCancel, vbAbortRetryIgnore, vbYesNoCancel, vbYesNo, and vbRetryCancel. After the user responds, the function returns the constant for the particular button selected.

For example, a vbYesNoCancel dialog can return vbYes, vbNo, or vbCancel. A vbOKCancel dialog can return vbOK or vbCancel. Do not test for a result that the dialog does not offer: a Yes/No prompt cannot provide a distinct Cancel choice. If declining and cancelling mean different things to the macro, include all three buttons.

Icons are also specified in the buttons argument, but they do not alter the response values. For example, vbQuestion adds a question-style icon, and vbExclamation adds a warning-style icon. Combine a button layout and an icon with Or, as in vbYesNoCancel Or vbQuestion.

MsgBox syntax and the buttons argument

The general function syntax is MsgBox(prompt, buttons, title, helpfile, context). Only prompt is required. The optional buttons argument can combine a button layout, an icon, a default-button choice, and certain other flags. title supplies the caption shown in the dialog’s title bar. Help-related arguments are rarely needed for ordinary Excel prompts.

If you omit buttons, VBA uses an OK-only dialog and the first button is the default. A default-button constant changes which available button is initially selected; it does not change the result values. For example, vbDefaultButton2 selects the second displayed button as the default. Choose a default deliberately, especially for prompts where the safer response should not be accepted by pressing Enter accidentally.

Here is a complete example that captures the result and gives each response a separate path. Save a copy of the workbook before running a macro: VBA changes cannot be undone with Ctrl+Z.

Sub AskBeforeContinuing()
    Dim answer As VbMsgBoxResult

    ' Offer three distinct responses.
    answer = MsgBox("Continue with this operation?", _
                    vbYesNoCancel Or vbQuestion Or vbDefaultButton2, _
                    "Confirm")

    Select Case answer
        Case vbYes
            MsgBox "The macro will continue.", vbInformation
        Case vbNo
            MsgBox "The macro will stop without continuing.", vbInformation
        Case vbCancel
            MsgBox "The request was cancelled.", vbInformation
    End Select
End Sub

Capture the result and branch on it

To make a decision, assign the function’s return value to a variable. Declaring it as VbMsgBoxResult communicates its purpose and lets the code compare it directly with the built-in constants. A variable declared as Long can also hold the numeric result, but using the enumeration type is clearer to a reader.

A short conditional is suitable when the only meaningful action is whether the answer was Yes. With a worksheet name in sheetName, for example, the macro might ask whether to continue and run the next operation only when the return is vbYes. For three or more possible responses, Select Case makes it easier to see that every choice has been considered.

One useful pattern is to exit the procedure unless the user explicitly agrees. It avoids accidentally treating No or Cancel as approval:

Sub ConfirmAction()
    Dim answer As VbMsgBoxResult

    ' Save a copy before running any macro that changes workbook data.
    answer = MsgBox("Proceed with the change?", vbYesNo Or vbQuestion, "Confirm")

    If answer <> vbYes Then Exit Sub

    ' Put the intended operation here, after the confirmation.
End Sub

The example deliberately uses vbYesNo because it only needs agreement or refusal. If the user must be able to distinguish refusing from backing out, use a Cancel button and handle vbCancel separately instead.

Choosing a default response

Button order and default selection influence how easy it is to respond safely. The default constants are vbDefaultButton1, vbDefaultButton2, vbDefaultButton3, and vbDefaultButton4. The first button is the normal default, so specify another only when it better fits the decision. The chosen default must correspond to a button that is actually present.

For a consequential action, avoid making the destructive or irreversible choice the default. A Yes/No prompt with Yes first can make a quick Enter press behave like approval. Setting No as the second-button default, or redesigning the prompt so the safer choice is selected initially, can reduce that risk. A confirmation dialog is not a substitute for a backup, validation, or a clear explanation of what the macro will do.

Cancel, Escape, and closing the dialog

Use a Cancel button when cancellation is a meaningful outcome, and test for vbCancel explicitly. In a dialog that includes Cancel, pressing Escape is treated as cancelling. Users may also try to close a dialog with its window close control; the behavior can depend on the available buttons and Windows dialog handling, so do not treat that gesture as a reliable substitute for a clearly offered choice.

When a user must choose between distinct actions, make those choices explicit. In particular, do not interpret every result other than vbNo as Yes: a Cancel response should not accidentally authorize an operation. A Select Case statement with a case for each offered result makes this safer and documents the intended behavior.

Common MsgBox result mistakes

  • Comparing with the layout constant: Check answer = vbYes, not answer = vbYesNo. The first is a response; the second configures buttons.
  • Discarding the return value: Calling MsgBox without assigning its result displays the message but leaves the macro with no stored choice to inspect.
  • Using text instead of a constant: Do not compare the result with the string "Yes". The function returns an enum value, not the visible button label.
  • Leaving out an offered outcome: If the dialog has Cancel, decide what the macro should do for vbCancel, even if the answer is simply to exit.
  • Using raw numbers: A test such as If answer = 6 Then can work because vbYes has that value, but the named constant explains the intent and is less error-prone.

Also remember that MsgBox is modal: the macro waits for a response before executing the next statement. A message box is useful for a brief confirmation or status message, but it is not a background notification and should not be used where the user needs to interact with the workbook while the prompt remains open.

When to use a different prompt

MsgBox is a good fit for a small number of fixed choices, such as Yes, No, or Cancel. It is not designed to collect arbitrary typed input. Use VBA’s InputBox when a simple text or numeric entry is appropriate, and validate what the user enters before using it. For richer forms with several fields, lists, or controls, a VBA UserForm offers more control over layout and validation.

For important decisions, keep the prompt concise and specific: say what will happen, not merely “Are you sure?” If a choice could delete or overwrite information, explain the consequence and provide a recovery route such as a saved copy. MsgBox buttons are fixed labels, so they are not a good way to present a long list of custom actions.

Frequently Asked Questions

What does VBA MsgBox return?
It returns a VbMsgBoxResult value identifying the response button selected, such as vbOK, vbYes, vbNo, or vbCancel.

What is the result of clicking Yes in MsgBox?
The return value is vbYes, whose numeric value is 6. Test the named constant, rather than the number, in normal code.

How do I check whether the user clicked No?
Assign the result to a variable and compare it with vbNo, or use a Select Case branch for vbNo. The dialog must include a No button, such as through vbYesNo.

Does MsgBox return a string?
No. It returns an enumerated numeric value. The displayed words on the buttons are labels, not strings returned by the function.

What does MsgBox return when the user cancels?
When the user selects the Cancel button, the result is vbCancel. Include that button in the layout and handle the result if cancellation needs to differ from No or from approval.

Can I show a message without using its result?
Yes. Call MsgBox as a statement when you only need to display information. If later code must depend on the user’s choice, capture the function result instead.

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.