VBA MsgBox Function: Syntax, Buttons, and Examples

VBA & macros · Updated November 17, 2026

In VBA, MsgBox displays a dialog box with a message and optional buttons, title, and icon. It also returns a value identifying the button the user clicked, so a macro can pause for a choice and then respond to it.

What the VBA MsgBox Function Does

MsgBox is a built-in Visual Basic for Applications function. In Excel, it is commonly used in macros to explain what is happening, report a result, or ask a person to confirm an action. It is not an Excel worksheet function: you do not enter it in a cell as you would SUM or IF.

A message box is modal by default. When VBA displays it, the running procedure waits until the user chooses a button or closes the dialog. That makes the function useful for decisions that need a response, but it can also interrupt a macro that should run unattended. The dialog appears in Excel, but the function belongs to VBA and can also be used in VBA projects in other Office applications.

For example, a macro might ask whether to clear a range, then clear it only if the user chooses Yes. A message box can also show a simple notification with an OK button. It is best suited to short, immediate communication—not lengthy instructions, data entry, or a permanent on-screen status display.

Syntax and Optional Arguments

The full syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is the message text. The remaining arguments are optional, but their position matters when you omit an earlier argument and want to supply a later one.

  • prompt: The text to display. Use vbCrLf to put text on a new line, or join text with the & operator.
  • buttons: A value made by combining constants that specify the button set, icon, default button, and modality. If omitted, the dialog has an OK button.
  • title: Text in the dialog’s title bar. If omitted, VBA uses the application name.
  • helpfile and context: Optional help-file information. These are for applications that provide a corresponding help file and context IDs; most ordinary Excel macros do not need them.

Because the function returns a result, you can either use it as a statement when you only need to display a message or assign its result to a variable when the choice matters. A title is useful even for a simple notification because it tells the reader which macro or task produced the message.

Choosing Buttons, Icons, and a Default

The buttons argument combines one button-group constant with optional icon, default-button, and modality constants. Use the named constants rather than unexplained numeric values; they make code easier to read and maintain. The button-group choices include:

  • vbOKOnly for a notification that needs no decision.
  • vbOKCancel or vbYesNoCancel when the user should be able to back out.
  • vbYesNo for a straightforward yes-or-no question.
  • vbAbortRetryIgnore and vbRetryCancel for particular recovery or retry situations.

Icons include vbInformation, vbQuestion, vbExclamation, and vbCritical. Choose an icon that matches the message rather than using a warning symbol to make routine information seem urgent. Default-button constants such as vbDefaultButton1 and vbDefaultButton2 select which button is initially active. For a potentially destructive choice, making a safe option the default can reduce accidental confirmation.

Combine the constants with the plus sign. For example, vbYesNo + vbQuestion + vbDefaultButton2 requests Yes and No buttons, a question icon, and No as the default. The default modality is application-modal, meaning the user must respond before continuing work in that application. A system-modal dialog is also available, but it is unusually intrusive and is rarely appropriate for normal workbook macros.

Understanding the Return Value

When a user clicks a button, MsgBox returns a value of the VbMsgBoxResult enumeration. Compare the result with named constants such as vbYes, vbNo, vbCancel, or vbOK. Avoid relying on their underlying numbers: named values explain what the code means and are less prone to mistakes.

The constants you should test depend on the buttons you displayed. A Yes/No dialog returns either vbYes or vbNo; it does not return vbOK. A dialog with a Cancel button can return vbCancel. A common bug is to show several choices but then test only for Yes, leaving the other outcomes without an intentional response.

It is often clearest to store the result in a variable declared as VbMsgBoxResult, then use an If statement or Select Case to handle it. This also makes it easier to add a deliberate Cancel path later. A Cancel choice should generally mean that the requested action does not proceed.

Excel Example: Confirm Before Clearing Cells

The following example asks for confirmation before clearing the contents of the active sheet’s used range. Before running any macro, save a copy of the workbook: changes made by VBA macros cannot be undone with Ctrl+Z. This example clears cell contents, including formulas, but does not clear formatting. Check that the active sheet is the one you intend to change.

Sub ConfirmBeforeClearing()
    Dim answer As VbMsgBoxResult

    ' Ask before removing values and formulas.
    answer = MsgBox("Clear contents from the active sheet?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm clearing")

    If answer = vbYes Then
        ActiveSheet.UsedRange.ClearContents
        MsgBox "The used range contents were cleared.", _
               vbInformation, "Done"
    Else
        MsgBox "No cells were cleared.", vbInformation, "Cancelled"
    End If
End Sub

