Smart Systems for Home

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

Excel VBA MsgBox Format: Step-by-Step 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 format a message box in Excel VBA, use the MsgBox function with its optional buttons, title, and help file arguments, and control line breaks and spacing with vbCrLf and vbTab. The basic syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is the text shown, buttons is a combination of constants like vbOKOnly or vbYesNo, and title is the caption of the dialog. You can also use vbCrLf to insert a line break and vbTab to insert a tab. This guide walks through the exact steps to build, format, and troubleshoot message boxes in the Excel VBA editor.

What You'll Need

  • Excel installed on your computer (any modern version works).
  • Access to the Developer tab. If it is not visible, enable it via File > Options > Customize Ribbon and check Developer in the right-hand list.
  • Basic familiarity with the VBA editor (Alt+F11 opens it).
  • A workbook saved as .xlsm if you plan to keep macros.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Windows) or Fn+Alt+F11 (on some laptops). On Mac, use the Developer tab > Visual Basic. The editor opens in a separate window.
  2. Insert a module. In the VBA editor, go to Insert > Module. A blank code window appears. This is where you will write your macro.
  3. Start a new subroutine. Type Sub ShowMessage() and press Enter. The editor adds End Sub automatically. All your code goes between these lines.
  4. Write a basic MsgBox. Type MsgBox "Hello, world!" on its own line. This displays a simple message box with an OK button and no custom title.
  5. Add a title. Change the line to MsgBox "Hello, world!", , "My Title". The empty argument between the commas is the buttons argument; leaving it blank uses the default OK button. The third argument sets the dialog caption.
  6. Choose button constants. Replace the blank with a constant such as vbYesNo or vbOKCancel. For example: MsgBox "Continue?", vbYesNo, "Confirm". Common constants include vbOKOnly, vbOKCancel, vbYesNo, vbYesNoCancel, vbRetryCancel, and vbAbortRetryIgnore. You can combine a button constant with an icon constant using the plus sign, e.g., vbYesNo + vbQuestion.
  7. Add an icon. Use vbCritical, vbQuestion, vbExclamation, or vbInformation. For example: MsgBox "File not found.", vbOKOnly + vbCritical, "Error".
  8. Insert line breaks. Use vbCrLf inside the prompt string. For example: MsgBox "Line one" & vbCrLf & "Line two", vbInformation, "Two Lines". You can chain multiple vbCrLf to create paragraphs.
  9. Add tabs for alignment. Use vbTab to separate columns. For example: MsgBox "Name:" & vbTab & "John" & vbCrLf & "Age:" & vbTab & "30", vbInformation, "Details".
  10. Capture the user's response. Assign the MsgBox to a variable. For example: Dim ans As Integer; ans = MsgBox("Save changes?", vbYesNo + vbQuestion, "Save"). Then test ans against vbYes or vbNo to branch your code.
  11. Run the macro. Press F5 while the cursor is inside the subroutine, or use Run > Run Sub/UserForm. The message box appears. Click a button to dismiss it.
  12. Save your work. Press Ctrl+S. If prompted about macro-free format, choose No and save as .xlsm to keep the code.

Note: Menu labels and dialog wording can differ slightly by Excel version, platform, or region. The steps above apply to Excel for Windows; on Mac, the VBA editor is similar but some shortcuts differ.

Common Mistakes

  • Forgetting quotes around text. MsgBox Hello will cause a compile error. Always enclose literal text in double quotes.
  • Using the wrong argument order. The prompt comes first, buttons second, title third. Mixing them up produces unexpected results or errors.
  • Omitting the buttons argument when setting a title. To set a title without changing buttons, use two commas: MsgBox "Text", , "Title".
  • Using parentheses incorrectly. When you call MsgBox as a statement (not assigning to a variable), do not wrap arguments in parentheses. Use MsgBox "Text", vbOKOnly. When assigning to a variable, use parentheses: ans = MsgBox("Text", vbYesNo).
  • Expecting vbCrLf to work in the title. Line breaks are only supported in the prompt, not the title bar.
  • Ignoring the return value. If you use vbYesNo but do not capture the result, the user's choice has no effect.

Tips

  • Use string concatenation to build dynamic messages from cell values, e.g., MsgBox "Value is " & Range("A1").Value.
  • Keep prompts concise; long messages may be truncated on some systems.
  • Use vbInformation for neutral messages, vbExclamation for warnings, and vbCritical for errors.
  • Combine button and icon constants with + (e.g., vbYesNo + vbQuestion). Do not use + to combine two button constants.
  • For help files, the helpfile and context arguments are rarely needed in modern Excel; omit them unless you have a compiled help file.
  • Test your macro with different responses to ensure branching works as intended.

Troubleshooting

  • Compile error: Expected: end of statement. Check for missing quotes or commas. Ensure the prompt is a string.
  • Message box does not appear. Make sure the macro is running and not stuck on a previous dialog. Press Esc or Ctrl+Break to interrupt.
  • Wrong buttons shown. Verify the constant name. Use the Object Browser (F2) to search for MsgBox constants.
  • Line breaks not working. Ensure you are using vbCrLf (or Chr(13) & Chr(10)) inside the prompt string, not in the title.
  • Macro security blocks the code. Enable macros via File > Options > Trust Center > Trust Center Settings > Macro Settings, or open the file from a trusted location.
  • Unexpected variable type error. If assigning the result, declare the variable as Integer or Variant.

Frequently Asked Questions

Can I change the font or color of a MsgBox?

No, the standard MsgBox function does not support font or color customization. For advanced formatting, create a custom UserForm.

How do I show a message box with just an OK button and a custom title?

Use MsgBox "Your text", vbOKOnly, "Your Title".

What is the difference between vbCrLf and vbNewLine?

Both insert a line break; vbCrLf is a carriage return plus line feed, while vbNewLine is platform-dependent. In Excel VBA, vbCrLf is commonly used and works on Windows.

Can I use MsgBox to get user input other than button clicks?

No, MsgBox only returns a button choice. Use InputBox for text or number input.

Why does my MsgBox show a help button?

You may have included vbMsgBoxHelpButton in the buttons argument. Remove it if not needed.

How do I display a variable in a MsgBox?

Concatenate it with &, e.g., MsgBox "The value is " & myVar.