In VBA, a MsgBox style is the combination of buttons, an icon, the default button, and other display options passed to the MsgBox function. For example, vbYesNo Or vbQuestion Or vbDefaultButton2 displays Yes and No buttons, a question-mark icon, and makes No the default choice.
What the MsgBox style controls
A message box gives a macro a simple way to show information or ask for a decision. Its style argument determines which standard buttons appear, what icon is shown, and which button is initially selected. The message text and optional title are separate arguments, so changing the style does not change the wording or window title.
The general syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is required; the other arguments are optional. The second argument, commonly called buttons or style, accepts one or more VbMsgBoxStyle constants combined with the VBA Or operator. If you omit it, VBA uses the default style: an OK button, no special icon, and the first button as the default.
The name “style” can be misleading: it does not let you set a custom font, color, or layout. It selects from the options provided by the built-in message box. For a branded or more interactive interface, use a UserForm instead.
Button and icon constants
Use named constants instead of memorizing numeric values. The button group and icon group are designed to be combined, so a style can specify one choice from each group.
vbOKOnlydisplays OK;vbOKCanceldisplays OK and Cancel.vbAbortRetryIgnoredisplays Abort, Retry, and Ignore;vbRetryCanceldisplays Retry and Cancel.vbYesNodisplays Yes and No;vbYesNoCanceldisplays Yes, No, and Cancel.vbCriticaldisplays the critical/error icon;vbExclamationdisplays the warning icon.vbInformationdisplays the information icon;vbQuestiondisplays the question icon.
For example, vbOKCancel Or vbExclamation combines an OK/Cancel choice with a warning icon. vbInformation by itself selects an icon while leaving the button choice at its default, OK. Constants such as vbOKOnly and vbApplicationModal have a value of zero; they can still be used to make the intended choice clear in code.
Default buttons and other options
A default button is the one that initially has keyboard focus and is generally activated by pressing Enter. Use vbDefaultButton1, vbDefaultButton2, vbDefaultButton3, or vbDefaultButton4 to select its position in the displayed button set. The numbering refers to the buttons from left to right, not to a specific label. A two-button Yes/No box with vbDefaultButton2, for example, defaults to No.
Other style constants cover less common needs:
vbApplicationModalis the default. The user must respond to the message box before continuing in that application.vbSystemModalrequests a system-modal box, which blocks interaction with other applications until dismissed. Use this sparingly; an ordinary application-modal prompt is usually less disruptive.vbMsgBoxHelpButtonadds a Help button when the help-file and context arguments are supplied.vbMsgBoxRightright-aligns the text, andvbMsgBoxRtlReadingrequests right-to-left reading order.
These options can be joined with the button, icon, and default-button constants using Or. Help-related arguments are separate optional arguments in the function call; the style constant alone does not provide help content.
How to combine styles in VBA
In the Visual Basic Editor, a style can be written directly in the call. When the user’s response matters, assign the return value to a variable and compare it with the matching vb result constant. This is safer than testing the visible button label as text.
Before running any macro, save a copy of the workbook. VBA actions generally cannot be undone with Ctrl+Z, even if a particular example only displays a message box.
Sub ConfirmReport()
Dim answer As Integer
' Ask before proceeding; No is the default choice.
answer = MsgBox("Continue with the report?", _
vbYesNo Or vbQuestion Or vbDefaultButton2, _
"Confirm report")
If answer = vbYes Then
MsgBox "The report can continue.", vbInformation, "Confirmed"
Else
MsgBox "The report was not started.", vbInformation, "Cancelled"
End If
End Sub
In this example, answer receives a numeric result. The first box combines three choices: the Yes/No buttons, the question icon, and the second button as the default. The title is the third argument, so it follows the style argument. The second message box uses an information icon and the default OK button.
Read the response, not just the prompt
The MsgBox function returns a value that identifies which button the user selected. Common results are vbOK (1), vbCancel (2), vbAbort (3), vbRetry (4), vbIgnore (5), vbYes (6), and vbNo (7). The available results depend on the buttons you displayed.
A prompt is not a substitute for handling the response. If a macro displays Yes and No but then continues regardless of the answer, the user’s choice has no effect. Branch on the returned value when the decision changes what the macro does. For a simple notification with only OK, it is often sufficient to call MsgBox without assigning its result.
VBA syntax differs slightly depending on whether you use the function result. To ignore it, a call can be written as a statement, such as MsgBox "Saved", vbInformation, "Status". To capture the result, use parentheses around the arguments on the right side of the assignment, as in answer = MsgBox("Continue?", vbYesNo, "Confirm").
Choosing a style that fits the situation
Choose buttons based on the actual decision the macro can handle. A Yes/No prompt is suitable for a binary choice; add Cancel only when the user needs a genuine way to back out without choosing either outcome. Avoid offering buttons whose actions are unclear or identical.
- Use
vbInformationfor a completion notice or neutral status message. - Use
vbExclamationto draw attention to a caution, such as an action that may overwrite data. - Use
vbCriticalfor a serious failure, not as decoration for routine messages. - Use
vbQuestionwhen the prompt is asking the user to make a choice. - Set a non-destructive option as the default when an accidental Enter press could have consequences.
Write a concise prompt that describes the action and, where useful, its consequence. “Delete the selected rows?” is less informative than “Delete the 12 selected rows? This cannot be undone.” The second version gives the user meaningful context before they choose.
Common mistakes and limitations
A frequent mistake is using a button constant where an icon constant was intended, or vice versa. The result may still run because the style argument is numeric, but the displayed box may not match the intention. Named constants and a quick check of each constant’s purpose make the code easier to review.
Another mistake is treating the style as a single named preset. It is usually a combination of separate options: buttons, icon, default button, and sometimes modality or text direction. Use Or to combine those options. The numeric constants are bit flags; adding arbitrary numbers or using unexplained numeric literals makes the code harder to understand and can produce unintended combinations.
Also remember that vbDefaultButton2 does not mean “make No the default” in every message box. It means make the second displayed button the default. If the order or number of buttons changes, the default changes accordingly. Finally, MsgBox has a fixed, operating-system-controlled appearance. It cannot provide custom button captions, extra text fields, or a custom layout.
MsgBox versus a UserForm
MsgBox is convenient for short notices and straightforward decisions because it requires little code and uses familiar standard controls. It is not suitable for collecting a name, date, or other typed input; for that, VBA also provides the InputBox function, or a UserForm when validation and a richer interface are needed.
A UserForm is the better fit when you need custom button labels, several fields, explanatory text, or controls such as check boxes and drop-down lists. It takes more setup than MsgBox, but it gives the developer control over the interface and how responses are validated. Choose the simpler message box when its limited choices accurately express the task.
Frequently Asked Questions
What is the default VBA MsgBox style?
If you omit the style argument, VBA displays an OK button with no special icon. The message box is application-modal, and the first button is the default.
How do I show Yes and No buttons with an icon?
Combine vbYesNo with an icon constant using Or. For example, vbYesNo Or vbQuestion displays Yes and No with a question icon. Add a default-button constant if you need to choose the initial selection.
How can I tell which MsgBox button was clicked?
Assign the result of MsgBox to a variable, then compare it with a return constant such as vbYes or vbCancel. The function returns a number representing the selected button.
Can I change the text on MsgBox buttons?
No. MsgBox uses standard button captions such as OK, Cancel, Yes, and No. Use a VBA UserForm if the interface requires custom captions or controls.
Does a MsgBox macro work the same in Excel for Mac?
The VBA MsgBox function and its standard style constants are part of VBA, but the box’s appearance can reflect the operating system. Excel for the web does not run VBA macros, so a VBA MsgBox will not run there.
Related
- Microsoft Excel Always Not Responding? Causes and Fixes
- Merge Cells but Keep All Data in Excel: What Works
- Merge Cells in Excel: How to Keep All Your Data
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Not Equal: Operators, Examples, and FixesIn Excel VBA, use the <> operator to test whether two values are not equal. For example, If…
- Excel Goal Seek Macro: VBA Code and How to Run It SafelyAn Excel Goal Seek macro is VBA code that asks Excel to change one input cell until a…
- VBA Send Email From a Different Address in OutlookYes. In classic Outlook for Windows, VBA can send a message from another address when that address is…
- Excel VBA Programming: What It Is and How to Use ItExcel VBA programming means using Visual Basic for Applications, Excel’s built-in programming language, to automate workbook tasks and…
- Excel VBA: Find All Cells With a ValueTo find every cell that matches a particular value in Excel VBA, use the Range.Find method and repeat…
- Excel VBA Get Current Workbook Name: Code and TipsTo get the name of the workbook currently active in Excel VBA, use ActiveWorkbook.Name. For example, assign Set…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.