VBA MsgBox Command: Syntax, Buttons, and Examples in Excel

VBA & macros · Updated November 17, 2026

In VBA, MsgBox displays a message in a dialog box and can return a value indicating which button the user clicked. In Excel, use it to show information, ask a simple question, or pause a macro for confirmation; assign its result to a variable when the macro needs to respond to the choice.

What MsgBox Does in VBA

MsgBox is a built-in VBA function available in Excel and other Office applications that support VBA. It shows a message and waits for the user to dismiss the dialog or choose one of its buttons. Because the dialog is modal, the macro pauses while the box is open. Execution continues after the user responds.

Despite the common phrase “MsgBox command,” it is technically a function: it returns a result. You can use it as a simple statement when you only need to show a message, or capture its result when the next step depends on the user’s choice. A message box is not a worksheet formula, and it cannot be entered into a cell to display a dialog when a formula recalculates.

For example, a workbook macro might announce that a report is ready, warn that an operation will change data, or ask whether to continue. A message box is best for short, immediate interactions. It is not a replacement for a form when a user must enter several fields or make a complex set of choices.

Syntax and Arguments

The general syntax is MsgBox(prompt, [buttons], [title], [helpfile], [context]). The square brackets indicate optional arguments; they are notation, not characters to type in VBA. The prompt is the text shown in the dialog. The remaining arguments control the buttons and icon, the title-bar text, and optional help-file information.

  • prompt: Required text for the message. You can join strings and insert line breaks, for example with vbCrLf.
  • buttons: Optional constants that select a button group, an icon, a default button, and certain display options. If omitted, the dialog has an OK button.
  • title: Optional text in the title bar. If omitted, VBA uses the application name.
  • helpfile and context: Optional information that connects the dialog to a help file and a help topic. When providing a help file, provide its context value as well.

VBA permits more than one calling style. When you assign the returned value, use parentheses around the arguments, as in choice = MsgBox(...). When you discard the result and call the function directly, write MsgBox "Report complete." without parentheses. With the Call statement, parentheses are used: Call MsgBox("Report complete."). Mixing these conventions is a common source of syntax errors.

Choosing Buttons, Icons, and a Default

The buttons argument is usually built from named VBA constants. These make the code easier to read than numeric values. The button-group constants include vbOKOnly, vbOKCancel, vbAbortRetryIgnore, vbYesNoCancel, vbYesNo, and vbRetryCancel. Pick a group that offers choices the macro can actually handle. For example, do not show Yes and No if both responses lead to the same outcome.

You can combine a button group with an icon constant such as vbInformation, vbExclamation, vbQuestion, or vbCritical. You can also specify which button is selected by default with constants such as vbDefaultButton1 or vbDefaultButton2. Combine the constants with +, as in vbYesNo + vbQuestion + vbDefaultButton2. The first button is the default unless you specify another one.

Use icons to clarify the kind of message, not to imply a level of danger the situation does not warrant. A routine completion notice generally needs no dramatic warning icon. For a consequential action, choose a sensible default: making “No” the default can reduce accidental confirmation. A default button does not replace clear wording or a reliable Cancel path.

Reading the User’s Response

When you store the result, declare it as VbMsgBoxResult. The returned value is one of the named results corresponding to the buttons shown: vbOK, vbCancel, vbAbort, vbRetry, vbIgnore, vbYes, or vbNo. Compare against these constants rather than unexplained numbers such as 6 or 7. The available responses depend on the button group you selected.

For instance, a Yes/No dialog returns vbYes or vbNo; it does not return vbOK. If the user closes a dialog that has a Cancel button, the result is typically vbCancel. Design the code to handle every response the dialog offers, especially when continuing would modify or delete workbook data.

A Practical Excel Example

The following macro asks for confirmation before proceeding. It stores the result and performs the next step only after an affirmative response. The second message stands in for the work your own macro would do.

Before running any macro, save a copy of the workbook. VBA changes cannot be undone with Ctrl+Z, so a backup matters even when a macro only appears to show a dialog.

