VBA MsgBox Buttons: Options, Examples, and Return Values

VBA & macros · Updated November 18, 2026

In VBA, the MsgBox function’s buttons argument controls which buttons and icon appear, which button is selected by default, and sometimes how the dialog behaves. For example, use vbYesNo + vbQuestion for a yes-or-no question, then check whether the function returns vbYes or vbNo.

What VBA MsgBox buttons do

A message box is a small, modal dialog that displays a prompt and waits for a user response. The buttons you specify should match the decision the macro needs: an information-only message may need just OK, while a confirmation may offer Yes and No. The dialog does not carry out the action itself; your VBA code must inspect the response and decide what happens next.

The buttons are selected through the second, optional argument to MsgBox. If you omit that argument, VBA displays an OK button. You can also combine a button set with an icon or a default-button setting. The available choices are built-in constants, so using their names is clearer and less error-prone than typing the corresponding numbers.

Syntax and return values

The general syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is the message text. The optional buttons argument specifies the button group and related display options; title supplies the caption in the dialog’s title bar. The final help arguments are used when connecting a dialog to a help file. If you need a response, assign the function’s result to a variable.

VBA returns a value identifying the button the user clicked. The important return constants are vbOK, vbCancel, vbAbort, vbRetry, vbIgnore, vbYes, and vbNo. These are not the same as the constants that choose which buttons to show. For instance, vbYesNo configures the displayed pair, while vbYes identifies a response.

When you do not need to branch on the user’s choice, you can call MsgBox without assigning its return value. When the choice matters, capture it and test the result explicitly. That distinction prevents a common mistake: displaying Yes and No buttons but never using the answer.

Button and icon constants

Choose one button-set constant. The most commonly used sets are:

  • vbOKOnly: OK.
  • vbOKCancel: OK and Cancel.
  • vbAbortRetryIgnore: Abort, Retry, and Ignore.
  • vbYesNoCancel: Yes, No, and Cancel.
  • vbYesNo: Yes and No.
  • vbRetryCancel: Retry and Cancel.

You can add one icon constant to a button set. vbInformation displays an information icon, vbExclamation an exclamation icon, vbQuestion a question icon, and vbCritical a critical-error icon. Icons communicate the nature or tone of the message; they do not change what the buttons do or what values they return.

Constants for additional options include vbDefaultButton1, vbDefaultButton2, vbDefaultButton3, and vbDefaultButton4. The default is the first button unless you choose another. Other flags include vbApplicationModal (the default modality), vbSystemModal, vbMsgBoxHelpButton, vbMsgBoxRight, and vbMsgBoxRtlReading. Most everyday prompts need only a button set and, optionally, an icon.

How to combine options and choose a default

Combine compatible constants with + or the VBA Or operator. For example, vbYesNo + vbQuestion + vbDefaultButton2 shows Yes and No, uses a question icon, and makes No the default choice. These options are defined as flags, so the names help convey the intended combination.

Use exactly one button-set constant. Adding two button sets together does not create a larger custom set; it produces an invalid or unintended combination. Similarly, a default-button option is useful only when that button exists in the selected set. With vbYesNo, for example, vbDefaultButton2 selects No by default. A default is not a preselected answer: the user still has to confirm a choice.

Choose the default with care. For a decision that could discard data or trigger a consequential action, make the safer or less destructive choice the default, or avoid a default that nudges the user toward proceeding. Also keep prompts specific: “Save changes before closing?” is more useful than “Continue?” because the user can understand what Yes and No mean.

Example: ask for confirmation

Before running any macro, save a separate copy of the workbook. VBA macros cannot be undone with Ctrl+Z, and a macro can change workbook data. This example asks a question and branches on the response; it does not delete anything until you add your own action in the indicated place.

Sub ConfirmAction()
    Dim answer As VbMsgBoxResult

    ' Ask a clear question; make No the default.
    answer = MsgBox("Proceed with the action?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm action")

    If answer = vbYes Then
        ' Put the intended action here.
        MsgBox "You chose Yes.", vbOKOnly + vbInformation, "Response"
    ElseIf answer = vbNo Then
        MsgBox "No action was taken.", vbOKOnly, "Response"
    End If
End Sub

The variable is declared as VbMsgBoxResult, which makes its purpose clear. The Yes and No branches compare the returned response constants, not the button-set constant. If you add workbook-changing code to the Yes branch, test it on a copy first.

Handling three buttons with Select Case