The underscore at the end of a line continues the VBA statement onto the next line. The variable records the user’s selection, and the If block checks specifically for vbYes. If the user selects No, the macro follows the other branch instead. In a production workbook, you might replace ActiveSheet with a specifically named worksheet so the result does not depend on which sheet happens to be selected.

Displaying a Simple Message

If the macro only needs to tell the user that a task finished, you do not need to save the return value. Specify a button style, icon, and title to make the purpose clear:

Sub ShowCompletionMessage()
    ' Save a copy of the workbook before running macros.
    MsgBox "The report is ready.", vbOKOnly + vbInformation, "Report status"
End Sub

This displays an informational icon and an OK button. The comment is a reminder, not a backup mechanism; save the workbook through Excel before running the macro. A completion message can be helpful after a task that takes time, but repeated message boxes inside a loop can force users to dismiss a dialog for every record. For batch work, consider showing one summary at the end instead.

Writing Useful and Accessible Prompts

A good prompt names the action and its consequence. “Continue?” gives little context, while “Delete the selected rows from the Orders sheet?” helps a user judge the request. If the action affects many cells, a file, or a setting, say so plainly. Keep the message concise enough to scan, and use a specific title such as “Confirm deletion” rather than a generic label.

For a multi-line message, join text with vbCrLf. For instance, a prompt can put a short explanation on one line and the question on the next. Use standard text and meaningful button labels where possible; a message box offers a fixed set of buttons, so it is not suitable when choices need custom wording such as “Save draft” and “Discard.” In that situation, a custom VBA UserForm may be a better fit.

Do not use a message box as the only place to put long operating instructions. It blocks the workflow, is easy to dismiss, and is not a substitute for clear worksheet labels or documentation. Also avoid asking the user to type a value into a message box: MsgBox displays a message and choices, whereas an input dialog or custom form is designed to collect information.

Common Problems and Safe Use

  • The dialog shows only OK: Check that the intended button-group constant is included in the second argument. Omitting that argument uses the default OK-only style.
  • The macro ignores a choice: Make sure you assign the function’s result and compare it with the matching named constant. For example, a Yes/No prompt should test vbYes and, if relevant, vbNo.
  • The title is missing or unexpected: The title is the third argument. If you want a title but no special buttons, supply the button argument first, such as vbOKOnly.
  • The macro appears frozen: It may be waiting for a response because the message box is modal. Check for a dialog behind another window and ensure the macro is not displaying dialogs repeatedly in a loop.
  • A destructive action ran on the wrong sheet: Code using ActiveSheet acts on whichever sheet is active. Referencing a known worksheet explicitly is safer when the task must always target the same sheet.

Macros can change workbook data, and MsgBox itself does not make an action safe or reversible. Keep a backup before running unfamiliar code, review what the procedure changes, and do not enable macros in a workbook from an untrusted source merely to see a message. Follow your organization’s macro-security rules and use trusted files.

MsgBox Compared with InputBox and UserForms

MsgBox is for presenting text and receiving a choice from a predefined set of buttons. VBA’s InputBox function instead asks for a text response, such as a name or a short value. Excel also provides Application.InputBox, which can request a particular kind of input, including a range selection when configured for that purpose. Neither input method is a replacement for validating the value after it is entered.

Use a UserForm when the interaction needs custom button captions, several fields, validation, or a more structured layout. Use a worksheet cell or a status display when information should remain visible without interrupting the user. These alternatives are not automatically safer: the best choice depends on how much input is needed and whether the user must respond immediately before the macro continues.

Frequently Asked Questions

Is MsgBox a function or a command in VBA?
MsgBox is a VBA function because it returns a value identifying the selected button. You can still call it without storing that value when you only want to display a notification.

How do I make a Yes/No message box in Excel VBA?
Use vbYesNo in the buttons argument, for example: MsgBox("Continue?", vbYesNo + vbQuestion, "Confirm"). Assign the result to a variable if the macro must behave differently for Yes and No.

How do I check whether the user clicked Yes?
Declare a variable as VbMsgBoxResult, assign the result of MsgBox to it, and compare it with vbYes. This is clearer and more reliable than comparing the returned number.

Can MsgBox be used in an Excel cell formula?
No. It is VBA code, not a worksheet function that you enter directly into a cell. Put it in a VBA procedure, such as a macro. A message dialog is also a poor fit for a worksheet calculation because recalculation can occur more than once or at unexpected times.

Can I change the text on MsgBox buttons?
No. The built-in buttons have fixed captions. If you need custom choices or a more detailed interface, create a VBA UserForm or use another suitable input method.

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.