VBA MsgBox If: Check a User’s Choice with an If Statement

VBA & macros · Updated November 18, 2026

In VBA, use MsgBox inside an If statement to show a prompt and run different code according to the button the user clicks. For example, compare the function’s result with vbYes to detect Yes: If MsgBox("Continue?", vbYesNo) = vbYes Then.

What “MsgBox If” means in VBA

“VBA MsgBox If” is shorthand for a common pattern: ask a question with a message box, then use a conditional statement to respond. MsgBox displays a dialog and returns a value identifying the button selected. The If statement checks that value, so a macro can continue, stop, or take another action.

This is useful when a macro needs a simple decision from the person using the workbook. You might ask whether to proceed before clearing a range, confirm that a report should be generated, or let someone choose whether to continue after a warning. A message box is best for a small number of straightforward options; it is not a replacement for a larger form or a well-designed worksheet interface.

One important distinction is that MsgBox does not itself decide what the macro should do. It only displays the prompt and returns a button result. The If or Select Case code that follows is what defines the action for each possible response.

The basic If pattern

The general form is If MsgBox(prompt, buttons, title) = expectedResult Then, followed by the code for that result and, optionally, an Else branch. The prompt is required. The buttons and title are optional arguments, but specifying buttons makes the available choices clear.

For example, if you want Yes and No buttons, compare the returned result with vbYes. The key detail is the comparison: the result of MsgBox is a button-choice value, not the text displayed in the dialog. You do not compare it with the word Yes or with the prompt string.

Sub AskToContinue()
    Dim answer As VbMsgBoxResult

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

    If answer = vbYes Then
        MsgBox "The task can continue.", vbInformation, "Confirmed"
    Else
        MsgBox "The task was not continued.", vbInformation, "Stopped"
    End If
End Sub

Here, answer is declared as VbMsgBoxResult, the VBA type used for message-box results. Because the only buttons are Yes and No, the Else branch represents No. If your dialog offers more choices, handle each one explicitly rather than assuming every result other than Yes means the same thing.

Choosing buttons, icons, and a title

The second argument to MsgBox combines constants that control the buttons, icon, default button, and other dialog behavior. Add constants with the + operator. For example, vbYesNo + vbQuestion displays Yes and No buttons with a question icon. The icon is a visual cue; it does not change the result returned by the function.

Common button groups and their return constants include:

  • vbOKOnly returns vbOK.
  • vbOKCancel returns vbOK or vbCancel.
  • vbYesNo returns vbYes or vbNo.
  • vbYesNoCancel returns vbYes, vbNo, or vbCancel.
  • vbAbortRetryIgnore returns vbAbort, vbRetry, or vbIgnore.

Icon constants include vbInformation, vbQuestion, vbExclamation, and vbCritical. A title is the third argument, such as "Confirm update". A useful title and a specific prompt make the question easier to understand, especially when someone has several workbooks open.

You can set which button is initially selected with constants such as vbDefaultButton1 or vbDefaultButton2. For a Yes/No question where an accidental Enter key should not immediately approve an action, you can make No the default: use vbYesNo + vbQuestion + vbDefaultButton2. Choose the default deliberately; it should not nudge users into an unintended or risky choice.

A practical Yes-or-No example

This macro shows the common confirmation pattern. It asks a clear question, stores the response, and gives each response an explicit branch. The example does not modify the workbook; put the operation you actually want to run in the indicated branch.

Sub ConfirmUpdate()
    Dim answer As VbMsgBoxResult

    ' Make No the default choice for this confirmation.
    answer = MsgBox("Run the update now?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm update")

    If answer = vbYes Then
        ' Put the update procedure or code here.
        MsgBox "The update can run now.", vbInformation, "Confirmed"
    ElseIf answer = vbNo Then
        ' Leave the workbook unchanged when the user declines.
        MsgBox "No update was run.", vbInformation, "Cancelled"
    End If
End Sub

The line-continuation character, a space followed by an underscore, lets the MsgBox call continue on the next line for readability. The prompt should describe the action, not merely say “Are you sure?” If an action affects specific data, say what will happen so the user can make an informed choice.

Handling Yes, No, and Cancel

When the buttons include three choices, an If with an Else can accidentally group two different answers together. For example, treating every non-Yes response as No also treats Cancel as No. Use ElseIf branches or Select Case to handle each return value separately.