When the dialog offers three choices, a Select Case statement keeps the possible outcomes easy to read. A Cancel choice should have a defined meaning in the surrounding workflow; it should not accidentally fall through to the same behavior as Yes or No.

Sub AskToSave()
    Dim answer As VbMsgBoxResult

    ' Offer three distinct responses before continuing.
    answer = MsgBox("Save changes before continuing?", _
                    vbYesNoCancel + vbQuestion, _
                    "Unsaved changes")

    Select Case answer
        Case vbYes
            ' Add code that saves the relevant changes.
            MsgBox "Save was selected.", vbOKOnly, "Choice"
        Case vbNo
            ' Continue without saving, if that is appropriate.
            MsgBox "No was selected.", vbOKOnly, "Choice"
        Case vbCancel
            ' Stop or return to the earlier workflow.
            MsgBox "The operation was cancelled.", vbOKOnly, "Choice"
    End Select
End Sub

This example displays follow-up messages rather than saving or discarding data. In a real macro, connect each branch to the appropriate action, and make sure the Cancel branch genuinely stops or backs out of the operation when that is what the prompt promises.

Icons, titles, and other display options

A useful message box has a concise prompt and a title that identifies its context. If you omit the title, VBA uses the application name. A custom title such as “Confirm action” or “Import complete” helps distinguish a prompt from other dialogs, especially when a macro runs as part of a longer task.

Pick an icon for meaning, not decoration. An information icon suits a completion notice; an exclamation icon can signal a caution; a critical icon is appropriate for a serious error. A question icon can accompany a genuine decision, but the wording should still explain the action. Do not rely on color or the icon alone to convey essential instructions.

For right-to-left text or alignment, the vbMsgBoxRtlReading and vbMsgBoxRight flags can affect text presentation. vbMsgBoxHelpButton can add a Help button when used with suitable help-file and context arguments. Such options are specialized; they do not replace a clear prompt or response handling.

Common mistakes and limitations

  • Confusing display constants with response constants: vbYesNo chooses two buttons; vbYes is the return value to test after a click.
  • Ignoring the return value: If the macro must behave differently for each choice, store the result and use an If statement or Select Case.
  • Combining incompatible button sets: Add one set, then optional icon and display flags. Do not add vbOKCancel to vbYesNo expecting four buttons.
  • Using Cancel as a vague synonym for No: Cancel usually means stop or back out; explain that distinction in the prompt and code.
  • Expecting a custom button label: MsgBox offers fixed button captions. It cannot rename Yes to “Overwrite” or add arbitrary controls.

A message box is also modal: the user must respond before continuing in the relevant context. That makes it useful for brief decisions, but disruptive for routine status updates or repeated notifications. Avoid putting a prompt inside a loop if it could appear dozens or hundreds of times. For unattended automation, interactive dialogs may prevent the procedure from completing.

When to use a UserForm or another prompt

Use MsgBox when a short message and a standard set of responses are enough. If you need custom button text, several fields, validation, a list of choices, or a more carefully arranged layout, create a VBA UserForm instead. A UserForm takes more setup, but its controls and labels can match the task more closely.

For a simple request for text or a value, VBA’s InputBox function may be suitable. It is not a drop-in replacement for a Yes/No confirmation: it asks for text, and its function form returns an empty string when the user cancels, which can be ambiguous if an empty response is also valid. Excel’s Application.InputBox offers additional options, including a way to specify the expected type. Choose the prompt style that best represents the information the macro needs.

Frequently Asked Questions

What is the default button in VBA MsgBox?
If you do not specify a default-button flag, the first button is the default. Add a flag such as vbDefaultButton2 to select the second button instead, provided that button is part of the chosen button set.

How do I find out which MsgBox button the user clicked?
Assign the result of MsgBox to a variable, then compare it with a response constant such as vbYes, vbNo, or vbCancel. The returned value tells you the selection; it is separate from the constant that configured the visible buttons.

Can I change the text on a VBA MsgBox button?
No. The built-in dialog uses fixed labels such as OK, Yes, and Cancel. Use a UserForm if the interface requires custom captions or other controls.

How do I show Yes and No buttons with an icon?
Set the buttons argument to a combination such as vbYesNo + vbQuestion. Then capture the function’s result and handle vbYes and vbNo separately if the macro needs to respond to the choice.

Can VBA MsgBox show four buttons?
The standard MsgBox button sets do not provide a four-button combination. For more choices or custom labels, build a UserForm rather than trying to combine multiple button-set constants.

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.