VBA MsgBox Default Button: Set It and Choose Safely

VBA & macros · Updated November 18, 2026

In VBA, a MsgBox uses its first button as the default unless you specify otherwise. To make another button the initial choice, combine the button layout with a constant such as vbDefaultButton2 in the second argument: MsgBox("Continue?", vbYesNo Or vbDefaultButton2).

What the default button means

The default button is the button that is initially highlighted or selected when the message box opens. It matters when someone presses Enter: the dialog activates its default choice, just as if the user clicked that button. It does not make VBA choose an answer by itself. The macro pauses at the message box until the user responds, and the chosen button determines the value returned to the code.

For example, a Yes/No message normally defaults to Yes because Yes is the first button. Adding vbDefaultButton2 makes No the initial choice instead. The buttons remain visible in their usual order; the constant changes which one is the default, not their order or wording.

This distinction is useful for confirmation prompts. If a dialog asks whether to delete records, making the safer option the default can reduce accidental confirmation when someone presses Enter without reading the prompt. A default is still only a starting selection, however: the user can choose another button with the mouse or keyboard.

Syntax and where to put the setting

The MsgBox syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is required; the remaining arguments are optional. The default-button constant belongs inside the buttons argument, alongside the button group and, if wanted, an icon. Use the VBA Or operator to combine these style constants.

Here, the prompt asks a question, vbYesNo creates the two choices, vbQuestion displays a question icon, and vbDefaultButton2 makes the second choice the default:

Sub AskBeforeContinuing()
    Dim answer As VbMsgBoxResult

    ' Ask for confirmation; No is the initial choice.
    answer = MsgBox("Continue with this action?", _
                    vbYesNo Or vbQuestion Or vbDefaultButton2, _
                    "Confirm action")

    If answer = vbYes Then
        MsgBox "The action can continue."
    End If
End Sub

Save a copy of the workbook before running a macro, especially if it changes data. VBA macros cannot be undone with Ctrl+Z, and a message box does not provide rollback protection for changes made after the user answers.

Default-button constants and button groups

VBA provides constants for choosing the default button position. The common choices are vbDefaultButton1, vbDefaultButton2, and vbDefaultButton3. The first button is the default when you omit a default-button constant. VBA also defines vbDefaultButton4, but standard MsgBox button groups have no fourth button, so the first three are the practical choices for the built-in layouts.

The position means the first, second, or third button in the displayed group, not a button identified by its caption. For instance, in a Yes/No/Cancel group, the positions are Yes, No, and Cancel. In a Retry/Cancel group, they are Retry and Cancel; there is no third button to select. Choose a default position that actually exists in the group you use.

Built-in button groups include vbOKOnly, vbOKCancel, vbAbortRetryIgnore, vbYesNoCancel, vbYesNo, and vbRetryCancel. The icon styles, such as vbInformation and vbExclamation, can also be combined in the same argument. Avoid combining multiple button-group constants: select one group, then add any desired icon and default-button setting with Or.

Choosing a sensible default

Pick the default based on what happens if the user presses Enter without noticing which choice is selected. A useful rule is to make the less destructive or less committal option the default when the prompt concerns deletion, overwriting, sending, or another consequential action. For a low-risk prompt where the expected action is clear, the first button may be a reasonable default.

  • Deletion: In a Yes/No prompt, use vbDefaultButton2 if No means leave the data untouched.
  • Saving changes: In a Yes/No/Cancel prompt, consider whether No or Cancel is the safer initial choice for the situation; do not assume one choice is universally best.
  • Routine acknowledgement: For an OK-only message, there is no alternative button to choose, so a default-button setting adds no useful choice.

Also make the prompt specific. “Are you sure?” gives little context, while “Delete the 12 selected rows from the report?” tells the user what the decision concerns. A cautious default cannot compensate for unclear wording, and an unexpected default can confuse people if the prompt does not explain the consequences.

How the return value relates to the default

After the user responds, MsgBox returns a value identifying the button activated. Assign it to a variable if the macro needs to make a decision. The result is not the default-button constant: vbDefaultButton2 configures the dialog, while values such as vbYes, vbNo, and vbCancel report the user’s response.

