To add a new line to a string in Excel VBA, join the text with a newline constant: use vbCrLf for a Windows-style line break, or vbLf for a line break inside an Excel worksheet cell. For example, message = "First line" & vbCrLf & "Second line" creates a two-line message.
The basic VBA newline syntax
VBA does not treat pressing Enter inside a quoted string as a way to insert a line break. Instead, build the string in parts and concatenate those parts with the & operator. A newline constant acts as the separator between the text fragments. This is useful for message boxes, text files, cell values, and other places where the receiving application recognizes line-ending characters.
Here is a complete example for a message box. Before running any macro, save a copy of your workbook: changes made by VBA macros generally cannot be reversed with Ctrl+Z.
Sub ShowTwoLineMessage()
Dim message As String
' Join two text fragments with a Windows line break.
message = "First line" & vbCrLf & "Second line"
MsgBox message, vbInformation, "Example"
End Sub
The ampersands join the first text fragment, the line-break character, and the second fragment. The variable is declared as a String, which is VBA’s text data type. You can use the same construction more than once in an expression to create several lines.
What the newline constants mean
VBA provides several built-in constants for line endings. Choosing among them matters because different destinations expect different characters:
vbCrLfrepresents a carriage return followed by a line feed. It is the conventional Windows text-file line ending and is a good choice for Windows message text and many external text outputs.vbLfrepresents a line feed. Excel worksheet cells use this character for an in-cell line break, so it is typically the right choice when composing a cell value with multiple visible lines.vbCrrepresents a carriage return alone. It is less commonly needed in ordinary Excel work and should be used only when the destination specifically expects it.vbNewLinerepresents the platform-appropriate newline. It is useful when the output should follow the environment’s line-ending convention rather than a specific character sequence.
For Excel for Microsoft 365 on Windows, vbCrLf is a clear choice for a message box, while vbLf is usually best for a worksheet cell. If a macro is intended to run on multiple operating systems or write data for a specific external application, consider the destination’s requirements rather than assuming every newline is interchangeable.
Choosing the right line break for the destination
A string can contain newline characters, but whether they appear as separate visible lines depends on the program or control displaying that string. A message box, worksheet cell, text file, and user form are different destinations; one line-ending choice does not guarantee identical rendering in all of them.
Use this quick guide when deciding which constant to use:
- For a Windows message box or a Windows-oriented text output, start with
vbCrLf. - For multiple lines within one worksheet cell, use
vbLfand enable text wrapping. - For code that should use the current platform’s conventional newline, consider
vbNewLine. - For data passed to another program, follow that program’s documented line-ending format.
If a receiving system expects a single specific character, using vbNewLine may be less explicit than using vbLf or vbCrLf. Conversely, hard-coding a Windows-specific sequence is unnecessary if the output is meant to adapt to the current platform. The destination, not the appearance of the VBA statement, should guide the choice.
Adding a new line to a message box
For a message box with several lines, put the line-break constant between the portions of text. You can also build the message over multiple statements, which can make longer messages easier to read and maintain:
Sub ShowStatusMessage()
Dim message As String
' Save a copy of the workbook before running this macro.
message = "The import is complete." & vbCrLf
message = message & "Rows added: 12" & vbCrLf
message = message & "Review the results before closing."
MsgBox message, vbInformation, "Import status"
End Sub
Each use of vbCrLf starts another displayed line. The last line does not need a newline after it unless you specifically need the string to end with a line break. Including a trailing newline can matter when writing to a file or combining this message with more text later, but is usually unnecessary for a simple message box.
In a message box, line breaks control where the text is displayed; they do not create separate buttons or message boxes. If the message is too long, consider shortening it or displaying details in a worksheet or user form instead.
Putting multiple lines in one worksheet cell
To store two lines in the same cell, join the text with vbLf. Turn on Wrap Text so Excel displays the lines within the cell, and adjust the row height if necessary. The following macro writes a two-line value to cell A1:
Sub WriteTwoLinesToCell()
Dim target As Range
' Save a copy of the workbook before running this macro.
Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
target.Value = "First line" & vbLf & "Second line"
target.WrapText = True
End Sub
Replace Sheet1 with the actual worksheet name if needed. The cell contains one value with an embedded line-feed character; it is not two separate cells or two separate records. In Excel for Microsoft 365 on Windows, you can also make a line break while editing a cell by pressing Alt+Enter. On Mac, the in-cell line-break shortcut differs by keyboard and Excel version; check Excel’s current shortcut help for your setup. The VBA method avoids depending on a manual keystroke.
Wrap Text affects display, not the stored string. A cell may contain the line-feed character even if wrapping is off, but the text may look clipped or remain on one visible line. If row height does not update as expected, use Home > Format > AutoFit Row Height, or adjust the row height manually.
Building a string from several lines or values
When a message includes changing values, concatenate each value with the appropriate line break. Convert non-text values to text where clarity requires it; VBA can often coerce numbers into strings, but explicit conversion makes the intention easier to see. For example:
Sub BuildSummary()
Dim itemCount As Long
Dim summary As String
' Save a copy of the workbook before running this macro.
itemCount = 12
summary = "Import summary" & vbCrLf & _
"Items processed: " & CStr(itemCount) & vbCrLf & _
"Status: Complete"
MsgBox summary, vbInformation, "Summary"
End Sub
The underscore at the end of a VBA code line is a line-continuation marker. It lets the programmer split a long statement across lines; it does not add a line break to the string. The actual displayed breaks in this example come from vbCrLf. This distinction is a frequent source of confusion: formatting the VBA source code across multiple lines does not automatically format the text created by the code.
For repeated pieces of text, a loop can assemble the result by appending a separator between entries. Be careful about whether the final output should have a trailing newline. A common approach is to add the separator before every item except the first, which avoids leaving an extra blank line at the end.
Common problems and how to fix them
If the string does not display on separate lines, check both the characters in the string and the behavior of the destination. The most common issues are straightforward to diagnose:
- The cell shows one continuous line: use
vbLffor the cell and turn on Wrap Text. Increase or autofit the row height if the second line is hidden. - The code itself appears on several lines, but the output does not: source-code line breaks and the VBA continuation character
_do not insert runtime newlines. AddvbCrLf,vbLf, or the appropriate constant to the string. - The output has an unexpected blank line: check whether two newline constants are next to each other, or whether the text already contains a line break before another one is added.
- A text file looks wrong in another application: confirm the file’s required line-ending format. Some tools expect a particular sequence, so a platform-specific default may not be suitable.
- Text appears as a small square or odd symbol: the destination may be showing a control character instead of interpreting it as a line ending. Check the target application’s behavior and use the newline convention it supports.
When debugging, inspect the string at the point where it is created and then test it in the actual destination. A line break can be present in the value but not visible because of formatting, cell dimensions, or how another application reads the text.
Newline constants versus Chr
You may see examples that use Chr(10) or Chr(13) & Chr(10) instead of named constants. These correspond to the line-feed and carriage-return characters, respectively. For a simple Excel macro, vbLf and vbCrLf communicate the intent more clearly and reduce the need to remember character numbers.
Character functions can still be useful when working with data that contains particular control characters, or when inspecting and cleaning imported text. For instance, a file may contain carriage returns and line feeds that need to be handled separately. Do not replace every occurrence blindly: removing line endings can merge records or paragraphs that were meant to remain separate.
Also distinguish line breaks from spaces. A space separates words on the same line; a newline starts a new line in a destination that recognizes the character. Concatenating text with & " " & will not produce a new line, and typing a slash sequence such as \n into a VBA string does not have the special meaning it has in some programming languages.
Frequently Asked Questions
How do I start a new line in a VBA string?
Concatenate the text with a newline constant, such as "First" & vbCrLf & "Second". Choose the constant based on where the text will be displayed or written.
Should I use vbCrLf or vbLf in Excel VBA?
Use vbCrLf for typical Windows message text and Windows-style line endings. Use vbLf for a line break inside an Excel worksheet cell.
Why does vbLf not show as a new line in my cell?
Confirm that the value contains vbLf, turn on Wrap Text, and make the row tall enough to show both lines. The line break may be stored even when the cell’s formatting hides its effect.
Does the VBA underscore add a new line to the string?
No. The underscore is a source-code continuation marker that allows a statement to continue on the next code line. Add a newline constant to the string to create a displayed line break.
Can I use vbNewLine in a worksheet cell?
You can use it in a string, but vbLf is the more direct choice for a worksheet cell’s in-cell line break. Use vbNewLine when you want a platform-appropriate newline for output whose line-ending convention should follow the environment.
Related
- Excel Notched Box Plot: What It Shows and How to Make One
- Excel Online Page Break Preview: Limits and Workarounds
- Excel Not Responding to Keyboard? Causes and Fixes
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Outlook Create Contact Group from ExcelYes. In classic Outlook for Windows, an Excel VBA macro can read email addresses from a worksheet, create…
- Excel VBA: How to Find the Next Empty Cell in a ColumnTo find the next empty cell below the last entry in an Excel column, use VBA’s End(xlUp) navigation…
- Set Image Height in Excel VBA Using InchesTo set an image to a height measured in inches, assign its VBA Height property the result of…
- VBA Loop Format: Syntax, Examples, and When to Use EachA VBA loop repeats a block of code while counting through values, visiting items in a collection, or…
- Excel VBA UserForm Code Examples and SetupExcel VBA UserForms are custom dialog boxes for collecting information or guiding a task; useful examples include opening…
- Excel VBA Copy Paste Transpose: Working Code and TipsTo copy a range and paste it transposed with Excel VBA, copy the source range and use PasteSpecial…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.