Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA MsgBox Options: A Complete Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To use MsgBox options in Excel VBA, call the MsgBox function with the message text and an optional buttons-and-icons argument, for example MsgBox "Save changes?", vbYesNo + vbQuestion, "Confirm". The second argument controls which buttons appear and which icon is shown; the third sets the title bar text. You can capture the user's click in a variable to branch your code. This guide covers the built-in constants, how to combine them, how to read the return value, 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 VBA editor (Alt+F11).
  • A workbook saved as .xlsm if you want to keep your macros.
  • Basic familiarity with writing a Sub procedure.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11. In the Project Explorer, right-click your workbook and choose Insert > Module. A blank code window appears.
  2. Write a minimal MsgBox. Type Sub Demo(), press Enter, then MsgBox "Hello", then End Sub. Place the cursor inside the procedure and press F5. A message box with an OK button appears. This confirms your environment works.
  3. Add the buttons argument. The second argument is a combination of constants. Common button sets: vbOKOnly (default), vbOKCancel, vbYesNo, vbYesNoCancel, vbRetryCancel, and vbAbortRetryIgnore. For example: MsgBox "Continue?", vbYesNo. Note that constant names are not case-sensitive in VBA, but using the standard casing improves readability.
  4. Add an icon. Combine a button constant with an icon constant using the plus sign: vbYesNo + vbQuestion, vbOKOnly + vbExclamation, vbOKCancel + vbCritical, or vbOKOnly + vbInformation. Icons are cosmetic but help signal intent (question for decisions, exclamation for warnings, critical for errors).
  5. Set a default button. Add vbDefaultButton1, vbDefaultButton2, or vbDefaultButton3 to control which button has focus when the box opens. For a Yes/No box, vbYesNo + vbQuestion + vbDefaultButton2 makes No the default, which is safer for destructive actions.
  6. Add a title. The third argument is a string shown in the title bar: MsgBox "Delete this row?", vbYesNo + vbExclamation, "Confirm Delete". If you omit it, Excel shows the application name.
  7. Capture the return value. Assign the result to a variable: Dim ans As VbMsgBoxResult then ans = MsgBox("Save?", vbYesNoCancel). Compare with the constants vbYes, vbNo, vbCancel, vbOK, vbRetry, vbIgnore, vbAbort. Use an If or Select Case block to branch.
  8. Test each branch. Run the macro and click every button to confirm the code responds correctly. For a Cancel branch, exit the sub with Exit Sub rather than continuing with partial changes.
  9. Handle the Escape key. If the box includes a Cancel button, pressing Esc returns vbCancel. If it does not, Esc behaves like the default button. Plan for this in destructive prompts.

Common Mistakes

  • Using & instead of + to combine constants. In VBA, & concatenates strings; + adds numeric values. Button and icon constants are numbers, so use +. Using & produces a type mismatch or wrong result.
  • Forgetting to capture the return value. A MsgBox without an assignment still displays, but you cannot react to the click. Always assign to a variable when the choice matters.
  • Comparing to the wrong constant. If you show vbYesNo but test for vbOK, the branch never runs. Match the constants to the buttons you displayed.
  • Ignoring Cancel in loops. A Cancel click inside a loop that keeps running frustrates users. Check for vbCancel and exit cleanly.
  • Overusing message boxes. A MsgBox halts execution until dismissed. In long macros, too many prompts make the tool unusable. Reserve them for decisions and errors.
  • Assuming the title argument is required. It is optional, but omitting it gives a generic title that confuses users about which macro is running.

Tips

  • Use vbQuestion for yes/no decisions, vbExclamation for warnings, and vbCritical for errors. Consistency helps users respond correctly.
  • Keep messages short and specific: state what will happen and what the buttons do. Avoid jargon.
  • For destructive actions, default to the safe button (vbDefaultButton2 on a Yes/No box) and use vbExclamation.
  • Store repeated messages in a string variable or constant so you can reuse and translate them easily.
  • Use vbInformation for confirmations after a successful operation, but avoid a box for every row processed.
  • Remember that MsgBox is modal: the user cannot interact with the sheet until they click a button. That is fine for decisions, but not for progress updates.

Troubleshooting

  • Compile error: Expected: expression. Usually a missing comma or a misspelled constant. Check the argument list and ensure constants are spelled exactly as in the object library.
  • Type mismatch when combining constants. You likely used & instead of +. Replace it.
  • MsgBox does not appear. The macro may have stopped earlier, or Application.DisplayAlerts or ScreenUpdating settings are interfering. Step through with F8 to find where execution halts.
  • Wrong button returns. Verify you are comparing against the correct constant and that the button set includes that button. A Yes/No box never returns vbOK.
  • Escape closes the box unexpectedly. If the box has no Cancel button, Esc acts as the default. Add a Cancel button if you need to detect cancellation.
  • Macro runs on a protected sheet. MsgBox itself works, but subsequent actions may fail. Unprotect the sheet or adjust your code before showing the prompt.

Frequently Asked Questions

Can I change the button labels in a MsgBox?

No. The built-in MsgBox uses fixed labels (OK, Cancel, Yes, No, Retry, Ignore, Abort). For custom labels you need a UserForm with your own buttons.

How do I show a message without any buttons?

Every MsgBox has at least one button. The closest is vbOKOnly, which shows a single OK button. There is no buttonless message box in VBA.

What does MsgBox return if the user closes it with the X?

Closing with the X is treated like pressing Cancel if a Cancel button is present; otherwise it returns the default button's value. Always include a Cancel button when cancellation matters.

Can I use MsgBox in Excel for Mac?

Yes, the MsgBox function and its constants work in Excel for Mac. The visual style follows the Mac dialog conventions, but the return values are the same.

Why does my MsgBox show a number instead of text?

You likely passed a numeric variable as the message without converting it. Use CStr() or concatenate with an empty string to force text.

Is there a limit to the message length?

Yes, the message string is limited to about 1,024 characters. For longer text, use a UserForm or write to a cell and point the user there.