Smart Systems for Home

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

Excel VBA MsgBox New Line: Step-by-Step Guide

· 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

To put a line break inside an Excel VBA message box, join your text with the built-in constant vbNewLine (or vbCrLf) using the & operator, then pass the result to MsgBox. For example, MsgBox "Line 1" & vbNewLine & "Line 2" shows two lines. That is the whole trick: MsgBox does not interpret a typed Enter key or a literal \n inside a string, so you must concatenate an actual newline constant. The steps below show exactly where to put it, how to handle longer messages, and the mistakes that make the box show everything on one line.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Visual Basic Editor available (it is included in standard desktop Excel; it is not available in Excel for the web).
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code; a plain .xlsx will drop macros on save.
  • Basic familiarity with opening the editor and running a macro. No add-ins or references are required.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or Option+F11 (Mac). Menu labels and shortcut behavior can differ slightly by Excel version and region, but the editor itself is the same tool.
  2. Insert a module. In the editor, choose Insert > Module. A blank code window appears. Writing your test in a standard module keeps it separate from worksheet event code.
  3. Start a short procedure. Type Sub ShowMultiLine() and press Enter. The editor adds End Sub automatically. All code goes between those two lines.
  4. Build the first line of text. Type MsgBox "First line" on its own line. At this point it is an ordinary single-line message box, which is your baseline.
  5. Add the newline constant and the second line. Change that line to:
    MsgBox "First line" & vbNewLine & "Second line"
    The ampersand joins the pieces into one string. vbNewLine is the part that becomes the visible break.
  6. Add more lines the same way. Each additional line needs its own & vbNewLine & before the next piece of text. Three lines look like:
    MsgBox "Name: " & Range("A1").Value & vbNewLine & _
    "Date: " & Range("B1").Value & vbNewLine & _
    "Status: Ready"
    The underscore at the end of a line continues the statement on the next line so it stays readable.
  7. Run the macro. Press F5 with the cursor inside the procedure, or use Run > Run Sub/UserForm. The message box should appear with each piece on its own line.
  8. Add a title and icon if you want. MsgBox accepts optional arguments after the message: a buttons value, a title, and a help file. For example, MsgBox "Line 1" & vbNewLine & "Line 2", vbInformation, "Report Status" shows an information icon and a custom caption. The message string always comes first, so the newline work happens before those arguments.
  9. Capture the user's choice when needed. If you use buttons such as vbYesNo, assign the result to a variable: answer = MsgBox("Save changes?" & vbNewLine & "This cannot be undone.", vbYesNo). Then test answer = vbYes. The line break does not affect the return value.
  10. Save the workbook as macro-enabled. Use File > Save As and pick the .xlsm format so the code survives closing and reopening.

Common Mistakes

  • Typing \n inside the quotes. VBA does not translate a backslash-n sequence in a normal string; the box will literally show \n. Use vbNewLine or vbCrLf instead.
  • Pressing Enter inside the string in the editor. VBA strings cannot span physical lines that way; the code will fail to compile. Break the statement with & vbNewLine & and, if needed, a line-continuation underscore.
  • Forgetting the ampersands. Writing "First" vbNewLine "Second" is a syntax error. Every piece must be joined with &.
  • Using Chr(10) alone on Windows dialogs. A bare line feed can render oddly in some contexts. vbNewLine and vbCrLf are the safe, portable choices for message boxes.
  • Mixing up MsgBox the function and the statement. Calling it as a statement ignores the return value; if you need the button result, wrap the arguments in parentheses and assign to a variable.
  • Assuming Debug.Print behaves the same. The Immediate Window handles line breaks differently; test the actual message box rather than judging from printed output.

Tips

  • vbNewLine, vbCrLf and vbLf are built-in constants, so no declarations are needed. vbNewLine is the most readable choice.
  • For long messages, build the string in a variable first (msg = msg & vbNewLine & nextPart) and pass msg to MsgBox once. This keeps the call short and makes it easy to add or remove lines.
  • Keep message boxes short. A dialog with many lines becomes hard to read; if you need a lot of text, consider writing to a cell range or a user form instead.
  • Use a blank line by concatenating two newline constants in a row: "Section 1" & vbNewLine & vbNewLine & "Section 2".
  • Test on both Windows and Mac if the workbook is shared; fonts and dialog sizing differ, so check that wrapped lines still read correctly.

Troubleshooting

The box shows one long line. Check that you used vbNewLine outside the quote marks and joined it with &. If the constant is inside the quotes, it is treated as plain text.

Compile error: Expected: end of statement. A string was split across lines without an underscore, or an ampersand is missing. Rebuild the statement as one expression, using _ at the end of each continued line.

Compile error: Variable not defined. This usually means Option Explicit is on and you mistyped a constant or variable name. vbNewLine is built in, so a typo such as vbNewline with different casing is fine in VBA, but a misspelling like vbNewLines is not.

The macro does not run at all. Confirm macros are enabled for the workbook and that the file is saved as .xlsm. If the editor opens but F5 does nothing, click inside the procedure first so the cursor is in the right place.

The message box appears behind Excel. This is a window focus issue, not a code issue. Click the Excel taskbar icon; avoid adding workarounds that change the message box type.

Text still looks wrong after fixing the code. Close the editor, save, and reopen the workbook. If the problem persists, copy the procedure into a fresh module and run it there to rule out conflicting code in the same module.

Frequently Asked Questions

What is the difference between vbNewLine and vbCrLf?

Both insert a line break in a message box. vbCrLf is specifically a carriage return plus line feed; vbNewLine is the platform-appropriate newline and is generally the safer, more readable choice.

Can I use a line break in the message box title?

No. The title argument is a single-line caption; newline constants there do not produce a clean two-line title. Put the extra information in the message body instead.

Does a newline work with variables and cell values?

Yes. Concatenate the variable or Range(...).Value between newline constants exactly as you would with literal text. The value is converted to text as part of the join.

Why does my message box show a square or odd character?

That usually means a line break character was inserted in a way the dialog does not render, or the text was pasted with a hidden character. Retype the line using vbNewLine rather than pasting from another source.

Can I add a newline in an InputBox prompt?

Yes. The prompt argument of InputBox accepts the same concatenation with vbNewLine, so the same technique applies.

Do I need to declare vbNewLine?

No. It is a built-in VBA constant and works without any declaration or reference.