Sub ConfirmReportRun()
    Dim answer As VbMsgBoxResult

    ' Ask before starting the report procedure.
    answer = MsgBox("Run the report now?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm report")

    If answer = vbYes Then
        MsgBox "The report can now run.", vbInformation, "Ready"
    Else
        MsgBox "The report was not started.", vbInformation, "Cancelled"
    End If
End Sub

The underscore at the end of a line is VBA’s line-continuation character; it lets the argument list continue on the next line. The macro uses vbDefaultButton2, making No the default choice in this Yes/No dialog. The prompt should still describe the action plainly. If the operation is destructive, say what it will change and provide a way to cancel before making the change.

Making Messages Easier to Read

Keep a message concise and put the action or important result first. Long blocks of text are harder to scan in a small dialog. To separate a heading from details, concatenate strings with vbCrLf, the VBA constant for a carriage return and line feed. For example, a prompt can display “Import finished” on one line and the number of imported rows on the next.

When composing a prompt from a variable, convert values to text where appropriate and make units or context clear. A message saying “12” is less useful than one saying “12 rows imported.” Avoid using a message box for a progress update that must appear repeatedly during a long operation: because each dialog blocks the macro, repeated prompts can make a workbook difficult to use.

Common Problems and Fixes

  • The code will not compile: Check commas between arguments, matching parentheses, and quotation marks around text. If assigning the result, use the function-call form with parentheses.
  • The response is ignored: Confirm that the result is assigned to a variable and that the If statement compares it with a constant actually offered by the chosen buttons.
  • A line break does not appear: Join the text with vbCrLf, for example "First line" & vbCrLf & "Second line". In HTML, the ampersand shown in this example joins VBA strings.
  • The macro seems frozen: Look for a message box hidden behind another window or waiting for a click. A modal dialog pauses code until the user responds.
  • The macro does not run at all: Check that macros are permitted for the workbook and that the code is in a VBA procedure. A standard macro workbook is commonly saved as an Excel Macro-Enabled Workbook (.xlsm).

To open the VBA editor in Excel for Microsoft 365 on Windows, use Alt+F11 or choose Developer > Visual Basic. On Mac, the corresponding shortcut is generally Option+F11; some keyboards require Fn+Option+F11, depending on their function-key settings. Menu and shortcut behavior can vary by configuration. Excel for the web does not run VBA macros, so use the desktop application for this code.

Safety and Alternatives

A message box is only a prompt; it does not make a macro safe by itself. The code must check the response before taking action, and users should understand what they are confirming. Do not enable macros in an unfamiliar file just because it displays a reassuring message. Follow your organization’s macro-security rules and use trusted workbooks. Keep a separate copy before a procedure that edits or deletes data, since VBA actions are not reversed by Excel’s Undo command.

Choose another tool if the interaction needs more than a short message and a few buttons. VBA’s InputBox can ask for a small amount of text, while a custom UserForm can collect several inputs, validate them, and present a more structured interface. If the goal is simply to communicate status without stopping the user, an in-sheet status cell or another nonblocking display may be more appropriate. The right choice depends on whether the user needs to read, decide, or enter information.

Frequently Asked Questions

What is the syntax for MsgBox in VBA?
The basic form is MsgBox(prompt, [buttons], [title], [helpfile], [context]). Only the prompt is required. Use parentheses when assigning its return value, and omit them when calling it directly without Call and not using the result.

How do I make a Yes/No MsgBox in VBA?
Use vbYesNo in the buttons argument, optionally combined with an icon such as vbQuestion. Save the result in a VbMsgBoxResult variable and test for vbYes or vbNo.

How do I add a new line to a VBA MsgBox?
Join the parts of the prompt with vbCrLf. This inserts a line break in the dialog; it does not create a new worksheet row or change cell contents.

Can MsgBox return text typed by the user?
No. It returns a button choice, not typed text. Use VBA’s InputBox for a simple text prompt or a UserForm for more controlled input.

Can I use MsgBox in an Excel cell formula?
No. MsgBox is VBA code, not an Excel worksheet function. Put it inside a VBA procedure and run that procedure in desktop Excel; Excel for the web does not execute VBA macros.

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.