To add a new line character to a string in Excel VBA, use the vbCrLf constant or the vbNewLine constant concatenated with your text using the ampersand (&). For example, myString = "First Line" & vbCrLf & "Second Line" will create a string with a carriage return and line feed, effectively breaking the text into two lines. This is essential for creating readable message boxes, generating formatted reports, or inserting multi-line data into a single cell.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- The Developer tab enabled in the Excel Ribbon (if not visible, right-click the ribbon, select Customize the Ribbon, and check the Developer box).
- Basic familiarity with opening the Visual Basic Editor (ALT + F11).
Step-by-Step
- Open the Visual Basic Editor: Press
ALT + F11on your keyboard to launch the VBA environment. - Insert a New Module: Go to the
Insertmenu at the top and selectModule. This provides a clean space to write your procedure. - Declare Your Variables: Start your subroutine by declaring a string variable to hold your text. For example, type
Dim myText As String. - Construct the String with vbCrLf: Use the concatenation operator (&) to join your text segments with the line break constant. Write the line:
myText = "Hello World!" & vbCrLf & "Welcome to VBA programming." - Choose Your Constant: While
vbCrLf(Carriage Return + Line Feed) is the most common, you can also usevbNewLine, which is platform-specific and generally behaves the same on Windows. If you only need a line feed (common in some Unix-style exports), usevbLf. - Output the Result to a Message Box: To verify the line break is working, add the line
MsgBox myText. When you run this, the message box will display the text on two separate lines. - Output the Result to a Cell: To put the multi-line string into an Excel cell, use
Range("A1").Value = myText. - Enable Wrap Text: This is the most critical step for cell output. VBA inserts the character, but Excel will not display the break unless Wrap Text is active. Add the line
Range("A1").WrapText = Trueto your code. - Run the Code: Press
F5or click the green "Run" arrow in the toolbar to execute your script.
Common Mistakes
One of the most frequent errors is forgetting to enable Wrap Text on the destination cell. Many users assume that adding vbCrLf to a string is enough to make the cell expand vertically. However, Excel's default cell formatting ignores line break characters and displays the text as a single continuous string unless the Wrap Text property is set to True. Without this, your data looks like it is all on one line, even though the character is technically present in the cell's value.
Another common mistake is using the wrong concatenation symbol. In some programming languages, a plus sign (+) is used for strings, but in VBA, the ampersand (&) is the standard and safest operator. Using a plus sign can occasionally lead to "Type Mismatch" errors if VBA attempts to mathematically add a numeric string to a line break constant.
Users also frequently confuse vbCr, vbLf, and vbCrLf. While vbCr (Carriage Return) and vbLf (Line Feed) might work in specific legacy environments or specific text files, using only one of them in a modern Windows Excel environment can sometimes result in a small square symbol (a glyph) appearing in the cell instead of a clean line break. Always prefer vbCrLf or vbNewLine for general purpose Windows applications.
Tips
When building very long strings with many line breaks, using a single line of code can become messy and hard to read. Instead, use the concatenation operator at the end of the line followed by a space and an underscore ( _ ), which is the VBA line-continuation character. This allows you to visually organize your code to match the output format:
myText = "Line 1" & vbCrLf & _
"Line 2" & vbCrLf & _
"Line 3"
If you are creating a list of items dynamically (such as looping through a range of cells and adding each value to a string), use a loop with an accumulator. Start with an empty string and append the current value plus a vbCrLf in each iteration. To avoid having an extra empty line at the very end of your final string, you can use the Left function to trim the last two characters: myText = Left(myText, Len(myText) - 2).
For those working with large amounts of text, consider using a StringBuilder approach by joining an array. Collect your lines in a dynamic array or a Collection, and then use the Join function with vbCrLf as the delimiter. This is significantly faster than repeated string concatenation in large loops because it reduces the number of times VBA has to reallocate memory for the string.
Troubleshooting
If your code runs without errors but the text in the cell still appears on one line, first check the Wrap Text setting. You can do this manually by selecting the cell and clicking the "Wrap Text" button in the Home tab of the ribbon. If the manual fix works, ensure your VBA code explicitly includes .WrapText = True for that range.
If you see a strange character (like a small box or a question mark) where the line break should be, you are likely using vbCr or vbLf in a context that requires the full vbCrLf sequence. Replace the constant with vbCrLf and run the code again.
If you receive a "Compile Error: Expected: end of statement" when trying to use the underscore for line continuation, ensure there is a space before the underscore and that the underscore is the very last character on that line. No comments or other code can follow the underscore on the same line.
If the message box displays the text correctly but the cell does not, check if the cell is formatted as "Text" or "General". While formatting usually doesn't block the line break, some custom number formats can interfere with how characters are rendered. Reset the cell to "General" format to rule this out.
Frequently Asked Questions
What is the difference between vbCrLf and vbNewLine?
In almost all Windows-based Excel VBA projects, they are identical. vbCrLf specifically stands for Carriage Return and Line Feed. vbNewLine is a more generic constant that adapts to the platform the code is running on, though since VBA is primarily Windows-based, it resolves to the same characters.
Can I use Chr(10) or Chr(13) instead?
Yes. Chr(10) is the ASCII character for a Line Feed (equivalent to vbLf) and Chr(13) is the Carriage Return (equivalent to vbCr). Many developers use Chr(10) specifically for Excel cells because Excel often recognizes the Line Feed alone as the trigger for a new line within a cell.
How do I remove new line characters from a string?
You can use the Replace function. For example: cleanString = Replace(myText, vbCrLf, " "). This will find every instance of a line break and replace it with a single space, flattening the text into a single line.
Will these line breaks work if I export the data to a CSV?
Yes, but the CSV file must be opened in a program that supports multi-line cells (like Excel). If you open the CSV in a basic text editor, you will see the raw vbCrLf characters. To ensure they are preserved, Excel usually wraps the cell content in double quotes when exporting to CSV.
Does this work in Excel for Mac?
VBA on Mac handles line breaks slightly differently, but vbNewLine is generally the safest choice for cross-platform compatibility as it is designed to use the system's default newline character.