Excel VBA Message Box: How to Use MsgBox in Macros

VBA & macros · Updated November 2, 2026

Excel VBA Message Box: How to Use MsgBox in Macros

In Excel VBA, use the MsgBox function to show a message, optionally display buttons or an icon, and return the button the user clicked. For example, MsgBox "Report ready", vbInformation, "Status" displays an information box titled Status; the sections below explain how to tailor it and respond to a choice.

What an Excel VBA message box does

A message box is a small dialog that interrupts a macro and asks the person using the workbook to read a prompt or make a simple choice. It is useful for confirming an action, warning about a condition, or reporting that a task has finished. A message box is not a worksheet cell, comment, or form control: it appears temporarily, and the macro generally pauses until the user clicks a button.

VBA calls this function MsgBox. Its basic syntax is MsgBox(prompt, buttons, title), with the buttons and title arguments optional. The prompt is the message text. The buttons argument controls the available buttons, icon, default button, and some display behavior. The title appears in the dialog’s title bar. If you omit the title, Excel or the host application supplies a default caption.

Use a message box for short, immediate communication. It is usually a poor fit for lengthy instructions, data entry, or a choice with many options. For those needs, a worksheet, UserForm, or input dialog can give users more space and control.

Insert and run a simple MsgBox macro

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and add a procedure. You can also open the editor from the Developer tab by selecting Visual Basic. To run a procedure while the editor is active, place the cursor inside it and press F5. The Developer tab may need to be enabled through File > Options > Customize Ribbon.

On Mac, use Option+F11 to open the editor on many keyboards; if the function keys are configured for hardware controls, you may need Fn as well. The Visual Basic Editor and VBA are available in desktop Excel for Mac, though menus and keyboard behavior differ from Windows. Excel for the web cannot create, edit, or run VBA macros; open the workbook in the desktop application instead.

Before running any macro, save a copy of the workbook. Macro actions cannot be undone with Ctrl+Z, even if a later message box reports a problem. This simple example only displays a message:

Sub ShowNotice()
    ' Display a completion message.
    Dim messageText As String

    messageText = "The report is ready."
    MsgBox messageText, vbInformation, "Report status"
End Sub

The first argument is the prompt, vbInformation selects an information icon, and the last argument sets the caption. You can replace the text and title with values relevant to the workbook. A macro can also build the prompt from a variable or a cell value, but check that the value exists and is readable before displaying it.

Choose buttons, icons, and the default choice

The buttons argument combines VBA constants. Common button constants include vbOKOnly, vbOKCancel, vbYesNo, vbYesNoCancel, vbRetryCancel, and vbAbortRetryIgnore. Common icon constants are vbInformation, vbQuestion, vbExclamation, and vbCritical. You can combine one button constant and one icon constant with Or, for example vbYesNo Or vbQuestion.

The default button is the one initially selected for keyboard activation. Constants such as vbDefaultButton1, vbDefaultButton2, and vbDefaultButton3 let you choose it. Combine a default-button constant with the other options using Or. Be especially careful when setting a default for a destructive action: a safe, non-destructive choice should normally be selected initially. Do not use an icon as a substitute for clear wording.

  • Use OK for a simple notice that needs no decision.
  • Use Yes and No only when the prompt makes both outcomes clear.
  • Use Cancel when the user needs a straightforward way to leave without proceeding.
  • Choose an icon that supports the message: an error icon for an actual error, not merely to make a routine notice look important.

These constants are built into VBA, so it is clearer to use their names than to memorize numeric button values. If you combine incompatible options or set a default-button number that does not correspond to a displayed button, the dialog may not behave as intended. Keep the button set small and make the consequences of a choice explicit in the prompt.

Capture the button the user clicked

MsgBox can be used as a statement when the response does not matter, or as a function whose result is stored and tested. The result is a value from the VbMsgBoxResult enumeration, such as vbYes, vbNo, vbOK, or vbCancel. Compare the result with these named constants rather than hard-coded numbers.

For example, the following macro asks before continuing. Save a copy of the workbook before running it, as you should before any macro; this example itself only displays a dialog.

Sub ConfirmAction()
    ' Continue only when the user chooses Yes.
    Dim response As VbMsgBoxResult

    response = MsgBox("Continue with the next step?", _
                      vbYesNo Or vbQuestion Or vbDefaultButton2, _
                      "Confirm")

    If response = vbYes Then
        MsgBox "The next step can begin.", vbInformation, "Confirmed"
    Else
        MsgBox "No changes were requested.", vbInformation, "Cancelled"
    End If
End Sub

Here, the second button is the default, so No is initially selected. That is a cautious choice when Yes would start an action. The code still checks explicitly for vbYes; the alternative branch handles No. If you use a three-button set, test for every response that has a distinct meaning, including Cancel where appropriate. A message box cannot infer what the user intended from the prompt, so the macro must implement each outcome.

Format longer prompts with line breaks

A prompt can contain a line break using vbCrLf. This can make a short warning or summary easier to scan. For example, a macro could join a heading and a second line with vbCrLf, or add a count from a variable. Avoid putting a whole procedure manual into the dialog: long text is difficult to read in a compact box and may not fit as expected on different displays.

