Excel VBA MsgBox Syntax: A Complete Guide
In this article
To use MsgBox in Excel VBA, call it with a prompt string and optionally a buttons-and-icons constant, a title, and a help file reference. The simplest form is MsgBox "Your message", which shows a message box with an OK button. More control comes from the full syntax: MsgBox(prompt, [buttons], [title], [helpfile], [context]). This guide explains each argument, the constants you can use, how to capture the user's click, and the mistakes that trip people up.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- Access to the Visual Basic Editor (Alt+F11).
- Basic familiarity with writing a macro or inserting a module.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 in Excel. If the Developer tab is enabled, you can also click Developer > Visual Basic. The editor opens in a separate window.
- Insert a module. In the editor, go to Insert > Module. A blank code window appears. This is where you will write your macro.
- Write a basic MsgBox statement. Type the following into the module:
This displays a message box with the text and an OK button. The title bar will show "Microsoft Excel" by default.Sub SimpleMsgBox() MsgBox "Hello, world!" End Sub - Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Excel and run it from Developer > Macros. The message box appears. Click OK to dismiss it.
- Add a custom title. The third argument sets the title bar text. For example:
Note the empty second argument (buttons) is left blank, so you must include the comma. The title appears in the window's title bar.MsgBox "Hello, world!", , "My Title" - Choose buttons and icons. The second argument accepts constants that define which buttons appear and which icon is shown. Constants can be added together (or combined with
+orOr). For example, to show Yes and No buttons with a question mark icon:
Common button constants:MsgBox "Do you want to continue?", vbYesNo + vbQuestion, "Confirm"vbOKOnly,vbOKCancel,vbYesNo,vbYesNoCancel,vbRetryCancel,vbAbortRetryIgnore. Icon constants:vbCritical,vbQuestion,vbExclamation,vbInformation. Default button constants:vbDefaultButton1(first button is default),vbDefaultButton2,vbDefaultButton3,vbDefaultButton4. You can also set modality withvbApplicationModal(default) orvbSystemModal. - Capture the user's choice. MsgBox returns a value indicating which button was clicked. Assign it to a variable to branch your code. For example:
Return constants includeDim answer As VbMsgBoxResult answer = MsgBox("Save changes?", vbYesNo + vbQuestion, "Save") If answer = vbYes Then ' code to save Else ' code to skip End IfvbOK,vbCancel,vbYes,vbNo,vbRetry,vbIgnore,vbAbort. The variable typeVbMsgBoxResultis optional but makes the code clearer. - Use line breaks in the prompt. To split the message over multiple lines, use
vbCrLforvbNewLineinside the string:
You can also use theMsgBox "Line 1" & vbCrLf & "Line 2"Chr(10)orChr(13)characters, butvbCrLfis the standard. - Add a help file (optional). The fourth and fifth arguments specify a help file and context ID. This is rarely used in modern Excel and requires a compiled help file. For most purposes, leave them out.
- Test and refine. Run your macro with different constants to see how the message box changes. Use the Object Browser (F2) in the VBA editor to explore all
VbMsgBoxStyleandVbMsgBoxResultconstants.
Common Mistakes
- Forgetting the parentheses when using a function call. If you assign the result to a variable, you must use parentheses:
answer = MsgBox("Text", vbYesNo). Without parentheses, VBA treats it as a statement and you cannot capture the return value. - Omitting the button argument but including a title. You must include a comma placeholder:
MsgBox "Text", , "Title". A common error is writingMsgBox "Text", "Title", which tries to interpret "Title" as a button constant and fails. - Using the wrong constant type. Button constants are from
VbMsgBoxStyle, return constants fromVbMsgBoxResult. Mixing them up causes compile errors or unexpected behavior. - Assuming the default button is always the first. If you specify
vbDefaultButton2, the second button is focused when the box opens. This can lead to accidental clicks if the user presses Enter. - Not handling all possible return values. If you show Yes/No/Cancel, handle all three. Ignoring Cancel can cause your code to proceed as if OK was clicked.
- Using MsgBox in a loop without an exit condition. This can trap the user in an endless series of dialog boxes. Always provide a way to break out.
Tips
- Use
vbInformationfor neutral messages,vbExclamationfor warnings, andvbCriticalfor errors. This helps users understand the severity at a glance. - Keep prompts concise. Long messages are hard to read in a message box. If you need extensive text, consider a custom UserForm instead.
- Set the default button to the safest option. For destructive actions, make the default
vbNoorvbCancel. - Use a meaningful title. The default "Microsoft Excel" is unhelpful; a title like "Data Validation" gives context.
- Combine constants with
+orOr. Both work, butOris more explicit for bitwise combination. - Test your message boxes with different screen resolutions and Excel versions. The appearance may vary slightly.
Troubleshooting
If your MsgBox does not appear or behaves unexpectedly, check these common issues:
- Macro security settings: If macros are disabled, your code will not run. Enable macros in the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings).
- Compile errors: If you see "Expected: expression" or similar, check for missing commas, quotes, or parentheses. Use the Debug > Compile VBAProject command to catch syntax errors.
- Wrong variable type: If you assign the result to a variable declared as Integer, it may work but is less clear. Use
VbMsgBoxResultfor clarity. - Message box appears behind Excel: This can happen if the message box is system modal and another window has focus. Try clicking on the Excel window or pressing Alt+Tab.
- Help file error: If you specify a help file that does not exist, the message box may show an error or ignore the help button. Omit the helpfile argument unless you have a valid file.
- Unexpected return value: If the user closes the message box with the X button, the return value is
vbCancelfor most button configurations. Handle this case in your code.
Frequently Asked Questions
Can I use MsgBox without assigning the result?
Yes. If you do not need to know which button was clicked, you can call MsgBox as a statement without parentheses, like MsgBox "Hello". The message box will still wait for the user to click a button before continuing.
How do I show a message box with only an OK button?
Use vbOKOnly as the buttons argument, or omit the buttons argument entirely. For example: MsgBox "Done", vbOKOnly or simply MsgBox "Done".
What is the maximum length of the prompt?
The prompt can be up to approximately 1024 characters. If you need more, consider using a UserForm or splitting the message. Longer strings may be truncated.
Can I customize the buttons beyond the built-in constants?
No. MsgBox only supports the predefined button combinations (OK, OK/Cancel, Yes/No, Yes/No/Cancel, Retry/Cancel, Abort/Retry/Ignore). For custom buttons, you need to create a UserForm.
Why does my MsgBox show a help button?
You likely included vbMsgBoxHelpButton in the buttons argument. This adds a Help button, but it only works if you also provide a valid help file and context ID. Otherwise, clicking Help may do nothing or cause an error.
How do I make the message box appear on top of all other windows?
Use the vbSystemModal constant in the buttons argument. This makes the message box system modal, meaning it stays on top until dismissed. However, this can be disruptive, so use it sparingly.