For a Yes/No dialog, checking the result explicitly makes the intended behavior easier to follow:

Sub ConfirmDeletion()
    Dim answer As VbMsgBoxResult

    ' No is the default to avoid accidental deletion.
    answer = MsgBox("Delete the selected records?", _
                    vbYesNo Or vbExclamation Or vbDefaultButton2, _
                    "Confirm deletion")

    If answer = vbYes Then
        ' Place the deletion operation here.
    ElseIf answer = vbNo Then
        ' Leave the records unchanged.
    End If
End Sub

The example deliberately does not perform a deletion; it shows where the decision belongs. Match the conditions to the button group you actually use. For example, code that offers Cancel should account for vbCancel as well as any Yes or No results, rather than treating every response other than Yes as an interchangeable outcome.

Common mistakes and troubleshooting

  • Putting the constant in the wrong argument: The default-button style belongs in the second, buttons, argument. The third argument is the title.
  • Using a position that is not present: A two-button group cannot have a meaningful third button. Keep the default position within the displayed group.
  • Expecting the button order to change: The default constant selects a position; it does not move buttons or change their captions.
  • Using a caption instead of a result constant: Test the returned VbMsgBoxResult against constants such as vbYes or vbNo, not the visible text of a button.
  • Forgetting the argument combination: A call with only vbDefaultButton2 may set a default style without specifying the button group you intended. Include the group explicitly, for example vbYesNo Or vbDefaultButton2.

If the expected button is not highlighted, first inspect the full second argument and confirm the group and default constant are combined in that argument. Then check the actual order of the buttons in that group. Some hosts or operating systems can present dialog details differently, so avoid instructions that rely only on a button’s screen position; refer to its label and purpose in the prompt.

Keyboard use and safer prompts

A default button is especially relevant to keyboard use because Enter activates the current default choice. Users may also move between controls with the keyboard, so the default should not be treated as the only accessible way to respond. Use clear button groups and plain-language prompts, and do not depend on an icon alone to communicate danger or meaning.

For a consequential action, a well-designed confirmation should identify the affected item or scope and make the safe choice understandable. It should also handle every possible response in the VBA logic. If Cancel is offered, treat it as a distinct decision when that distinction matters. Do not put an irreversible operation before the confirmation: ask first, inspect the returned value, and perform the operation only for the appropriate response.

MsgBox is modal: execution waits for the user to dismiss it. That is suitable for a short question or notification, but it can interrupt a longer workflow if used repeatedly. A default button improves the initial selection; it does not make a message less intrusive or replace proper validation.

When to use a UserForm instead

MsgBox is convenient when a short prompt needs one of VBA’s built-in button groups. It has limited choices and fixed message-box behavior, so it is not the right tool for every interaction. If users need custom button text, several fields, explanatory content, or a more controlled layout, create a VBA UserForm instead. A UserForm takes more setup, but lets the developer design the controls and handle their events directly.

For a simple built-in question, use MsgBox and set its default with the appropriate constant. For a process that needs more than the standard buttons or must collect structured input, use a UserForm or another suitable input interface. The choice is about how much interaction and control the task requires, not just which default button constant is available.

Frequently Asked Questions

What is the default button in VBA MsgBox if I do not set one?
It is the first button in the displayed group. To select another available position, add vbDefaultButton2 or vbDefaultButton3 to the second argument with Or.

How do I make No the default in a Yes/No MsgBox?
Combine vbYesNo and vbDefaultButton2 in the buttons argument, as in MsgBox("Continue?", vbYesNo Or vbDefaultButton2). In this group, No is the second button.

Does vbDefaultButton2 automatically return vbNo?
No. It sets the initial default selection. The user still has to respond, and the return value reflects the button they activate. Check that result, such as vbYes or vbNo, in your code.

Can I make the third button the default?
Yes, when the selected button group has a third button. Combine the group with vbDefaultButton3; for example, a Yes/No/Cancel group has three positions. A two-button group has no third choice to make default.

Can I set a custom default button caption in MsgBox?
No. MsgBox offers predefined button groups and captions. Use a VBA UserForm if you need custom button labels or a more tailored dialog.

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.