VBA MsgBox Yes No Cancel: Buttons and Example

VBA & macros · Updated November 18, 2026

To show Yes, No, and Cancel in a VBA message box, call MsgBox with the vbYesNoCancel button option, then check the result for vbYes, vbNo, or vbCancel. For example: answer = MsgBox("Continue?", vbYesNoCancel + vbQuestion, "Confirm"). The returned answer is a value your code can use to choose what happens next.

The basic VBA MsgBox syntax

MsgBox is a built-in VBA function. Its first argument is the message shown to the user; the second controls which buttons and icon appear; and the third is the optional title in the dialog’s title bar. A simple Yes/No/Cancel prompt looks like this:

Dim answer As VbMsgBoxResult
answer = MsgBox("Do you want to continue?", vbYesNoCancel, "Confirm")

Because the code assigns the result to answer, it can distinguish all three choices. If you call MsgBox without assigning its return value, the dialog still appears, but the procedure cannot respond differently to Yes, No, and Cancel.

The complete function syntax also allows optional help-file and context arguments. Most workbook prompts do not need them. The important point is that the button and icon options belong in the second argument, while the title belongs in the third. Passing a title as the second argument, for example, would put it in the wrong place.

What the three buttons return

The visible button labels are not the values VBA returns. The function returns a VbMsgBoxResult value, and named constants make the result easy to read:

  • vbYes means the user chose Yes.
  • vbNo means the user chose No.
  • vbCancel means the user chose Cancel.

These constants are preferable to comparing the result with raw numbers. In VBA, the corresponding values are 6 for Yes, 7 for No, and 2 for Cancel, but using the names makes the intention clearer and reduces mistakes. The order of the buttons in the dialog is controlled by the button-set constant; the return values do not depend on which button appears first.

A message box is modal: while it is open, VBA waits for the user’s response before running the next line of that procedure. That makes it useful for a short confirmation, but it is not a way to collect several fields or keep a workflow running in the background.

A complete Yes, No, Cancel example

This procedure shows a question icon, makes Cancel the default button, and handles each response separately. The actions are deliberately harmless so you can adapt the branching pattern to your own macro.

Sub AskWhetherToContinue()
    Dim answer As VbMsgBoxResult

    ' Ask a question and make Cancel the default choice.
    answer = MsgBox("Do you want to continue?", _
                    vbYesNoCancel Or vbQuestion Or vbDefaultButton3, _
                    "Confirm")

    Select Case answer
        Case vbYes
            MsgBox "You chose Yes. The next step can run."
        Case vbNo
            MsgBox "You chose No. The optional step can be skipped."
        Case vbCancel
            MsgBox "You canceled. The procedure can stop here."
    End Select
End Sub

Select Case is a readable way to cover every possible response. In a real procedure, replace the example follow-up messages with the appropriate actions. For instance, put a save or export operation in the Yes branch if Yes means proceed, and use Exit Sub in the Cancel branch if the procedure should end immediately. Do not put the consequential operation before the prompt, or it will already have happened by the time the user answers.

Choosing the icon and default button

The second argument combines options that control buttons, icon, and default selection. VBA permits these options to be combined with Or (or addition). The button set is required if you want Yes, No, and Cancel; the icon and default button are optional.

  • vbQuestion displays a question-mark icon. Choose an icon that fits the message rather than using one as decoration.
  • vbExclamation signals a warning, while vbInformation is for information. An icon does not change the return values or validate the user’s choice.
  • vbDefaultButton1, vbDefaultButton2, and vbDefaultButton3 set which displayed button is selected by default. With Yes/No/Cancel, button 3 is Cancel.

The default matters because pressing Enter activates it. For a prompt that precedes a destructive or hard-to-reverse action, making Cancel the default can reduce accidental continuation. It is not a substitute for a backup or a clear warning, and it does not prevent someone from choosing Yes. The default should match the risk and the likely safe choice, not simply be whichever button is easiest to code.

Handle Cancel as its own decision

A common logic error is to treat every response other than Yes as No. That can erase an important distinction: No may mean “skip this optional step,” while Cancel may mean “stop the whole operation.” Write a separate branch when those outcomes should differ.

