VBA MsgBox Yes/No: Continue If the User Clicks Yes

VBA & macros · Updated November 18, 2026

Use VBA’s MsgBox with the vbYesNo button option, save the result, and put the work that should continue inside an If answer = vbYes Then block. If the user clicks No, VBA follows the other branch—or exits the procedure if you choose an early-exit pattern.

Sub ConfirmAndContinue()
    Dim answer As VbMsgBoxResult

    ' Ask for confirmation and store the selected button.
    answer = MsgBox("Continue with this task?", vbYesNo + vbQuestion, "Confirm")

    If answer = vbYes Then
        ' Put the work to perform after Yes here.
        MsgBox "Continuing.", vbInformation, "Status"
    Else
        ' This branch runs when the user selects No.
        MsgBox "The task was stopped.", vbInformation, "Status"
    End If
End Sub

This pattern works in desktop Excel VBA on Windows and Mac. Excel for the web does not run VBA macros, so a workbook opened there cannot execute this code.

How the Yes/No decision works

MsgBox both displays a dialog and returns a value representing the button selected. The first argument is the prompt. The second argument combines options that control the buttons, icon, and default button. The third argument, shown above, supplies the dialog title.

With vbYesNo, the user sees Yes and No buttons. The function returns vbYes when Yes is selected and vbNo when No is selected. These are named VBA constants, so compare the result to the constants rather than using their numeric values. Named constants make the code easier to read and less likely to be misunderstood later.

The variable is declared as VbMsgBoxResult, the VBA enumeration type intended for a message-box result. A variable declared as Long can also hold the value, but using the specific type communicates what the variable means. Assign the return value to a variable before testing it; otherwise, the code has no saved answer to check.

A complete macro with a clear No outcome

The first example includes an explicit Else branch. That is useful when clicking No should produce a message, log a decision, or run a different safe action. If No simply means stop, the branch can contain no operation, but an explicit response can reassure users that the macro understood their choice.

For instance, imagine a report macro that asks whether to refresh a report. The refresh operation belongs in the Yes branch, and the No branch should leave the report untouched. The message box does not perform the refresh itself; it only supplies the decision. The code inside the branch determines what happens next.

Keep the boundary between the branches clear. Statements placed after End If run whether the user chooses Yes or No, unless an earlier statement exits the procedure. That distinction matters: placing a task after the conditional by mistake can make it run even after No.

Stop immediately when the answer is not Yes

For a short macro, an early exit is often easier to read than a full If/Else structure. This version ends the current procedure unless the answer is Yes:

Sub StopUnlessConfirmed()
    Dim answer As VbMsgBoxResult

    ' Stop the procedure unless the user confirms.
    answer = MsgBox("Run the next step?", vbYesNo + vbQuestion, "Confirm")
    If answer <> vbYes Then Exit Sub

    ' Put the confirmed operation below this point.
    MsgBox "The next step is running.", vbInformation, "Status"
End Sub

The comparison answer <> vbYes means “answer is not Yes.” Since this dialog offers only Yes and No, it stops when No is selected. The early exit applies only to the current procedure; it does not close Excel or stop unrelated macros that may run elsewhere.

Choose this structure when there is one clear gate at the start of a procedure. Use an If/Else block when both choices need meaningful actions, or when you want the No path to be especially visible to someone maintaining the code.

Choose buttons, icons, and a safe default

VBA lets you combine message-box options with the plus sign. For example, vbYesNo + vbQuestion gives the Yes/No buttons and a question-mark icon. The icon is visual context; it does not change what the return value means. Use an icon to support the message, not as a substitute for explaining the consequence in words.

You can also set the initially selected button. Add vbDefaultButton2 to make the second button the default. With the standard Yes/No order, the second button is No:

Sub ConfirmWithNoAsDefault()
    Dim answer As VbMsgBoxResult

    ' Make No the default button for this confirmation.
    answer = MsgBox("Continue with the change?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm change")

    If answer = vbYes Then
        ' Continue only after an explicit Yes selection.
        MsgBox "Continuing with the change.", vbInformation, "Status"
    End If
End Sub

