Excel VBA MsgBox with Multiple Lines: Step-by-Step
In this article
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.xlsxfile will discard VBA modules when saved. - Access to the Visual Basic Editor: press
Alt+F11from Excel. - A blank module: in the VBA Editor, choose Insert > Module.
Step-by-Step
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).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.
Create a new subroutine. Type
Sub MultiLineMsg()and press Enter. The editor automatically addsEnd Sub. Your code goes between these two lines.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.vbCrLfis the line-break constant. Each time you insert it, the message box starts a new line.Run the macro. Press
F5while 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.Add a title and an icon (optional but useful). The full
MsgBoxsyntax isMsgBox(prompt, buttons, title). For example:MsgBox "Line one" & vbCrLf & "Line two", vbInformation, "My Report"vbInformationshows an information icon. Other common button constants arevbOKOnly,vbYesNo, andvbCritical. The title appears in the window's title bar.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.
Use a line-break constant consistently.
vbCrLfandvbNewLineboth work in a MsgBox on Windows. Pick one and stick with it. Do not type the characters\nliterally; VBA does not interpret them as line breaks in a message box string.Save and test. Press
Ctrl+Sin the VBA Editor, then return to Excel withAlt+F11and run the macro from Developer > Macros. Confirm each line appears separately and the title and icon are correct.
Common Mistakes
- Using
\ninstead ofvbCrLf. 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 2treats 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
Joinrather than chaining dozens of& vbCrLf &expressions. It is easier to read and edit. - Use
vbYesNowhen 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.