Sub ChooseReportAction()
    Dim answer As VbMsgBoxResult

    ' Offer three distinct actions.
    answer = MsgBox("Create the report?", _
                    vbYesNoCancel + vbQuestion, _
                    "Report")

    Select Case answer
        Case vbYes
            ' Start the report-generation code here.
            MsgBox "The report can be created.", vbInformation
        Case vbNo
            ' The user chose not to create the report.
            MsgBox "The report was not created.", vbInformation
        Case vbCancel
            ' Cancel means leave the current task as it is.
            MsgBox "No action was taken.", vbInformation
    End Select
End Sub

Select Case is often easier to scan when there are several possible results. If you use If instead, test the named results directly. Always decide what Cancel should mean in your process; it may mean stop, return to an earlier step, or leave the workbook untouched.

Where to put and run the VBA code

In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module, then paste a complete procedure into the module. Return to Excel and run the macro from the editor or through Excel’s macro interface, provided macros are allowed by your organization’s security settings.

On a Mac, the Visual Basic Editor can be opened from the Developer tab using Visual Basic; the keyboard shortcut may depend on the keyboard and function-key settings. If the Developer tab is not visible, enable it in Excel’s ribbon settings. The VBA logic shown here is the same in desktop Excel for Windows and Mac, though the editor and available integrations can differ.

Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) if you want to retain the code. Before running a macro that changes or deletes data, save a separate copy of the workbook. VBA macros cannot be undone with Ctrl+Z, so a confirmation dialog is not a substitute for a backup or careful testing.

Common mistakes and how to avoid them

  • Checking the wrong value: Compare the result with a return constant such as vbYes or vbCancel, not with the button’s displayed text.
  • Ignoring Cancel: If you include Cancel, give it a deliberate branch. Do not silently treat it as approval or as an ordinary No unless that is genuinely intended.
  • Using ambiguous prompts: A vague prompt can make the right button unclear. Name the action and, where relevant, what data it affects.
  • Assuming the dialog protects data: A person can choose the wrong button. Keep safeguards in the code too, such as checking that the expected worksheet or range exists before changing it.
  • Expecting custom button labels: The built-in MsgBox offers standard button sets. It does not let you rename Yes to a task-specific label. Use a UserForm when custom choices or more detailed instructions are needed.

A related misconception is that a question icon or a warning icon changes how VBA interprets the answer. It does not: the buttons determine the possible return values, and the icon only affects the presentation. Likewise, a message box does not validate a user’s workbook data. Code that depends on particular values should check those values separately before proceeding.

When to use MsgBox instead of another control

Use MsgBox for short, temporary questions with a small number of standard choices. It is quick to add and is appropriate for a one-time confirmation or simple status message. It pauses the macro while the dialog is open, so the user must respond before that procedure continues.

For a repeated workflow, a worksheet control or a VBA UserForm may be clearer. A UserForm can use custom button captions, show more context, and collect several pieces of input. A cell with data validation can guide choices directly on a worksheet without a pop-up. These options take more setup, but they can be easier for people who use the workbook regularly or need choices more descriptive than Yes and No.

Whichever interface you choose, keep the decision and the action separate enough to review. A simple confirmation should lead to a clearly defined branch, while important operations should also include suitable checks and a way to recover from mistakes, such as working on a copy.

Frequently Asked Questions

How do I use If with MsgBox in VBA?
Call MsgBox with the desired buttons, then compare its result with a constant. For example, store the result in a VbMsgBoxResult variable and test If answer = vbYes Then.

What does MsgBox return in VBA?
It returns a value identifying the button selected, represented by constants such as vbOK, vbYes, vbNo, and vbCancel. The available results depend on the button group displayed.

How do I check whether the user clicked No?
Compare the result to vbNo, as in If answer = vbNo Then. Make sure the message box uses a button set that includes No, such as vbYesNo or vbYesNoCancel.

Can a VBA MsgBox have custom button text?
No. The built-in message box uses standard button labels. If you need labels such as “Overwrite” and “Keep both,” create a VBA UserForm with custom controls.

Can I undo a macro after clicking Yes?
Usually, no: Excel’s Ctrl+Z undo history does not reverse VBA changes. Save a backup copy before running code that changes workbook data, and test the macro on a copy first.

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.