An Excel macro message box is a pop-up created with VBA’s MsgBox function. In Excel for Microsoft 365 on Windows, open the VBA editor with Alt+F11, choose Insert > Module, add a short macro that calls MsgBox, then run it with Alt+F8; save the workbook as an .xlsm file first.
What an Excel macro message box does
A message box displays a prompt or notice while a macro runs. It can show information, ask a simple question, or warn someone before an action. Depending on the buttons you specify, the user can dismiss the message or choose a response that the macro checks. For example, a macro might ask whether to refresh a report and continue only if the user selects Yes.
In VBA, MsgBox is a built-in function, not a worksheet formula. A typical call has this structure: MsgBox(prompt, buttons, title). The prompt is the main text, buttons controls which buttons and icon appear, and title supplies the text in the dialog’s title bar. The buttons and title are optional; a call with only a prompt displays a basic OK message.
Unlike a value entered in a cell, the pop-up is temporary and appears only when the macro runs. A standard message box is modal: the user must respond to it before continuing to interact with the workbook. That makes it useful for brief, important messages, but a poor choice for long instructions or information someone needs to keep visible.
Create and run a basic MsgBox macro
First save a copy of the workbook. In Excel for Windows, press Alt+F11 to open the Visual Basic Editor, then choose Insert > Module. Paste the following procedure into the module and return to Excel. With the cursor inside the procedure, press F5 in the editor, or close the editor and use Alt+F8, select ShowNotice, and choose Run.
Sub ShowNotice()
Dim message As String
' Set the notice shown to the user.
message = "The monthly report is ready."
MsgBox message, vbInformation, "Report status"
End Sub
The first line names the macro, and the Dim line declares a variable to hold the message text. The final call displays the text with an information icon and the title “Report status.” The procedure ends at End Sub. You can replace the message and title with text that fits the task, but keep quotation marks around literal text and ensure the VBA statements remain inside the procedure.
To run the macro from the worksheet without opening the macro list, you can add a button: show the Developer tab, choose Developer > Insert > Button (Form Control), draw the button on the sheet, and select the macro when Excel asks which one to assign. The button’s displayed label can be changed separately from the macro name. A Form Control button is often a simple, compatible way to give workbook users an obvious action to click.
Ask a question and use the response
For a Yes-or-No prompt, store the result of MsgBox in a variable of type VbMsgBoxResult. The macro can then test whether the result equals vbYes or vbNo. Here is a complete example that asks before refreshing workbook data:
Sub ConfirmRefresh()
Dim answer As VbMsgBoxResult
' Ask before starting the refresh.
answer = MsgBox("Refresh the report now?", vbYesNo + vbQuestion, "Confirm refresh")
If answer = vbYes Then
ThisWorkbook.RefreshAll
MsgBox "The refresh was started.", vbInformation, "Report"
Else
MsgBox "No changes were made.", vbInformation, "Cancelled"
End If
End Sub
vbYesNo requests Yes and No buttons, while vbQuestion adds a question icon. The plus sign combines button and icon options. The response check matters: displaying a Yes/No dialog alone does not make the macro act on the choice. In this example, selecting Yes calls ThisWorkbook.RefreshAll; selecting No skips that command. Depending on the workbook’s connections, a refresh may continue in the background, so “was started” is more accurate than claiming it has finished.
Other commonly used button constants include vbOKOnly, vbOKCancel, vbYesNoCancel, and vbRetryCancel. Common icons include vbInformation, vbExclamation, vbCritical, and vbQuestion. You can combine options, such as vbOKCancel + vbExclamation. A default-button option such as vbDefaultButton2 can change which button is initially selected; use it carefully, especially when the default could trigger a consequential action.
Choose buttons and write clear prompts
Match the dialog to the decision the person actually needs to make. An informational notice generally needs only OK. A reversible action might use OK and Cancel. A decision with two clear alternatives can use Yes and No. Avoid presenting buttons that do not lead to meaningfully different outcomes, and explain the consequence in the prompt rather than relying on the button labels alone.
- State the action: “Delete the selected rows?” is clearer than “Continue?”
- Make the consequence specific: If a choice overwrites a file or changes many cells, say so before the user confirms.
- Use a short title: Identify the workbook task, such as “Import check” or “Confirm refresh.”
- Keep the message concise: A message box is for a brief prompt, not a manual or a large report.
- Provide a safe route: For a potentially destructive action, make Cancel or No an understandable way to stop.
Text can be split across lines with the VBA constant vbCrLf. For example, concatenate a short heading, a line break, and a second sentence. This can make a compact message easier to scan, but many lines turn a small dialog into a cramped substitute for a proper form. Also remember that MsgBox buttons have fixed labels; VBA does not let you rename the standard Yes, No, OK, or Cancel buttons.
Save the workbook and handle macro security
A workbook containing VBA should normally be saved as an Excel Macro-Enabled Workbook (.xlsm). If you save a macro-containing file as .xlsx, Excel warns that the VBA project cannot be kept in that format; choose a macro-enabled format if you need the code later. A message box written in VBA will not run merely because its code is present: macros must be permitted for that file and environment.
Excel’s security controls are intended to reduce the risk of running untrusted code. Do not enable macros just to dismiss a warning when you do not know who supplied the workbook or what its code does. Use files from a trusted source, follow your organization’s policies, and ask an administrator about managed settings rather than weakening security globally. A trusted location or digitally signed macro may be appropriate in some organizations, but those options should follow the organization’s security process.
VBA macros cannot be undone with Ctrl+Z. If a macro changes cells, deletes content, refreshes data, or performs another action, the usual Excel undo history does not reverse the macro’s work. Save a separate copy before running unfamiliar code, and make a backup before using a macro that changes important data. A confirmation message can reduce accidental clicks, but it is not a substitute for a recoverable copy.
Common problems and how to diagnose them
- The macro does not appear in Alt+F8: Check that the code is a public procedure in a standard module, rather than code placed in a worksheet or workbook object. Confirm the workbook is saved in a macro-enabled format and that the procedure is not declared
Private. - Excel blocks the macro: Review the security notification and the file’s source. Do not bypass the block unless the workbook is trusted and your organization permits it. A file downloaded from the internet may receive additional protections in Windows.
- The dialog appears, but the macro ignores the answer: Make sure the return value is assigned to a variable and that the code compares it with a matching constant such as
vbYes. Check that theIfandElsebranches contain the intended actions. - The message text looks wrong: Check for missing quotation marks, misspelled variable names, or concatenation errors. In the VBA editor, choose Debug > Compile VBAProject to help identify compile errors in the project.
- The macro runs twice: If a worksheet button launches the procedure, check that the button has only one macro assigned and that the code does not call itself indirectly. Do not also run it manually while a long-running action is still in progress.
When code stops with an error, note the highlighted line and the exact error message before changing anything. A dialog can also appear behind another window, making it seem as if Excel has frozen; use the task switcher or bring Excel to the foreground to check for a waiting modal prompt. If the workbook is shared, test a copy with representative data before distributing a revised macro.
When MsgBox is not the right tool
Use MsgBox for a short notification or a small number of standard choices. If users need to enter text, select from a longer list, review several fields, or keep instructions visible while working, a VBA UserForm is usually more suitable. A UserForm can contain controls such as text boxes, labels, and buttons, and can provide a more structured interface than a fixed message box.
For a quick request for one text value, VBA’s InputBox function is another option, though it is still a simple dialog and needs validation: users can leave an entry blank or type a value in an unexpected format. Worksheet cells, data validation lists, and clear instructions may be more appropriate when inputs should remain visible or easy to review. For notifications that do not require a decision, a cell status area or a written note can avoid interrupting the user.
Excel for the web does not run VBA macros. A workbook may be opened there, but a VBA message box cannot be triggered in the browser. Excel for Mac supports VBA, although the editor, ribbon, security prompts, and keyboard shortcuts can differ from Windows; on some Mac keyboards, function keys require the Fn key. If a workbook must work consistently in a browser, consider an alternative such as Office Scripts where supported, or design the workflow around worksheet controls and features available to the intended users.
Frequently Asked Questions
How do I show a message box in an Excel macro?
Call VBA’s MsgBox function from a macro, for example MsgBox "Saved successfully", vbInformation, "Status". Put the code in a standard module, save the workbook as .xlsm, and run the macro with Alt+F8 or an assigned button.
Can an Excel message box have Yes and No buttons?
Yes. Use vbYesNo in the buttons argument, then store the result and test it against vbYes or vbNo. The macro must include conditional code if the choices are meant to cause different actions.
Can I change the text on MsgBox buttons?
No. The built-in MsgBox function uses standard button captions such as OK, Cancel, Yes, and No. Create a VBA UserForm if the interface needs custom labels or more elaborate choices.
Why does my macro message box not appear in Excel for the web?
Excel for the web does not execute VBA macros, so a VBA MsgBox cannot appear there. Open the workbook in a desktop version of Excel that supports VBA, or redesign the workflow with browser-compatible tools.
Can I undo a macro after clicking a message box button?
Usually not with Ctrl+Z: running VBA clears or bypasses Excel’s normal undo history. Save a backup before running a macro that changes data, and use a confirmation prompt to prevent mistakes rather than relying on undo.
Related
- Excel Table Styles: How to Apply and Customize Them
- Cut, Copy, and Paste in Excel: Shortcuts and Steps
- Excel: Highlight the Bottom 10 Values in Red
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Data Entry Form With a Picture Box: How It WorksYes. In Excel for Microsoft 365 on Windows, make a VBA UserForm, add an Image control to act…
- Excel VBA Font Color: Set, Read, and Fix ItIn Excel VBA, set a cell’s font color with the Font.Color property, usually by assigning an RGB value—for…
- VBA Loop Without Do: Choosing For, While, or For EachYes. In VBA, you can loop without using Do: use For...Next for a known number of repetitions, For…
- Excel Macro Not Working? Find and Fix Common CausesIf an Excel macro is not working, first confirm that the workbook is saved in a macro-enabled format,…
- VBA Range Address: Get, Set, and Understand ReferencesIn Excel VBA, use a range’s Address property to return its cell reference as text: for example, Range("B2:D5").Address…
- Excel VBA Not Working? Diagnose Common Macro ProblemsIf Excel VBA is not working, first check that you are using the desktop version of Excel, the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.