For example, suppose a macro asks whether to refresh a report before exporting it. Yes could refresh and export, No could export the current report without refreshing, and Cancel could abandon the export. If the code groups No and Cancel together, it may export a report after the user intended to stop. Consider what each choice means in the context of the actual task before writing the branches.

In a procedure where both No and Cancel intentionally have the same effect, it is fine to group them. Still, make that choice explicit in the code or comments so a later change does not accidentally turn Cancel into permission to proceed. A Yes/No/Cancel dialog should not be used merely because three buttons are available; each response should have a defined meaning.

Using the prompt in an Excel macro

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and place the procedure in the module. You can run it from the editor with the cursor inside the procedure and F5, or run a macro from Excel using Developer > Macros. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon.

On a Mac, open the Visual Basic Editor from Tools > Macro > Visual Basic Editor where available, or use the Developer tab. The keyboard shortcut can depend on the Mac keyboard and function-key settings; Option+F11 may require holding Fn as well. The VBA logic and the named MsgBox constants are the same, although workbook and macro security controls differ between Windows and Mac.

Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) if you need to retain the code. Before running a macro that changes workbook data, save a separate copy: VBA actions generally cannot be undone with Ctrl+Z, and Excel clears the ordinary undo history when a macro runs. Enable macros only in files you trust. A message box asks for a response; it does not make an unsafe macro safe.

Common problems and how to avoid them

  • The dialog has only an OK button: Check that vbYesNoCancel is included in the buttons argument, and that it has not been placed in the prompt or title argument by mistake.
  • The code cannot tell what the user chose: Assign the function result to a variable, then compare it with the named constants or use Select Case.
  • Cancel runs the same action as Yes: Review the branches and make sure the procedure does not continue into the action after the dialog regardless of the result. Put the action inside the Yes case when it must be exclusive to Yes.
  • The wrong button is selected initially: Check the default-button option. With three buttons, vbDefaultButton3 selects the third, Cancel button.
  • The code does not compile: Check for a misspelled constant, unmatched quotes, or a line-continuation underscore that is not preceded by a space. If you use an answer variable, declare it, for example as VbMsgBoxResult.

Also keep the prompt specific. “Are you sure?” does not tell someone what they are confirming. A message such as “Delete the selected rows from this worksheet?” gives the person more context, though critical actions may need an additional safeguard such as a backup or a review step.

When a MsgBox is not the right tool

A Yes/No/Cancel box is suitable for a brief decision with three clearly distinct outcomes. It is less suitable when users must type a value, choose among many options, review a long explanation, or select several settings. A custom VBA UserForm can provide labels, text boxes, lists, and more deliberate layout for those cases.

Excel’s Application.InputBox is another option when the macro needs a typed value or a selected range. It is not simply a larger Yes/No/Cancel dialog: its result handling differs, and cancellation needs to be checked according to the type of input requested. For a basic proceed, skip, or stop choice, the built-in MsgBox is usually simpler and easier for another VBA reader to understand.

Frequently Asked Questions

How do I make a VBA MsgBox with Yes and No only?
Use vbYesNo in the buttons argument, assign the result to a variable, and test it against vbYes or vbNo. Add vbCancel only when cancellation has a useful, separate meaning.

What does MsgBox return when I click Cancel?
It returns the named result vbCancel. Compare against that constant rather than relying on its numeric value, and decide explicitly whether cancellation should stop the procedure.

Can I make Cancel the default button?
Yes. Combine vbDefaultButton3 with vbYesNoCancel; Cancel is the third button in that set. Pressing Enter then activates the default choice.

Does clicking the X close button count as Cancel?
When a Cancel button is present, closing the dialog through its close control is treated as cancellation in the usual VBA message-box behavior. Handle vbCancel safely rather than assuming the user chose No or Yes.

Can a MsgBox show Yes, No, and Cancel with a custom button label?
No. The built-in MsgBox uses its standard button labels. Use a VBA UserForm if the interface needs custom labels or a more complex set of choices.

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.