VBA MsgBox Cancel: Buttons, Return Values, and Code

VBA & macros · Updated November 18, 2026

To detect Cancel in VBA, show a MsgBox button set that includes Cancel, store the function’s result, and compare it with vbCancel. For example, vbYesNoCancel provides a Cancel button; a vbYesNo box does not, so it cannot return vbCancel.

What Cancel means in a VBA MsgBox

MsgBox is a VBA function that displays a dialog and returns a value representing the button the user chose. The returned value has the VbMsgBoxResult type. When a dialog includes Cancel and the user chooses it, the return value is vbCancel. Your code can then stop, skip an operation, or return to an earlier step rather than continuing as though the user had confirmed.

The important distinction is that Cancel is not automatically available on every message box. A plain call such as MsgBox "Continue?" displays only an OK button by default. To offer cancellation, specify a button combination that includes it, such as vbOKCancel, vbRetryCancel, or vbYesNoCancel. Then capture the returned value if the next action depends on the response.

Cancel is also a choice in the dialog, not a general-purpose way to interrupt every running macro. The macro waits while the message box is open. After the user responds, VBA continues at the next statement unless your code branches or exits.

Choose a button set that includes Cancel

The second argument to MsgBox, named buttons, controls which buttons appear and can also specify an icon and a default button. Use a built-in button constant that matches the decision you want the user to make:

  • vbOKCancel: OK or Cancel, useful when OK means proceed.
  • vbRetryCancel: Retry or Cancel, suitable when an operation can be attempted again.
  • vbYesNoCancel: Yes, No, or Cancel, when Yes and No represent different deliberate answers and Cancel means neither.
  • vbAbortRetryIgnore: Abort, Retry, or Ignore, for a three-way response where those choices are genuinely appropriate.

By contrast, vbYesNo does not include Cancel. Do not write code that expects vbCancel from a button set that cannot return it. If the user must be able to back out, choose a cancellable combination and make the surrounding instructions clear about what each button does.

You can combine the button-set constant with an icon and a default-button constant using Or. For instance, vbYesNoCancel Or vbQuestion Or vbDefaultButton3 shows a question icon and makes the third button the default. In that combination the third button is Cancel. Choose the default carefully: pressing Enter activates it, so a potentially consequential action should not be selected by default without good reason.

How the return values work

Each button choice has a named VBA constant. Comparing against the names makes code easier to read and less error-prone than relying on the numeric values. Common return constants are:

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

These are results, not button-set constants. For example, vbOKCancel describes the buttons to show, while vbCancel describes a result to test after the user responds. Keeping that distinction in mind helps prevent a common mix-up: use a button-set constant when constructing the dialog, and a result constant when evaluating the answer.

For a simple OK-or-Cancel prompt, an If statement is often enough. With a prompt in VBA code, the pattern is answer = MsgBox(...) followed by If answer = vbCancel Then Exit Sub. For three or more choices, Select Case is clearer because it makes each possible result explicit.

Example: handle Yes, No, and Cancel

This example demonstrates three distinct outcomes. Yes continues to a placeholder action, No skips it, and Cancel exits the procedure. The message text describes those outcomes so the buttons are not left open to interpretation.

Public Sub AskBeforeContinuing()
    Dim answer As VbMsgBoxResult

    ' Offer three clearly different choices.
    answer = MsgBox("Run the next step? Yes runs it; No skips it; Cancel stops here.", _
                    vbYesNoCancel Or vbQuestion Or vbDefaultButton3, _
                    "Confirm next step")

    Select Case answer
        Case vbYes
            MsgBox "The next step would run here.", vbInformation
        Case vbNo
            MsgBox "The next step was skipped.", vbInformation
        Case vbCancel
            Exit Sub
    End Select
End Sub

The variable is declared as VbMsgBoxResult, which communicates what kind of value it stores. The line continuation character, an underscore preceded by a space, lets the function call span several lines. You could also put the call on one line; the behavior would be the same.

Before running a macro that changes workbook data, save a copy of the workbook. VBA macros generally cannot be undone with Ctrl+Z, so a Cancel branch does not restore changes that the macro made before displaying the prompt. This example only displays messages, but the precaution matters when adapting it to edit cells, delete records, or save files.

Cancel an operation before it begins

A confirmation prompt is most useful before an action that is difficult to reverse. Put the prompt before the action, and place the action only in the branch that represents confirmation. For example, if a procedure is about to clear a range, it should first ask whether to proceed, immediately exit when the result is Cancel, and only then clear the range after an affirmative response.

For an OK-or-Cancel prompt, the same principle can be written as:

