Smart Systems for Home

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

Excel VBA MsgBox with Multiple Lines: Step-by-Step

· 6 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

You can put multiple lines in an Excel VBA message box by joining your text with the built-in constant vbCrLf (carriage return + line feed) or vbNewLine, then passing the combined string to MsgBox. For example, MsgBox "Line 1" & vbCrLf & "Line 2" displays two lines. That one-line answer covers most cases; the rest of this guide shows the exact syntax, how to add titles and icons, how to avoid the most common errors, and how to troubleshoot a message box that shows everything on one line.

What You'll Need

  • Microsoft Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer in the right-hand list). Labels can differ slightly by Excel version and region.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep your code. A standard .xlsx file will discard VBA modules when saved.
  • Access to the Visual Basic Editor: press Alt + F11 from Excel.
  • A blank module: in the VBA Editor, choose Insert > Module.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt + F11. If nothing appears, check that macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings).

  2. Insert a module. In the VBA Editor menu, click Insert > Module. A blank code window opens on the right. This is where your macro will live.

  3. Create a new subroutine. Type Sub MultiLineMsg() and press Enter. The editor automatically adds End Sub. Your code goes between these two lines.

  4. Write the simplest multi-line MsgBox. Type the following inside the subroutine:

    MsgBox "First line" & vbCrLf & "Second line" & vbCrLf & "Third line"

    The ampersand (&) joins text strings. vbCrLf is the line-break constant. Each time you insert it, the message box starts a new line.

  5. Run the macro. Press F5 while the cursor is inside the subroutine, or click Run > Run Sub/UserForm. A message box appears with three separate lines. Click OK to dismiss it.

  6. Add a title and an icon (optional but useful). The full MsgBox syntax is MsgBox(prompt, buttons, title). For example:

    MsgBox "Line one" & vbCrLf & "Line two", vbInformation, "My Report"

    vbInformation shows an information icon. Other common button constants are vbOKOnly, vbYesNo, and vbCritical. The title appears in the window's title bar.

  7. Build the text from variables when needed. If your lines come from cell values, concatenate them the same way:

    Dim name As String
    Dim total As String
    name = Range("A1").Value
    total = Range("B1").Value
    MsgBox "Name: " & name & vbCrLf & "Total: " & total, vbOKOnly, "Summary"

    This is the pattern you will use most often in real macros.

  8. Use a line-break constant consistently. vbCrLf and vbNewLine both work in a MsgBox on Windows. Pick one and stick with it. Do not type the characters \n literally; VBA does not interpret them as line breaks in a message box string.

  9. Save and test. Press Ctrl + S in the VBA Editor, then return to Excel with Alt + F11 and run the macro from Developer > Macros. Confirm each line appears separately and the title and icon are correct.

Common Mistakes

  • Using \n instead of vbCrLf. A literal backslash-n is just two characters to VBA and will appear in the message box as text, not a line break.
  • Missing the ampersand. Writing MsgBox "Line 1" vbCrLf "Line 2" causes a compile error. Every piece of text and every constant must be joined with &.
  • Forgetting quotes around literal text. MsgBox Line 1 & vbCrLf & Line 2 treats the words as variables and fails unless those variables exist.
  • Putting the title in the wrong argument position. The title is the third argument. If you skip the buttons argument, you still need a comma placeholder: MsgBox prompt, , "Title".
  • Exceeding the character limit. A MsgBox prompt is limited to about 1,024 characters. Longer text is truncated, so split very long messages or write them to a cell instead.
  • Saving as .xlsx. The module disappears on save. Use .xlsm.

Tips

  • For many lines, build the string in a loop or with Join rather than chaining dozens of & vbCrLf & expressions. It is easier to read and edit.
  • Use vbYesNo when you need a decision, and capture the return value: If MsgBox("Continue?", vbYesNo) = vbYes Then ....
  • Keep messages short. A message box is a notification, not a report; long text is hard to read and easy to dismiss.
  • Test with the Immediate Window (Ctrl + G) by typing ? "A" & vbCrLf & "B" to preview the string before running the macro.
  • If your macro runs on a Mac, the same constants work in Excel for Mac, but the visual style of the dialog differs from Windows.

Troubleshooting

The message box shows everything on one line. Check that you used vbCrLf or vbNewLine and not \n or a manual line break typed inside the quotes. Also confirm the ampersands are present between each segment.

You get a "Compile error: Expected: end of statement". This usually means a missing &, a missing quote, or a stray character. Compare your line with the working example in Step 4.

Nothing happens when you press F5. Place the cursor inside the subroutine, not in a blank area of the module. If the macro is in a sheet module rather than a standard module, it may still run, but standard modules are easier to manage.

The macro is disabled. Excel may block macros from an untrusted location. Check the yellow security bar and enable content only for files you created or trust. Do not lower macro security settings globally.

The title does not appear. You likely passed the title as the second argument. The order is prompt, buttons, title. Use an empty argument placeholder if you do not want buttons: MsgBox "Text", , "Title".

Text is cut off. The prompt exceeds the message box limit. Shorten it or write the full text to a worksheet cell and show a short confirmation instead.

Frequently Asked Questions

What is the difference between vbCrLf and vbNewLine?

Both produce a line break in a VBA message box on Windows. vbCrLf is a carriage return plus line feed; vbNewLine is the platform's newline constant. Either works for this purpose.

Can I use a line break inside a cell instead of a message box?

Yes. In a cell, use vbLf and enable Wrap Text. In a MsgBox, vbCrLf is the conventional choice.

How do I add a variable and text on the same line?

Concatenate them with &: "Total: " & total. Then join that whole expression to the next line with & vbCrLf &.

Why does my MsgBox show the word vbCrLf as text?

It is probably inside the quotation marks. Constants must sit outside the quotes and be joined with &.

Is there a limit to how many lines I can show?

The practical limit is the prompt character limit, roughly 1,024 characters. The number of lines is not separately limited, but very long messages become unreadable.

Does this work in Excel for Mac?

Yes, the same MsgBox syntax and constants work in Excel for Mac, though the dialog's appearance differs from Windows.