Excel VBA MsgBox: How to Ask Questions
In this article
A VBA MsgBox question is a message box that asks the user something and returns a value based on which button they click. The shortest working version is If MsgBox("Continue?", vbYesNo) = vbYes Then followed by the code you want to run. This lets a macro pause, confirm a destructive action, or branch down different paths without any custom user form. You only need Excel and the Visual Basic Editor; no add-ins or references are required.
What You'll Need
- Excel on Windows or Mac (the VBA editor is included in both).
- A workbook saved as .xlsm if you want to keep the macro.
- Basic familiarity with opening the Visual Basic Editor (Alt+F11 on Windows, or Developer > Visual Basic on the ribbon).
Note that menu labels and the exact position of the Developer tab can differ slightly by Excel version and by region, but the VBA language itself is the same.
Step-by-Step
- Open the editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it.
- Insert a module. In the Project Explorer, right-click your workbook and choose Insert > Module. Code placed in a worksheet module also works, but a standard module keeps things tidy.
- Start a Sub procedure. Type
Sub AskQuestion()and press Enter. The editor addsEnd Subautomatically. - Write the simplest question. Inside the procedure, type:
If MsgBox("Delete this row?", vbYesNo) = vbYes ThenActiveCell.EntireRow.DeleteEnd If
This displays Yes and No buttons and runs the delete only when Yes is clicked. - Add a title and an icon. The full syntax is
MsgBox(Prompt, Buttons, Title). For example:answer = MsgBox("Delete this row?", vbYesNo + vbQuestion, "Confirm delete")
The third argument appears in the title bar, which makes the dialog easier to identify. - Store the result in a variable. Declare
Dim answer As VbMsgBoxResultbefore the call, then test it withIf answer = vbYes Then. Using a variable is clearer than nesting the call inside the If statement, especially when you need to check three or more outcomes. - Handle Cancel and the X button. With
vbYesNoCancel, the X button returnsvbCancel. Always include an Else or Case Else branch so closing the box does not silently continue with the action. - Run the macro. Press F5 with the cursor inside the procedure. The message box appears and the code waits until a button is clicked.
Common Mistakes
- Comparing to the wrong constant.
vbYesis 6 andvbNois 7. Comparing againstTrueor1gives misleading results because the numeric values do not line up with what you expect. - Forgetting the Else branch. If the user clicks No or closes the box, code after the If block still runs. That is how accidental deletions happen.
- Using MsgBox for data entry. A MsgBox can only return a button choice. To collect text or a number you need
InputBox, or a small user form. - Long prompts without line breaks. Use
vbCrLforvbNewLineinside the prompt string to split text across lines; otherwise the box grows very wide. - Assuming the title argument is required. It is optional, but without it Excel shows the application name, which looks unprofessional in a shared workbook.
- Leaving the macro in a personal workbook by accident. If you record or paste code into Personal.xlsb, other users will not see the message box when they open the file.
Tips
- Combine button and icon constants with
+:vbYesNo + vbExclamationis valid and common. - Use
vbDefaultButton2to make No the default, which reduces the chance of an accidental Yes on a destructive action. - Keep prompts under about 250 characters. Longer text is hard to read in the standard dialog.
- For repeated confirmations, wrap the check in a small helper function so the wording stays consistent across procedures.
- Test with every button, including the X, before sharing the workbook.
Troubleshooting
If nothing appears when you press F5, check that the cursor is inside the procedure and that macros are enabled. A security warning on the yellow bar blocks all VBA until you click Enable Content.
If the macro runs but the wrong branch executes, print the returned value with Debug.Print answer and look in the Immediate window. That shows exactly which constant came back.
If the message box appears behind the Excel window, the workbook may be running in a different application context; bring Excel to the foreground and run again. If the box freezes, press Esc or click a button; a modal MsgBox always waits for input.
If the code errors on the MsgBox line, confirm the string uses straight quotes, not curly ones pasted from a document. Curly quotes are a frequent cause of compile errors in VBA.
Frequently Asked Questions
Can a MsgBox return text the user typed?
No. A MsgBox only returns which button was clicked. Use InputBox when you need typed input.
What does the X button return?
It returns vbCancel (2), the same as the Cancel button, when Cancel is one of the available buttons.
Can I change the button labels?
Not with the built-in MsgBox. Custom labels require a user form with your own buttons.
Why does my MsgBox show a number instead of text?
You are probably displaying the result variable rather than the prompt. Check that the first argument is the message string.
Does MsgBox work on Excel for Mac?
Yes, the VBA MsgBox function works on Mac Excel, though some button combinations and icon constants may render differently.
How do I stop the macro if the user clicks No?
Use Exit Sub inside the No branch, or structure the code so the action sits only inside the Yes branch.