Excel VBA MsgBox: Syntax, Buttons, and Examples

VBA & macros · Updated November 18, 2026

Excel VBA’s MsgBox function displays a dialog box with a message, optional buttons and icon, and an optional title. Call it from a VBA procedure, then check its return value if you need your macro to respond to a choice such as Yes or No.

What MsgBox does in Excel VBA

MsgBox is a Visual Basic for Applications function, not an Excel worksheet formula. It pauses the running macro while the dialog is open and returns a value when the user selects a button or closes the dialog. A macro can use that result to decide what to do next.

For example, a message box can confirm a potentially disruptive action, report that an import finished, or explain why a macro cannot continue. It is useful for short, immediate interactions. It is not a good substitute for a worksheet or custom form when you need to collect several fields or keep a window open while the user works elsewhere.

MsgBox syntax and arguments

The VBA syntax is MsgBox(prompt, buttons, title, helpfile, context). Only prompt is required; the remaining arguments are optional. The prompt is the message text, while the other arguments control the buttons, icon, title, and optional help link.

  • prompt: The text to display. Join lines with vbCrLf when a message needs a line break.
  • buttons: A combination of constants that selects the buttons, icon, default button, and certain display options.
  • title: Text displayed in the dialog’s title bar. If omitted, VBA uses the application name.
  • helpfile and context: Optional help-file information. These are rarely needed in ordinary Excel macros.

For a simple notification, the call can be as short as MsgBox "The report is ready.". To make intent clearer, especially when setting an icon or buttons, specify the relevant constants rather than relying on defaults. The function returns a result, so assign it to a variable when the user’s choice matters.

Choosing buttons, icons, and a default

The buttons argument uses built-in VBA constants. Combine one button-set constant with an icon or default-button constant using +. Common choices include:

  • vbOKOnly, vbOKCancel, vbYesNo, and vbYesNoCancel choose the available buttons.
  • vbInformation, vbExclamation, vbQuestion, and vbCritical choose an icon.
  • vbDefaultButton1, vbDefaultButton2, vbDefaultButton3, and vbDefaultButton4 select which button is initially active.

For example, vbYesNo + vbQuestion + vbDefaultButton2 presents a question with Yes and No buttons and makes No the default. Choosing a safer default can reduce accidental confirmation, but it does not replace clear wording. Explain what the action will do, particularly if it changes or removes data.

Do not assume the buttons appear in an identical order on every operating system or language configuration. Write your code to test the returned constant, not to infer the answer from a button’s screen position.

How to check the user’s response

The return value is a VbMsgBoxResult. Depending on the buttons shown, it can be vbOK, vbCancel, vbYes, vbNo, vbRetry, vbIgnore, or vbAbort. Compare it with the matching named constant. Avoid checking a numeric value such as 6: the named constant makes the code easier to read and maintain.

When the buttons matter, store the result in a variable declared as VbMsgBoxResult. The following procedure asks before continuing. Its second message stands in for the action; it does not actually refresh a workbook.

Sub ConfirmRefresh()
    Dim answer As VbMsgBoxResult

    ' Ask before continuing; No is the default choice.
    answer = MsgBox("Continue with the refresh?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm refresh")

    If answer = vbYes Then
        MsgBox "The macro can continue here.", vbInformation, "Confirmed"
    Else
        MsgBox "No changes were requested.", vbInformation, "Cancelled"
    End If
End Sub

The code handles both possible answers explicitly. If you offer a Cancel button as well, handle vbCancel separately rather than treating every result other than Yes as the same outcome. That distinction matters when a macro has several branches or must clean up temporary work before stopping.

Writing a useful message and adding line breaks

Keep the prompt direct and specific. “Continue?” gives little context; “Replace the existing monthly report with the imported data?” tells the reader what is at stake. Use a concise title to identify the task, and avoid presenting routine progress as a question that seems to require a decision.

To display multiple lines, concatenate text with vbCrLf. The next example shows a summary with a variable value. In a real macro, the value could be set from the number of rows processed.