Public Sub ConfirmAction()
    Dim answer As VbMsgBoxResult

    answer = MsgBox("Continue with this action?", vbOKCancel Or vbExclamation, _
                    "Confirm action")

    If answer = vbCancel Then Exit Sub

    ' Put the confirmed action here, after the cancellation check.
    MsgBox "The action can continue.", vbInformation
End Sub

Here, anything after the cancellation check runs only if the user did not choose Cancel. With this two-button set, the remaining response is OK. If a dialog offers more choices, use a Select Case or explicitly test for the response that authorizes the action. This is safer than assuming every result other than Cancel means Yes.

Common mistakes and behavior to check

  • Expecting Cancel from a dialog without it: A vbYesNo message box has no Cancel button. Change the button set if users need a way to decline without choosing No.
  • Ignoring the returned value: Calling MsgBox without assigning its result displays the dialog but gives your code no decision to inspect afterward.
  • Comparing against text: The function returns a button-result constant, not the displayed word. Test answer = vbCancel, not answer = "Cancel".
  • Confusing button options and results: Use vbOKCancel to show buttons and vbCancel to recognize a response.
  • Letting code continue accidentally: Merely detecting Cancel does not stop the procedure. Add an exit, skip, or other explicit branch.

Users can also respond from the keyboard. For a dialog that has a Cancel button, pressing Esc generally selects Cancel. The precise result is still governed by the buttons and response the dialog supports. Do not assume that a keyboard shortcut or closing the window substitutes for a deliberately designed cancellation path; make the available choices obvious and handle the returned result.

MsgBox compared with Application.InputBox

Use MsgBox when you need a small set of fixed responses, such as Yes, No, and Cancel. It is not designed to collect a name, number, or cell reference. For input in Excel VBA, Application.InputBox provides a prompt and can restrict the expected type through its Type argument. It also has a Cancel outcome, but its return behavior differs from MsgBox.

When Application.InputBox is canceled, it returns the Boolean value False. This creates a possible ambiguity if the input is allowed to be a Boolean value: a returned False might be an entered answer rather than cancellation. A common way to avoid that ambiguity is to set the input type to one appropriate for the task, such as text or a number, and then check for the cancellation result before using the input.

Do not substitute InputBox for MsgBox just to get a Cancel button. Use a message box for a decision among known options, and an input box when the user must provide a value. If the task requires a more complex form, validation, or several related inputs, a VBA UserForm may be a better fit than either dialog.

Designing a clear and safe prompt

A good Cancel button is part of a clear interaction, not just a code branch. Tell users what the action will do and what each choice means. A vague prompt such as “Are you sure?” does not identify the operation, and a Yes/No/Cancel arrangement can be confusing if No and Cancel are functionally identical. Choose only the options that represent meaningfully different outcomes.

Consider these practical points when designing a prompt:

  • Put the important consequence in the message, such as whether data will be overwritten or a step will be skipped.
  • Use a button set that matches the decision; do not add choices merely because they are available.
  • Set a default button that does not encourage accidental confirmation of a risky action.
  • Check the result before changing workbook contents, sending information, or closing a document.
  • Make the cancellation path harmless and predictable, especially if earlier steps have already changed data.

The icon is a visual cue, not a replacement for precise wording. For example, vbExclamation can draw attention to a warning, but the text still needs to explain what happens. Likewise, a confirmation dialog is not a security boundary: it cannot prevent other code or a user with access to the workbook from making changes.

Frequently Asked Questions

What does MsgBox return when I click Cancel?
When the message box includes a Cancel button and the user chooses it, the function returns vbCancel. Store the result and compare it with that constant to branch on the choice.

How do I make Cancel exit a VBA sub?
Capture the result from a cancellable button set, then use If answer = vbCancel Then Exit Sub. Put that check before the operation you want to prevent.

Can a vbYesNo MsgBox return Cancel?
No. vbYesNo displays Yes and No, not Cancel. Use a button set such as vbYesNoCancel if all three responses are needed.

What is the difference between vbCancel and vbOKCancel?
vbOKCancel is a button-set constant that displays OK and Cancel. vbCancel is a result constant returned when the user chooses Cancel.

Does pressing Esc cancel a VBA MsgBox?
For a message box that offers Cancel, Esc generally activates Cancel. The robust approach is to test the returned value rather than rely on how the user dismissed the dialog.

Can I undo a macro after choosing Cancel?
Cancel only controls what your code does after the prompt; it does not undo earlier macro changes. VBA macro actions generally cannot be reversed with Ctrl+Z, so save a workbook copy before running code that edits data.

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.