To insert a newline character in Excel VBA, use the constant vbCrLf or vbNewLine for message boxes, and Chr(10) for cell contents. These constants tell Excel to stop the current line of text and start a new one, allowing you to create structured, readable data within a single cell or a popup alert.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with opening the Visual Basic Editor (VBE).
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This opens the VBE window where all your macros and modules are stored. - Insert a New Module: Go to the top menu and select
Insert > Module. This creates a blank workspace where you can write your procedure. - Define Your Procedure: Type
Sub InsertNewLine()at the top of the module to start your macro. - Choose Your Newline Constant: Depending on where the text is going, choose one of the following:
- For Message Boxes: Use
vbCrLf(Carriage Return Line Feed) orvbNewLine. These are virtually identical in modern Windows environments. - For Excel Cells: Use
Chr(10). WhilevbCrLfsometimes works in cells,Chr(10)is the specific character Excel recognizes as a "Line Feed" for internal cell wrapping.
- For Message Boxes: Use
- Concatenate the String: Use the ampersand (
&) symbol to join your text strings with the newline constant. For example, to create a two-line message, write:MsgBox "First Line" & vbCrLf & "Second Line". - Apply to a Cell: To put a newline in a cell, use:
Range("A1").Value = "First Line" & Chr(10) & "Second Line". - Enable Wrap Text: This is the most critical step for cells. VBA inserts the character, but Excel will not display the line break unless the cell is formatted to wrap text. Add this line to your code:
Range("A1").WrapText = True. - Run the Macro: Press
F5or click the green "Run" arrow in the toolbar to execute the code and see the results in your spreadsheet.
Common Mistakes
One of the most frequent errors is forgetting to set the WrapText property to True. If you omit this, Excel will treat the newline character as a non-printing character or a small square symbol, and the text will appear as one long continuous string. You must explicitly tell the cell to wrap the text for the Chr(10) character to trigger a visual break.
Another common mistake is using vbCrLf inside a cell and expecting it to work consistently across different versions of Excel or different operating systems. While it often works, vbCrLf actually inserts two characters: a Carriage Return (ASCII 13) and a Line Feed (ASCII 10). Excel cells specifically look for the Line Feed (ASCII 10). Using vbCrLf can sometimes result in a strange "box" character appearing at the start of the second line.
Beginners also often forget the spaces around the ampersand operator. While VBA is sometimes forgiving, writing "Text"&vbCrLf&"Text" can lead to syntax errors or readability issues. Always use spaces: "Text" & vbCrLf & "Text".
Tips
When building long, complex strings with multiple newlines, avoid writing one massive line of code. Instead, use a string variable and build the message incrementally. For example, start with Dim msg As String, then use msg = msg & "Line 1" & vbCrLf on separate lines. This makes your code much easier to debug and modify later.
If you are working with a large range of cells, do not set WrapText = True inside a loop for every single cell. This can slow down your macro significantly. Instead, select the entire range first and apply the formatting once: Range("A1:A100").WrapText = True, then loop through the cells to insert your text and newline characters.
For those creating professional-looking reports, consider using vbNewLine. While it functions like vbCrLf, it is platform-independent, meaning it automatically adjusts the newline character based on the operating system the code is running on, making your VBA project slightly more portable.
Troubleshooting
If your text is still appearing on one line despite using Chr(10), first check the cell formatting. Right-click the cell, select Format Cells, go to the Alignment tab, and ensure the Wrap text checkbox is checked. If it is checked and still not working, ensure you are using Chr(10) and not Chr(13). Chr(13) is a Carriage Return, which Excel does not recognize as a line break within a cell.
If you see a small square or a strange symbol where the newline should be, you have likely used vbCrLf or vbCr in a cell. Replace these with Chr(10). The square symbol is Excel's way of telling you it doesn't know how to render that specific control character in the grid.
If your code throws a "Syntax Error" on the line containing the newline constant, check your quotation marks. Every piece of literal text must be enclosed in double quotes, and the constants (like vbCrLf) must be outside those quotes. A common error is writing "First Line & vbCrLf & Second Line", which Excel treats as one long sentence rather than a command to break the line.
Frequently Asked Questions
What is the difference between vbCrLf and vbNewLine?
In most Windows-based Excel environments, they are identical. vbCrLf specifically stands for Carriage Return + Line Feed. vbNewLine is a more generic constant that provides the correct newline character for the current operating system.
Can I use a newline character in an Excel formula via VBA?
Yes, but you must wrap the Chr(10) in quotes within the formula string. For example: Range("A1").Formula = "=" & ""First" & CHAR(10) & ""Second"". Note that in a worksheet formula, you use CHAR(10), whereas in VBA code, you use Chr(10).
Why does my MsgBox ignore the newline character?
This usually happens if the newline constant is placed inside the quotation marks. Ensure the constant is concatenated using the & symbol. Correct: "Hello" & vbCrLf & "World". Incorrect: "Hello vbCrLf World".
How do I remove newline characters from a cell using VBA?
You can use the Replace function. To remove all line breaks in a cell, use: Range("A1").Value = Replace(Range("A1").Value, Chr(10), " "). This replaces the newline character with a standard space.
Does Chr(10) work on Mac versions of Excel?
Generally, yes, as modern Excel for Mac has standardized much of its behavior with the Windows version. However, if you encounter issues, vbNewLine is the safest bet for cross-platform compatibility.