To add a new line in an Excel VBA MsgBox, use the built-in constant vbNewLine (or vbCrLf) within your message string. For example: MsgBox "Line 1" & vbNewLine & "Line 2". This inserts a line break between the two text parts, making your message box easier to read. You can also use the Chr(10) function for a line feed character. This guide covers all the methods, how to combine them with variables, and common pitfalls to avoid.
What You'll Need
- Microsoft Excel (any recent version that supports VBA, typically Excel 2010 or later).
- Access to the VBA editor: press Alt + F11 in Excel to open it.
- A place to run the code: either a module in the VBA editor or the Immediate Window (press Ctrl + G in the VBA editor to open it).
Step-by-Step
- Open the VBA editor by pressing Alt + F11 in Excel. If you're using a Mac, press Fn + Option + F11.
- Insert a module (if you don't have one) by right-clicking on any item in the Project Explorer (usually on the left) and selecting Insert > Module. A blank code window appears.
- Write a simple MsgBox with a new line. Type the following code in the module:
Sub ShowMsgBox() MsgBox "First line" & vbNewLine & "Second line" End SubThe&concatenates the strings, andvbNewLineinserts the line break. - Run the macro by pressing F5 while the cursor is inside the
ShowMsgBoxsubroutine, or go to the Run menu and choose Run Sub/UserForm. You'll see a message box with two lines. - Use multiple new lines by repeating the constant. For three lines, write:
MsgBox "Line 1" & vbNewLine & "Line 2" & vbNewLine & "Line 3"
This stacks the text vertically. - Combine new lines with variables. If you have a variable like
Dim userName As String: userName = "Alice", you can display it with a break:MsgBox "Hello " & userName & vbNewLine & "Welcome to Excel!"
The variable's value appears on the first line, and the welcome message on the second. - Use the alternative constant
vbCrLfif you prefer. It stands for Carriage Return + Line Feed, which is the traditional Windows line break. ReplacevbNewLinewithvbCrLfin the same way. Both work identically on Windows; on Mac,vbNewLineis recommended for compatibility. - Use the
Chr()function for more control.Chr(10)is a line feed character, andChr(13)is a carriage return. For example,MsgBox "Line 1" & Chr(10) & "Line 2"works the same asvbNewLinein most cases. You can also combine them:Chr(13) & Chr(10)is exactlyvbCrLf. - Test with the Immediate Window for quick checks. In the VBA editor, press Ctrl + G to open the Immediate Window at the bottom. Type
MsgBox "Test" & vbNewLine & "Another line"and press Enter. This runs the MsgBox immediately without creating a subroutine. - Format longer messages by breaking the string into multiple lines in your code using the underscore (
_) continuation character. For example:MsgBox "This is a long message " & _ "that continues on the next line " & _ "in the code, but appears as one line " & _ "in the MsgBox unless you add vbNewLine."This keeps your code readable without affecting the output.
Common Mistakes
- Using a slash instead of an ampersand: Some people try
"Line 1" / vbNewLine, which causes a compile error. Always use&for concatenation. - Forgetting the space before and after the ampersand: VBA requires spaces around the
&operator."Line1"&vbNewLinewill fail; write"Line1" & vbNewLine. - Using
vbNewLineinside a string literal: WritingMsgBox "Line1 vbNewLine Line2"will literally display the text "vbNewLine" instead of a line break. The constant must be outside the quotes. - Confusing
vbNewLinewithvbTab:vbTabinserts a tab character, not a new line. UsevbNewLinefor line breaks. - Using
Chr(10)on old Mac systems: On classic Mac OS (pre-OS X),Chr(10)may not produce a line break; useChr(13)orvbNewLinefor compatibility. Modern Mac Excel handlesvbNewLinecorrectly.
Tips
- Use
vbNewLineconsistently for readability. It's self-documenting and works across platforms. - Build the message string step by step if you have many parts. For example:
Dim msg As String msg = "Line 1" & vbNewLine msg = msg & "Line 2" & vbNewLine msg = msg & "Line 3" MsgBox msg
This makes it easier to debug and modify. - Combine with
vbTabfor alignment: You can create simple tables in a MsgBox by mixing tabs and new lines. For example:MsgBox "Name" & vbTab & "Age" & vbNewLine & "Alice" & vbTab & "30". - Use
MsgBoxwith a title to provide context:MsgBox "Line1" & vbNewLine & "Line2", vbInformation, "My Title". The title appears in the title bar of the message box. - Test with different line lengths: If your message is too long, Excel may truncate it visually. Break it into logical lines to keep it readable.
Troubleshooting
If your MsgBox doesn't show a new line as expected, try these checks in order:
- Check for syntax errors: Look for red text in the VBA editor. Common issues include missing ampersands or quotes. Correct them and run again.
- Verify you're using the right constant: On some systems,
vbCrLfmight not work if the message box uses a different line ending. Switch tovbNewLineand test. - Test with a simple example: Type
MsgBox "A" & vbNewLine & "B"in the Immediate Window. If that works, your code has a different issue (like a variable containing a line break that's being overwritten). - Check for hidden characters: If you copied code from a web page, invisible characters might break the line. Delete the line and retype it manually.
- Ensure your macro is actually running: If you have multiple macros with the same name, Excel might run the wrong one. Use F8 to step through the code and see which line executes.
Frequently Asked Questions
Can I use a line break in a MsgBox without VBA?
No, the standard Excel message box (from the 'Insert Function' or data validation) does not support line breaks. You need VBA to create a custom MsgBox with new lines.
What's the difference between vbNewLine and vbCrLf?
In VBA, vbNewLine is the platform-specific new line character (on Windows it's vbCrLf, on Mac it's vbLf). vbCrLf is always carriage return + line feed. For most purposes, they are interchangeable, but vbNewLine is safer for cross-platform compatibility.
How do I add a blank line in a MsgBox?
Use two new line constants consecutively: MsgBox "Line1" & vbNewLine & vbNewLine & "Line2". This creates an empty line between the two text lines.
Can I use a line break in the title of a MsgBox?
No, the title argument of MsgBox does not support line breaks. It will display as a single line. Use the message body for multi-line text.
Why does my MsgBox show the text "vbNewLine" instead of a line break?
This happens when you put vbNewLine inside quotes, like "vbNewLine". Remove the quotes so it's treated as a constant, not a string.
Can I use line breaks in a MsgBox that displays a range value?
Yes, concatenate the range value with vbNewLine. For example: MsgBox Range("A1").Value & vbNewLine & Range("A2").Value. This works for any variable or expression.