Smart Systems for Home

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

Excel VBA Multiline MsgBox: 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 show a multiline message in an Excel VBA MsgBox, build the text as a single string with line breaks inserted using vbCrLf (or vbNewLine), then pass that string to MsgBox. For example: MsgBox "Line 1" & vbCrLf & "Line 2". That is the whole trick — the MsgBox itself has no “multiline” property; the line breaks live inside the string you feed it. The steps below show the reliable ways to assemble that string, including a helper function for long messages, and the mistakes that make line breaks disappear.

What You'll Need

  • Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows; on Mac, Tools > Macro > Visual Basic Editor).
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
  • Basic familiarity with inserting a module and running a macro.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or use Tools > Macro > Visual Basic Editor (Mac). Menu labels can differ slightly by Excel version and platform.
  2. Insert a standard module. In the editor, choose Insert > Module. A blank code window appears. Keeping demo code in a standard module makes it easy to run and delete later.
  3. Write a minimal multiline MsgBox. Type this procedure:
    Sub SimpleMultiline()
        MsgBox "First line" & vbCrLf & "Second line" & vbCrLf & "Third line"
    End Sub
    The ampersand joins the pieces into one string; vbCrLf is the carriage-return plus line-feed pair that Windows dialogs treat as a new line.
  4. Run it. Place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. The message box should show three stacked lines. If it shows one long line, see Common Mistakes below.
  5. Add a title and icon (optional). The second argument of MsgBox is the title bar text, and the third controls buttons and icon:
    MsgBox "Saved: " & vbCrLf & "Report.xlsx", vbInformation, "Backup Status"
    Named constants such as vbInformation are clearer than raw numbers and are part of VBA itself, so they work across versions.
  6. Use a helper function for long messages. Concatenating many lines gets unreadable. A small function that joins an array with vbCrLf keeps the call site clean:
    Function JoinLines(ParamArray parts() As Variant) As String
        JoinLines = Join(parts, vbCrLf)
    End Function
    
    Sub ShowSummary()
        MsgBox JoinLines("Backup complete", "Files copied: 12", "Errors: 0"), vbInformation, "Summary"
    End Sub
    Join is a built-in VBA function; ParamArray lets you pass any number of strings.
  7. Build lines from variables. Real macros usually report values. Convert numbers with CStr or let concatenation coerce them, and keep each line as its own expression:
    Dim msg As String
    msg = "Rows processed: " & CStr(rowsDone) & vbCrLf
    msg = msg & "Time: " & Format(Now, "hh:mm:ss")
    MsgBox msg, vbInformation, "Run Report"
    Format avoids locale surprises in dates and times.
  8. Test on both platforms if you share the file. Windows uses vbCrLf; Mac historically used a carriage return only, but modern Excel for Mac accepts vbCrLf in MsgBox. If a shared workbook shows odd spacing on Mac, try vbNewLine, which resolves to the platform's line separator.
  9. Save and clean up. Save the workbook as .xlsm. Delete demo procedures you no longer need so the module stays readable.

Common Mistakes

  • Using a literal backslash-n. In VBA, "\n" is just the characters backslash and n. Use vbCrLf, vbNewLine, or Chr(10)/Chr(13).
  • Putting the line break outside the string. MsgBox "A" vbCrLf "B" is a syntax error; every piece must be joined with &.
  • Forgetting spaces around &. VBA tolerates it, but "A"&vbCrLf&"B" is harder to read and easy to break when editing.
  • Assuming MsgBox wraps text automatically. Long single lines may be clipped or widen the dialog; insert your own breaks at sensible points.
  • Mixing up argument order. The prompt is first, buttons/icon second, title third. Swapping them produces confusing dialogs or a compile error.
  • Leaving out CStr on ambiguous values. Concatenating a Variant that holds an error or Null can blank the whole message; convert explicitly.

Tips

  • Define a module-level constant such as Const NL As String = vbCrLf to shorten long concatenations.
  • Keep messages under roughly a dozen lines; beyond that, write to a sheet or a text file instead of a dialog.
  • Use vbInformation, vbExclamation, and vbCritical consistently so users recognise severity at a glance.
  • For debugging only, Debug.Print with vbCrLf writes the same multiline text to the Immediate Window without interrupting the run.
  • If you need formatted output, build the string with Format or FormatNumber before joining, not inside the MsgBox call.

Troubleshooting

  • Everything appears on one line: confirm you used vbCrLf and not "\n"; check that each segment is joined with &.
  • Compile error “Expected: end of statement”: usually a missing & or an unclosed quote. Count quotation marks in the line.
  • Message is blank or shows “Error”: a variable in the concatenation is Null or an error value. Wrap it with CStr and test the variable with Debug.Print first.
  • Dialog is cut off on screen: the text is too wide. Insert vbCrLf earlier in long sentences.
  • Macro won't run at all: the file may be saved as .xlsx, which strips macros. Save as .xlsm and re-open.
  • If that did not work: paste the exact failing line into a new module with hard-coded strings. If the hard-coded version works, the problem is in how the variables are built, not in MsgBox itself.

Frequently Asked Questions

What is the difference between vbCrLf and vbNewLine?

vbCrLf is always carriage return plus line feed. vbNewLine resolves to the platform's newline. For MsgBox on Windows they behave the same; vbNewLine is the safer choice for cross-platform files.

Can I use Chr(10) instead?

Yes. Chr(10) is a line feed and works in MsgBox on Windows. Chr(13) is a carriage return. vbCrLf combines both and is the conventional choice.

Why does my line break show as a small square?

That usually means the break character is not one MsgBox recognises — often a literal backslash-n or a character copied from another language. Replace it with vbCrLf.

Can I show a table or columns in a MsgBox?

Not truly. MsgBox uses a proportional font and ignores tabs for alignment. Use spaces for rough alignment, or write the table to a worksheet and show a short summary in the dialog.

Does MsgBox have a maximum length?

Very long prompts can be truncated or produce an oversized dialog. Keep prompts to a few hundred characters; for longer output, write to a cell range or a text file.

Can I include a variable and text on the same line?

Yes: "Total: " & CStr(total). Join the label, the converted variable, and any line break with &.