Excel VBA MsgBox Parameters: A How-To Guide
In this article
A VBA MsgBox displays a dialog box with a message and optional buttons, icons, and a title. The parameters control exactly what appears: the prompt text, the button and icon combination, the window title, and help file context. You can call it as a simple statement (MsgBox "Hello") or as a function that returns which button the user clicked. This guide walks through each parameter, how to combine them, and how to handle the return value.
What You'll Need
- Microsoft Excel (any recent version) with the Developer tab enabled, or the ability to open the VBA editor with Alt+F11.
- A workbook saved as .xlsm (macro-enabled) if you want to keep your code.
- Basic familiarity with writing a Sub in a standard module.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (Mac). In the Project Explorer, right-click your workbook, choose Insert > Module. This is where you will write the code.
- Write a minimal MsgBox. Type
Sub ShowMessage()thenMsgBox "Hello, world!"andEnd Sub. Press F5 to run. A dialog appears with an OK button and no title. This uses only the first parameter, Prompt. - Add the Buttons parameter. The second argument, Buttons, is a number that combines button type, icon, default button, and modality. For example,
MsgBox "Continue?", vbYesNoshows Yes and No buttons. Common button constants:vbOKOnly(0),vbOKCancel(1),vbYesNoCancel(3),vbYesNo(4),vbRetryCancel(5). - Add an icon. Combine the button constant with an icon constant using
+orOr. For example,vbYesNo + vbQuestionshows Yes/No with a question mark. Other icons:vbCritical,vbExclamation,vbInformation. The icon is purely visual; it does not change behavior. - Set a default button. Add
vbDefaultButton1,vbDefaultButton2, orvbDefaultButton3to make a specific button the default when the dialog opens. For example,vbYesNo + vbQuestion + vbDefaultButton2makes No the default. - Add a title. The third parameter, Title, is a string shown in the dialog's title bar. Example:
MsgBox "Save changes?", vbYesNoCancel, "My Add-in". If omitted, Excel uses the application name. - Capture the return value. Use a variable to store which button was clicked. Declare
Dim answer As VbMsgBoxResultand assignanswer = MsgBox("Proceed?", vbYesNo + vbQuestion, "Confirm"). Then test withIf answer = vbYes Then .... Return constants includevbYes,vbNo,vbCancel,vbOK,vbRetry,vbIgnore,vbAbort. - Use the HelpFile and Context parameters (optional). The fourth and fifth parameters, HelpFile and Context, add a Help button that opens a compiled help file. These are rarely used in modern Excel and require a .hlp or .chm file. If you do not have one, omit them.
- Test different combinations. Run your macro with various buttons and icons to see how the dialog changes. Remember that the Buttons argument is a sum of constants; you can use the immediate window to evaluate combinations (e.g.,
? vbYesNo + vbQuestion).
Common Mistakes
- Forgetting to capture the return value. If you call MsgBox as a statement (
MsgBox "..."), you cannot tell which button was pressed. Always assign to a variable when you need a decision. - Using the wrong constant for the button set. For example, using
vbYesNobut testing forvbOKwill never be true. Match the test to the buttons shown. - Combining incompatible constants. You cannot combine two button-type constants (e.g.,
vbYesNo + vbOKCancel) because they occupy the same bit field. Use only one button constant, one icon, and one default-button constant. - Assuming the title parameter is required. It is optional; if omitted, the title bar shows "Microsoft Excel".
- Ignoring modality. By default, MsgBox is application-modal, meaning the user cannot interact with Excel until dismissing it. The
vbSystemModalconstant makes it system-modal, blocking all applications. Use sparingly.
Tips
- Use
vbInformationfor neutral messages,vbExclamationfor warnings, andvbCriticalfor errors. This helps users recognize severity at a glance. - Keep prompt text concise. Long messages may be truncated or wrap awkwardly.
- Use
vbCrLforvbNewLineinside the prompt string to create line breaks:"Line 1" & vbCrLf & "Line 2". - For yes/no questions, phrase the prompt so that Yes and No have clear meanings. Avoid double negatives.
- When testing, use a variable of type
VbMsgBoxResultfor clarity and IntelliSense support.
Troubleshooting
- Compile error: Expected: end of statement. Check that you have not used a reserved word or missed a comma. Also ensure you are not using a constant that does not exist (e.g.,
vbQuestionMarkinstead ofvbQuestion). - MsgBox does not appear. The macro may have halted on an error before reaching the MsgBox, or the MsgBox may be behind another window. Check for error handling and try running from the VBA editor with F8 to step through.
- Return value is always 1 (vbOK). If you use
vbOKOnly, the only possible return isvbOK. Make sure you are using a button set that includes the button you are testing for. - Help button does nothing. The HelpFile parameter must point to a valid help file, and the Context parameter must be a valid context ID. Without a proper help file, the button may appear but do nothing.
- Dialog appears on a different monitor. MsgBox appears on the monitor where the Excel window is active. If you move Excel, the next MsgBox will follow.
Frequently Asked Questions
Can I customize the button labels in a MsgBox?
No. VBA MsgBox only offers predefined button sets (OK, OK/Cancel, Yes/No, etc.). For custom labels, you need to create a UserForm.
What is the maximum length of the prompt?
The prompt can be up to 1024 characters. Longer strings are truncated.
How do I make a MsgBox appear on top of all other windows?
Add the vbSystemModal constant to the Buttons argument. This makes the dialog system-modal, blocking all applications until dismissed. Use with caution.
Can I use MsgBox in Excel for Mac?
Yes, VBA MsgBox works in Excel for Mac, but some constants like vbSystemModal may not behave the same. The basic parameters (Prompt, Buttons, Title) work identically.
Why does my MsgBox return 0?
A return value of 0 corresponds to vbOK only when the button set includes an OK button. If you use vbYesNo, the return values are 6 (Yes) or 7 (No). Check that you are testing against the correct constant.
Can I display a variable in a MsgBox?
Yes, concatenate the variable into the prompt string: MsgBox "The value is " & myVar. Use CStr if necessary to convert numbers to text.