A No default can be sensible when the action is consequential or the user may dismiss the prompt without reading carefully. A default button does not replace the conditional: the macro still needs to inspect the returned value. If the user should have a separate Cancel choice, use a button set that includes Cancel, such as vbYesNoCancel, and handle vbCancel as its own result.

Write a prompt that makes the choice meaningful

A dialog that says only “Are you sure?” forces the user to remember what the macro is about to do. A stronger prompt names the action and, when useful, its scope: for example, “Clear the values in the selected input cells?” The Yes and No buttons can remain unchanged, while the prompt explains the real decision.

For actions that change or remove data, consider telling the user what will be affected and whether the action can be reversed. Avoid claiming that a choice is harmless if the macro performs changes elsewhere. A message box is a confirmation step, not a backup, validation check, or undo system.

  • Use a concise prompt that names the action.
  • Use a clear title, such as “Refresh report” or “Confirm deletion,” rather than a generic title.
  • Make the No path safe and predictable.
  • Use a default No for consequential choices when that suits the workflow.

Where to put the VBA and how to run it

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11. In the editor, choose Insert > Module, paste the procedure into the module, and run it with the cursor inside the procedure by pressing F5. You can also return to Excel and open the macro list with Alt+F8, select the macro, and choose Run.

On Mac, the Visual Basic Editor shortcut is generally Option+F11; depending on the keyboard’s function-key settings, you may need Fn+Option+F11. In the editor, use Insert > Module as on Windows. Keyboard shortcuts can vary with hardware and system settings, so the Excel or editor menus are an alternative way to access the same commands.

Save the workbook in a macro-enabled format such as .xlsm if it needs to retain VBA. Before running a macro that changes a workbook, save a separate copy. VBA changes cannot be undone with Excel’s Ctrl+Z on Windows; the same limitation applies to Undo on Mac. A confirmation prompt is not a replacement for a recoverable copy.

Common problems and how to fix them

The macro continues after No. Check whether the operation is accidentally placed after End If, where it runs regardless of the choice. Move it inside the Yes branch, or use the early-exit pattern before the operation.

The comparison never behaves as expected. Confirm that the variable receives the result, as in answer = MsgBox(...), and that the comparison uses vbYes or vbNo. Comparing the result with text such as "Yes" is incorrect: the function returns a result constant, not the button caption as a string.

The code does not compile. Check that each If has a matching End If, the procedure has End Sub, and any continued line has a space followed by an underscore at the end. If you use Option Explicit, declare the result variable before using it; declaring it as VbMsgBoxResult is appropriate.

The message box does not appear in the web version. Excel for the web cannot run VBA. Open the workbook in the desktop Excel application to run the macro. Also check that macros are permitted by the workbook’s security settings; do not enable macros in files from sources you do not trust.

When to use another approach

MsgBox is useful for a quick, blocking confirmation in a desktop macro. It is not designed for a custom form with many fields, a persistent choice, or a workflow that needs several pieces of input. For a richer dialog, a VBA UserForm can provide labeled controls and more detailed instructions, but it requires more design and code.

For a simple two-way decision, the built-in message box is usually the clearest option. If the task runs unattended, a dialog may be a problem because it waits for someone to respond. In that situation, consider a non-interactive rule, such as stopping safely or recording a status, instead of waiting indefinitely for a click.

Frequently Asked Questions

How do I continue only if the user clicks Yes in VBA? Store the result of MsgBox in a variable, then place the next statements inside If answer = vbYes Then. This ensures that the Yes branch runs only for the Yes result.

What does MsgBox return for No? When the dialog uses vbYesNo, selecting No returns the VBA constant vbNo. Compare against that constant if you need a specific No branch.

Can I make No the default button? Yes. Add vbDefaultButton2 to the button options. For the usual Yes/No order, button two is No. Still test the returned result in your code.

Can a Yes/No message box have a Cancel button? Not with vbYesNo alone. Use vbYesNoCancel if the user needs three distinct choices, then handle vbYes, vbNo, and vbCancel separately.

Does this VBA code work in Excel for the web? No. Excel for the web does not execute VBA macros. Run the workbook in desktop Excel for Windows or Mac 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.