Excel VBA MsgBox: How to Use It
In this article
The Excel VBA MsgBox function displays a dialog box with a message and optional buttons and icons. It pauses your macro until the user clicks a button, then returns a number that tells you which button was clicked. You need Excel with the Developer tab enabled (or just press Alt+F11 to open the VBA editor).
What You'll Need
- Excel (any modern desktop version). The steps use the VBA editor, which is not available in Excel for the web.
- The Developer tab enabled: File > Options > Customize Ribbon, then tick Developer in the right-hand list. Labels can differ slightly by version or region.
- A workbook saved as .xlsm (macro-enabled) if you want to keep your code.
Step-by-Step
- Open the VBA editor with Alt+F11 (or Developer > Visual Basic).
- In the Project Explorer, right-click your workbook and choose Insert > Module. A blank code window appears.
- Type a simple message box. The basic syntax is
MsgBox "Your message". For example:MsgBox "Hello, world!". - Place the cursor inside the subroutine and press F5 to run it. A dialog box appears with an OK button. Click OK to continue.
- Add a title with the third argument:
MsgBox "Hello", , "My Title". The second argument is for buttons and icons; leave it blank if you only want a title. - Choose buttons with the second argument. For example,
MsgBox "Continue?", vbYesNoshows Yes and No buttons. Other common constants:vbOKOnly,vbOKCancel,vbYesNoCancel,vbRetryCancel. - Add an icon by combining constants with a plus sign:
MsgBox "Delete this row?", vbYesNo + vbQuestion. Icons includevbInformation,vbExclamation,vbCritical. - Capture the user's choice. Assign the result to a variable:
Dim answer As Integerthenanswer = MsgBox("Continue?", vbYesNo). Compare it:If answer = vbYes Then .... - Use the return values: vbOK = 1, vbCancel = 2, vbAbort = 3, vbRetry = 4, vbIgnore = 5, vbYes = 6, vbNo = 7. Using the named constants is safer than hard-coding numbers.
- Set a default button with
vbDefaultButton2(and so on) added to the button constant, so pressing Enter picks the safer option. - Save the workbook as .xlsm, then run the macro from Excel with Alt+F8 or assign it to a button.
Common Mistakes
- Forgetting the second argument when you want a title:
MsgBox "Hi", "Title"treats "Title" as the buttons argument and fails. UseMsgBox "Hi", , "Title". - Comparing the result to the wrong constant. If you show vbYesNo, do not test for vbOK.
- Using MsgBox inside a loop without a way to exit, so the user is trapped clicking OK forever.
- Expecting MsgBox to return text. It only returns a number; to collect input you need
InputBox. - Leaving out quotes around literal text, which causes a compile error.
Tips
- Use
vbCrLfto break a long message into lines:MsgBox "Line 1" & vbCrLf & "Line 2". - Keep messages short. A dialog box interrupts work, so reserve it for confirmations and errors.
- For a yes/no decision that should default to No, add
vbDefaultButton2so an accidental Enter does not proceed. - Use
vbInformationfor confirmations andvbCriticalfor errors so the icon matches the meaning. - If you need the user's name or a value, use
InputBoxinstead of trying to parse MsgBox output.
Troubleshooting
- Nothing happens when you press F5: the cursor may be outside a subroutine. Click inside the
SubandEnd Sublines and try again. - Compile error: check for missing quotes, a missing comma, or a misspelled constant such as
vbYesNo. - Wrong buttons appear: confirm the second argument is the button constant and the third is the title. Commas matter.
- Macro does not run from Excel: check the file is saved as .xlsm and that macros are enabled in the Trust Center.
- Dialog box appears behind Excel: click the Excel window or press Alt+Tab to bring it forward.
- Code lost after closing: VBA code is not saved in .xlsx files. Save as .xlsm before closing.
Frequently Asked Questions
What does MsgBox return?
A number corresponding to the button clicked: 1 for OK, 2 for Cancel, 6 for Yes, 7 for No, and so on. Use the vb constants to compare.
Can I get text input from MsgBox?
No. MsgBox only displays a message and returns a button value. Use InputBox to collect text.
How do I add a title to the message box?
Use the third argument: MsgBox "Message", vbOKOnly, "Title". Leave the second argument blank if you do not need buttons or icons.
Why does my MsgBox show the wrong buttons?
The second argument controls buttons and icons. Check that you used a button constant such as vbYesNo and that commas are in the right places.
Does MsgBox work in Excel for the web?
No. VBA and the VBA editor are not available in Excel for the web; use the desktop app.
How do I show a message on multiple lines?
Join the lines with vbCrLf: MsgBox "First" & vbCrLf & "Second".