In VBA, format a MsgBox by building its prompt text with string concatenation and line-break constants, then set its title, buttons, icon, and default button with the optional arguments. For example, use vbCrLf to put text on separate lines; the built-in dialog does not support rich text, custom fonts, colors, or arbitrary positioning.
What MsgBox formatting means
MsgBox is a built-in VBA function that displays a standard dialog. Its appearance is largely controlled by Windows or the Office host: VBA can supply the words and choose from predefined button and icon styles, but it cannot style individual words like a web page or change the dialog’s dimensions directly. The dialog normally waits for the user to choose a button before the macro continues.
That distinction helps set expectations. “Formatting” for a message box usually means making its text readable, adding a title, choosing suitable buttons and an icon, and deciding which button is selected by default. It does not mean setting bold or colored text, inserting a table, or positioning controls precisely.
Basic syntax and the title argument
The general syntax is MsgBox(prompt, buttons, title, helpfile, context). Only prompt is required. The remaining arguments are optional and are supplied in order: the button and icon settings, the title-bar text, and, if applicable, help-file information. A short example is:
Sub ShowImportStatus()
Dim messageText As String
Dim customerName As String
' Save a copy of the workbook before running a macro.
customerName = "Asha"
messageText = "Import complete for " & customerName & "."
MsgBox messageText, vbInformation, "Import status"
End Sub
The first argument is the body of the dialog, the second selects an information icon, and the third supplies the title. The title is not a heading inside the prompt; it appears in the dialog’s title bar. If the title is omitted, VBA uses a default title, which may not explain what the message concerns. A descriptive title such as “Import status” is generally more useful than a generic “Message”.
In VBA, & joins strings. Prefer it for assembling prompt text rather than using +, which can also perform arithmetic and may produce confusing results when values are not strings.
Add line breaks and organize the message
Use vbCrLf to start a new line in the prompt. Two consecutive line breaks create a blank line, which can separate a short summary from details. Keep each line concise: message boxes are intended for brief notices, and long text can be harder to scan or may require scrolling depending on the host and display.
Sub ShowReviewNotice()
Dim messageText As String
' Save a copy of the workbook before running a macro.
messageText = "Review finished." & vbCrLf & _
"Rows checked: 24" & vbCrLf & _
"Rows needing attention: 2"
MsgBox messageText, vbExclamation, "Review results"
End Sub
The underscore at the end of a VBA line continues the statement onto the next line; it is not a line break in the dialog. The actual displayed line breaks come from vbCrLf. You can also use vbTab to insert a tab, but tab stops and spacing are not a dependable way to make columns align in a standard dialog. For tabular data, use a worksheet or a form instead.
When a prompt includes a quotation mark as part of the displayed text, double it inside a VBA string literal. For example, "Select ""Continue"" to proceed." displays the word Continue in quotation marks. This is a VBA string-literal rule, not a special MsgBox formatting feature.
Choose buttons, icons, and the default choice
The buttons argument combines predefined constants with the Or operator. A constant can specify the button set, icon, default button, and certain other options. Common choices include:
vbOKOnly,vbOKCancel,vbYesNo, andvbYesNoCancelfor button sets.vbInformation,vbExclamation,vbQuestion, andvbCriticalfor standard icons.vbDefaultButton1,vbDefaultButton2, and, where the chosen set has enough buttons,vbDefaultButton3orvbDefaultButton4for the initially selected button.
For example, vbYesNo Or vbQuestion Or vbDefaultButton2 displays Yes and No buttons with a question icon and makes the second button the default. A default button is the one initially selected; it does not remove the other choice. Pick the button set to match the action. If an operation is irreversible, avoid making the action itself the effortless default.
Icons communicate a broad category, not a custom message. Use an information icon for a routine status, an exclamation icon for a warning, and a critical icon for a serious error. A question icon is available, but the wording should still state clearly what the user is deciding. Icons alone are not enough to explain consequences.
Read the user’s response
A message box can return a value, so a macro can take different paths based on the button the user clicked. Store the result in a variable declared as VbMsgBoxResult and compare it with named constants such as vbYes, vbNo, or vbCancel. Named constants make the decision easier to understand than comparing with numeric return values.
Sub ConfirmRefresh()
Dim answer As VbMsgBoxResult
' Save a copy of the workbook before running a macro.
answer = MsgBox("Refresh the report now?", _
vbYesNo Or vbQuestion Or vbDefaultButton2, _
"Confirm refresh")
Select Case answer
Case vbYes
MsgBox "Refresh selected.", vbInformation, "Report"
Case vbNo
MsgBox "No changes made.", vbInformation, "Report"
End Select
End Sub
This example makes No the default because it is the second button, which can be a sensible choice when the prompt asks whether to start an action. The follow-up message boxes are shown just to illustrate branching; in a real macro, the Yes case would normally call the refresh procedure rather than display another notice. A vbYesNo dialog does not offer Cancel, so do not write code that expects vbCancel from that button set. Use a set that actually includes Cancel if the user needs that option.
Format dates, numbers, and other values
Values joined into a prompt may need explicit formatting. Use Format$ when a date or number needs to be readable in a particular pattern, rather than relying on an implicit conversion. The format pattern affects the displayed string; it does not change the underlying cell value.
Sub ShowInvoiceSummary()
Dim dueDate As Date
Dim amount As Currency
Dim messageText As String
' Save a copy of the workbook before running a macro.
dueDate = DateSerial(2026, 11, 15)
amount = 1250.5
messageText = "Due date: " & Format$(dueDate, "mmmm d, yyyy") & vbCrLf & _
"Amount: " & Format$(amount, "$#,##0.00")
MsgBox messageText, vbInformation, "Invoice summary"
End Sub
Here, the date displays as November 15, 2026, and the amount as $1,250.50. The dollar sign is literal in the chosen pattern, so it may not be suitable for workbooks used in other currencies. Choose a currency label or format appropriate to the workbook’s audience, and remember that date and number conventions can vary by locale. If the value comes from a worksheet cell, read it into a suitable variable and format it deliberately before joining it to the prompt.
Limits and alternatives to MsgBox
A standard MsgBox cannot apply bold, italic, font-size, or color formatting to part of the prompt. It cannot add custom buttons with your own labels, embed worksheet ranges, or provide a multi-field data-entry layout. You can make plain text clearer with a title, short sentences, line breaks, and a suitable button set, but there is no supported VBA argument that turns the standard dialog into a fully designed interface.
Choose another interface when the task needs more control:
- Use a VBA UserForm for custom labels, text boxes, buttons, layout, and validation.
- Use
Application.InputBoxwhen the user needs to enter or select a value and its type or range matters. - Use worksheet cells for longer instructions, tables, or content users may need to revisit.
- Use the status bar for brief progress information that should not interrupt the user’s work.
Excel for the web does not run VBA macros. In Excel for Microsoft 365 on Windows, MsgBox is available in VBA; Excel for Mac also supports VBA message boxes, though the operating system and Office host can make the dialog’s visual appearance differ. Do not depend on pixel-identical rendering between platforms.
Common formatting and behavior problems
If the message appears as one long line, check that the string actually includes vbCrLf between the sections. If a variable appears without the expected number of decimal places or date style, format it explicitly before concatenating it. If the title is missing or appears in the prompt, check the argument order: the button settings come before the title.
Another frequent issue is showing buttons without checking the result. Calling MsgBox as a statement is fine for a notification where any click simply dismisses it. If the choice affects what the macro does, assign the return value and handle each relevant response. Also ensure that the buttons you display match the cases in your code; a Yes/No dialog cannot return Cancel.
Keep modal dialogs for moments when a user genuinely needs to notice or answer something. A message box pauses the procedure, and repeated pop-ups can interrupt work. For progress updates during a lengthy task, a status indicator or modeless form is usually more appropriate. Before running a macro that changes a workbook, save a separate copy: VBA changes generally cannot be reversed with Ctrl+Z.
Frequently Asked Questions
Can I make part of a VBA MsgBox bold or change its color?
No. The built-in dialog supports plain prompt text and predefined icons and buttons, but not rich-text formatting. Use a UserForm if custom styling is necessary.
How do I put a blank line in a MsgBox?
Join two line breaks into the prompt, for example "First section" & vbCrLf & vbCrLf & "Second section". The repeated vbCrLf creates an empty line between the text sections.
How do I change the MsgBox title?
Pass the title as the third argument: MsgBox "Saved successfully.", vbInformation, "Save status". The title appears in the title bar, while the first argument is the message body.
How do I make No the default button?
For a Yes/No dialog, combine vbYesNo with vbDefaultButton2, such as vbYesNo Or vbQuestion Or vbDefaultButton2. The second button is No in that button set.
Can MsgBox display a custom button label?
No. Its button captions come from the built-in button sets and the user’s Office or operating-system language. Use a UserForm when the buttons need specific wording.
Does MsgBox work in Excel for the web?
No. Excel for the web does not execute VBA macros, including code that calls MsgBox. Open a macro-enabled workbook in the desktop Excel application to run its VBA.
Related
- Power Query Remove Leading Zeros Without Losing IDs
- Power Query Reference: What It Does and When to Use It
- Excel Data Validation: Can You Select Multiple Items?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: How to Use Not IsErrorIn Excel VBA, write If Not IsError(value) Then to test whether a value is not an Excel error…
- VBA Macro Error Handling: Patterns, Examples, and FixesVBA macro error handling lets a procedure respond to runtime problems instead of stopping without explanation. Use On…
- Excel VBA String to Number: Safe Conversion MethodsIn Excel VBA, convert text that represents a number with a conversion function such as CDbl for decimals…
- Excel VBA Copy and Paste a Range to Another WorksheetTo copy a range to another worksheet with Excel VBA, identify the source and destination worksheets, then use…
- Excel VBA: Delete Rows Based on a Cell Value MatchTo delete worksheet rows when a cell matches a value, loop upward from the last used row and…
- Excel VBA ComboBox: Load a UserForm List From RangeTo fill an Excel VBA UserForm ComboBox from cells, set its RowSource property to a worksheet range, or…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.