Excel VBA Multiline MsgBox: Step-by-Step Guide
In this article
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
- 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.
- 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.
- Write a minimal multiline MsgBox. Type this procedure:
The ampersand joins the pieces into one string;Sub SimpleMultiline() MsgBox "First line" & vbCrLf & "Second line" & vbCrLf & "Third line" End SubvbCrLfis the carriage-return plus line-feed pair that Windows dialogs treat as a new line. - 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.
- Add a title and icon (optional). The second argument of MsgBox is the title bar text, and the third controls buttons and icon:
Named constants such asMsgBox "Saved: " & vbCrLf & "Report.xlsx", vbInformation, "Backup Status"vbInformationare clearer than raw numbers and are part of VBA itself, so they work across versions. - Use a helper function for long messages. Concatenating many lines gets unreadable. A small function that joins an array with
vbCrLfkeeps 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 SubJoinis a built-in VBA function;ParamArraylets you pass any number of strings. - Build lines from variables. Real macros usually report values. Convert numbers with
CStror 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"Formatavoids locale surprises in dates and times. - Test on both platforms if you share the file. Windows uses
vbCrLf; Mac historically used a carriage return only, but modern Excel for Mac acceptsvbCrLfin MsgBox. If a shared workbook shows odd spacing on Mac, tryvbNewLine, which resolves to the platform's line separator. - 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. UsevbCrLf,vbNewLine, orChr(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
CStron 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 = vbCrLfto 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, andvbCriticalconsistently so users recognise severity at a glance. - For debugging only,
Debug.PrintwithvbCrLfwrites the same multiline text to the Immediate Window without interrupting the run. - If you need formatted output, build the string with
FormatorFormatNumberbefore joining, not inside the MsgBox call.
Troubleshooting
- Everything appears on one line: confirm you used
vbCrLfand 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
CStrand test the variable withDebug.Printfirst. - Dialog is cut off on screen: the text is too wide. Insert
vbCrLfearlier 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 &.