For text assembled across several lines of VBA, use the line-continuation character, an underscore preceded by a space. In the confirmation example above, the underscore continues the function call on the next source line; it does not add a line break to the message. Use vbCrLf in the prompt itself when the displayed text needs a new line. Test dynamic text for blank values and unexpected characters so the resulting notice remains understandable.

VBA also offers options such as vbMsgBoxRight and vbMsgBoxRtlReading for right-aligned or right-to-left text. These are specialized display options, not replacements for choosing an appropriate language and writing a concise prompt. The familiar title argument can identify the workbook task or decision, but avoid a generic caption if it leaves the user unsure which process is asking.

Where macros run and what security to expect

VBA message boxes work in desktop Excel for Windows and Mac, subject to macro availability and security settings. A workbook with macros is commonly saved as an Excel Macro-Enabled Workbook (.xlsm); saving a macro workbook as a macro-free format can remove its VBA project. Excel for the web does not run VBA, so a MsgBox procedure will not appear there as an interactive prompt.

Excel may block macros in a file, particularly when it came from an untrusted location or the internet. Do not lower macro security just to make a message box appear. Only enable macros when you trust the workbook and its source, and follow your organization’s policy. A message box is not a security feature: it cannot make an unsafe macro safe, authenticate a user, or protect confidential information displayed on screen.

If a workbook is shared with people who use different platforms, test the workflow in the desktop versions they use. A standard MsgBox is broadly supported, but editor shortcuts, ribbon layout, macro permissions, and some surrounding workbook features can vary. If users need to run the workbook in a browser, choose a non-VBA approach such as worksheet instructions or a supported Office Scripts workflow instead.

Common problems and how to fix them

  • The macro does not run: Confirm the file is open in desktop Excel, the workbook format retains macros, and the macro is permitted by the current security settings. Check that the procedure is in a standard module and that it is a runnable public Sub without required arguments.
  • The dialog appears but the macro takes the wrong branch: Store the result in a variable of type VbMsgBoxResult and compare it with the matching constant, such as vbYes. Do not compare the result with the displayed text; the function returns a button result.
  • The wrong button is selected initially: Review the constants joined in the buttons argument. The default-button number refers to the button order in the selected set, so choose a valid default and make the safer response the default when practical.
  • The message is hard to read: Shorten the prompt, add a deliberate vbCrLf line break, and use a meaningful title. For more detailed guidance, show information on a worksheet or in a UserForm.
  • Changes cannot be undone: Ctrl+Z does not reverse VBA changes. Save a separate copy before running unfamiliar code, and ensure important actions have sensible confirmation and error handling.

A dialog can also seem to “freeze” Excel because it is modal: the user must dismiss it before interacting with the workbook window. That is expected for an ordinary MsgBox and can be useful when a decision is required. It is a poor choice for status updates that should not interrupt work. For long-running procedures, consider a status-bar message or a modeless UserForm, and make sure any status indicator is cleared when the procedure finishes.

MsgBox compared with other ways to communicate

Choose the tool according to whether the user needs to read, decide, or enter information. MsgBox is for a brief message and a limited set of button responses. VBA’s InputBox function asks the user to type a value, while Excel’s Application.InputBox can request a particular type of input, such as a range. These input methods are not interchangeable with MsgBox, which does not collect typed text.

For instructions that should remain visible, place them on a worksheet. For a custom dialog with multiple fields, validation, or a more tailored layout, use a VBA UserForm. For a non-blocking progress update, the Excel status bar may be more appropriate. Each alternative has different setup and behavior; a simple message box is often best when one short question is enough.

There is also an important difference between a VBA message box and Excel’s worksheet interface. A MsgBox is generated by running code, not by entering a formula in a cell. It will not automatically recalculate or appear when a cell changes unless a suitable VBA event or procedure calls it. Avoid triggering repeated dialogs from events that fire often, such as every worksheet selection change, because frequent interruptions quickly make a workbook frustrating to use.

Frequently Asked Questions

How do I create a message box in Excel VBA?
In a VBA procedure, call MsgBox with a prompt, optional button settings, and an optional title. For instance, MsgBox "Saved", vbInformation, "Status" displays a short information message.

How do I make a Yes or No message box?
Use vbYesNo in the buttons argument, usually combined with an icon such as vbQuestion. Store the function result and test it against vbYes or vbNo so the macro can take the appropriate path.

Can a VBA message box return what the user clicked?
Yes. Assign the result of MsgBox to a variable of type VbMsgBoxResult. The result identifies a button, such as OK, Yes, No, or Cancel; it does not contain text typed by the user.

Can I use MsgBox in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel to run the macro, or use an approach supported in the browser if the workflow must work online.

Why is my Excel VBA message box not appearing?
Check that you are using desktop Excel, that the workbook contains VBA code, and that macros are allowed for that trusted file. Also verify that the procedure was started and that its code path actually reaches the MsgBox call.

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.