Sub ShowImportSummary()
    Dim importedCount As Long
    Dim prompt As String

    ' Example count for the message.
    importedCount = 12
    prompt = "Import finished." & vbCrLf & _
             "Rows added: " & importedCount

    MsgBox prompt, vbInformation, "Import complete"
End Sub

In VBA code, the concatenation operator is the ampersand character. In this HTML display it appears as &. Use vbCrLf for a conventional new line; do not insert a worksheet formula or a literal HTML line-break tag into the VBA prompt.

Insert and run a MsgBox macro

In Excel for Microsoft 365 on Windows, save a copy of the workbook first, then press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module, paste a complete procedure into the module, and return to Excel. To run a simple test macro, press Alt+F8, select its name, and choose Run. The Developer tab also provides access to Visual Basic and the macro tools.

On Excel for Mac, open the Visual Basic Editor from Developer > Visual Basic; the keyboard shortcut is commonly Option+F11, and some keyboards require Fn as well. The VBA editor is available in desktop Excel for Mac, although certain Windows-specific automation features and APIs are not portable. Excel for the web cannot run VBA macros; use desktop Excel to run this code.

Save a macro-enabled workbook as an Excel Macro-Enabled Workbook (.xlsm) if you need to retain the VBA project. If you save as a regular .xlsx file, VBA code is not retained. A message box itself does not require a special file format, but the procedure that calls it does.

Common problems and their fixes

  • The macro does not run: Check that the code is inside a procedure in a standard module and that you are using desktop Excel. Review the workbook’s macro security prompt and only enable macros for files you trust.
  • The code pauses at the dialog: That is normal. A standard MsgBox is modal: the user must dismiss it before the macro continues.
  • The wrong branch runs: Store the return value and compare it with a constant such as vbYes or vbNo. Do not compare the displayed label or assume button positions.
  • The title or prompt is missing: Check the order of positional arguments. The title is the third argument, after the prompt and buttons. For complicated calls, named arguments can make the intended argument clearer.
  • The dialog appears at an inconvenient time: A message box can interrupt a sequence of automated work. Use one only when a person needs to make a decision or see a meaningful status; avoid displaying one for every row in a loop.

Remember that VBA macros cannot be undone with Excel’s Ctrl+Z command. A confirmation dialog can prevent an accidental action, but it is not a backup or rollback mechanism. Save a separate copy before running a macro that edits, deletes, or replaces workbook content.

When to use a UserForm instead

A MsgBox is suitable for a brief message and a small, standard set of choices. Its controls are limited to the built-in button combinations; you cannot add a text-entry field, a custom list, or a complex layout to the standard dialog. Use an InputBox when you need one simple text response, or create a VBA UserForm when the interaction needs several controls, validation, or a custom design.

A regular message box also blocks the macro until the user responds. If a task needs a window that remains available while other work continues, a modeless UserForm is a better fit. Do not assume that a different MsgBox button setting makes it modeless: the standard function is intended for a response before execution proceeds.

Frequently Asked Questions

How do I make a Yes or No MsgBox in Excel VBA?
Use vbYesNo in the buttons argument, store the returned value, and test it against vbYes or vbNo. Add an icon such as vbQuestion if it helps clarify the prompt.

How do I change the title of a VBA message box?
Pass the title as the third argument: the order is prompt, buttons, then title. For example, MsgBox "Saved.", vbInformation, "Workbook status" displays “Workbook status” in the title bar.

Can I put a line break in a VBA MsgBox?
Yes. Join parts of the prompt with vbCrLf, for example "First line" & vbCrLf & "Second line". This is useful for a short heading followed by a result or detail.

Can I use MsgBox in an Excel cell?
No. MsgBox is VBA code, not a worksheet function, so it cannot be entered into a cell formula. A macro or other VBA procedure must call it.

Why does MsgBox stop my macro?
A standard message box is modal. VBA waits for the user to dismiss it and returns the selected button’s result before continuing to the next statement.

Does VBA MsgBox work in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel for Windows or Mac to run a VBA procedure that displays a message box.

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.