In Excel VBA, join text with vbCrLf or vbNewLine to put it on separate lines in a message or string; for a line break inside an Excel cell, use vbLf (the line-feed character, also written as Chr(10)). The right choice depends on where the text will appear: a worksheet cell, a message box, or the VBA editor each handles new lines differently.
Which VBA New-Line Constant Should You Use?
VBA provides several built-in constants for control characters. They look similar in code, but they do not always have the same purpose:
vbLfis a line feed, equivalent toChr(10). It is the usual choice for inserting a visible line break in an Excel cell.vbCrLfcombines a carriage return and a line feed, equivalent toChr(13) & Chr(10). It is a common Windows text-file and message-box line ending.vbCris a carriage return, equivalent toChr(13). It is less commonly needed for ordinary Excel cell text.vbNewLinerepresents the platform’s newline convention. It is useful when text is intended to follow the current platform’s convention rather than a specific character sequence.
For code that writes into a worksheet cell, choose vbLf. For readable messages or general text that will be shown outside a cell, vbNewLine is a convenient choice; on Windows, vbCrLf is also widely used. If another application or file format requires a particular line-ending convention, use the exact character sequence that format expects.
Put a New Line Inside an Excel Cell
To create a multi-line cell with VBA, place vbLf between the text portions, then enable text wrapping so the separate lines are visible. This example writes two lines to cell A1 on a worksheet named Sheet1:
Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z, and this macro replaces the existing value in A1.
Sub WriteLinesToCell()
Dim target As Range
' Point to the cell that will receive the text.
Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
target.Value = "First line" & vbLf & "Second line"
target.WrapText = True
End Sub
After the macro runs, A1 contains “First line” followed by “Second line” on the next line. WrapText = True lets Excel display the line break within the cell. Depending on the row’s current height, the second line may not be fully visible at once; AutoFit the row or adjust its height if necessary. The line break is part of the cell’s text, not a new row or a new cell.
If the text already exists and you want to add another line, append the separator instead of replacing the value. For example, with a target cell assigned to a Range variable, use target.Value = target.Value & vbLf & "Additional detail". Check whether the cell is empty first if you do not want a blank first line when adding text to an empty cell.
Build a Multi-Line Message Box
For a message shown with MsgBox, use vbNewLine or vbCrLf between message parts. The constant is included in the text passed to the message box; it does not create a second dialog. For example, this macro displays a heading and a separate status line:
Save a copy before running this or any macro that changes workbook data. This particular example only displays a message.
Sub ShowStatusMessage()
Dim messageText As String
' Separate the heading from the status line.
messageText = "Import complete." & vbNewLine & "Rows added: 12"
MsgBox messageText, vbInformation, "Import status"
End Sub
The message appears on two lines, with the title Import status and the information icon. Replace the example text with variables or values from your own procedure. For instance, a line break can make a long warning easier to scan, but keep the message concise so users can understand what action is needed.
Use New Lines in Longer VBA Strings
The same approach works when assembling text for a report, an email body, a log entry, or another destination. The important distinction is between creating a string that contains a newline character and formatting the VBA source code across multiple lines. A string can contain vbLf or vbNewLine even though the VBA statement itself remains on one source line.
For example, to build a two-line text value before assigning it elsewhere, declare a String variable and concatenate the pieces with the chosen separator. Use vbLf when the destination is a worksheet cell, and select a platform-appropriate separator for text files or other applications. Concatenation with & is usually clearer than relying on implicit conversions when one or more pieces are numeric or come from a variable.
A new line inside text is not the same as a line continuation in VBA code. To split one long VBA statement across source lines, VBA uses a space followed by an underscore at the end of the continued line. The underscore tells the editor that the statement continues; it does not put a newline character into the resulting string. For example, a long assignment can be continued for readability, while the string still needs an explicit vbLf or vbNewLine where the output should break.
New Lines in a Worksheet Formula and Manual Entry
You can also create a cell line break with a worksheet formula, although VBA is the better fit when a macro is already generating the text. With the word North in A2 and South in B2, enter =A2&CHAR(10)&B2 in C2. The result is North on the first line and South on the second line; turn on Wrap Text for C2 to display both lines. CHAR(10) returns the line-feed character used for this cell break.
For a one-off manual edit, put the cell into edit mode, position the insertion point where the break belongs, and use the in-cell line-break shortcut. In Excel for Microsoft 365 on Windows, press Alt+Enter. On Excel for Mac, the shortcut is generally Control+Option+Return; keyboard layouts and versions can affect the exact key combination, so check Excel’s current shortcut help if it does not work. This inserts a break in the cell’s text without requiring VBA.
Common Problems and How to Fix Them
- The text appears on one line: Confirm that the string contains
vbLfand that the cell has Wrap Text enabled. A break can be present even if the row height hides part of the text. - The macro creates a blank line: Check for two newline constants in a row, or for a string that already ends with a newline before another is appended.
- The macro erases existing content: An assignment such as
target.Value = "New text"replaces the cell’s prior value. To append, concatenate the current value and new text, and handle empty cells deliberately. - Text copied to another program looks different: Applications and file formats may expect different line endings. A cell’s line feed may not be interpreted the same way by every destination. Choose
vbCrLfor another required convention when the receiving application specifies one. - VBA reports a syntax error: Make sure each string literal has matching quotation marks and that the concatenation operator is present between text and the newline constant. A source-code continuation underscore is not a substitute for a string newline.
For a cell that displays unexpectedly, inspect both the value and the formatting. The break character belongs to the value, while wrapping and row height affect how much of that value is visible. Changing the row height does not remove the break, and turning on Wrap Text does not insert one by itself.
Choosing Between VBA, a Formula, and a Shortcut
Use the method that matches the task rather than adding a macro unnecessarily:
- Use VBA when a procedure creates or updates multi-line text repeatedly, writes to many cells, or builds messages as part of an automated workflow.
- Use a formula when the displayed text should update automatically as source cells change. The formula result is text, so apply Wrap Text where you want to see the lines.
- Use the keyboard shortcut for an occasional line break entered by hand. It is direct and does not require macro permissions or code.
Excel for the web does not run VBA macros. A workbook containing VBA can be opened there, but its macro code cannot be run in the browser; use the desktop Excel application to execute VBA. Excel for Mac has VBA support in the desktop application, though some APIs and automation integrations differ from Windows, so platform-dependent code should be checked on the target system.
Frequently Asked Questions
What is the VBA code for a new line in an Excel cell?
Concatenate the text with vbLf, such as "First" & vbLf & "Second", and set the cell’s WrapText property to True so the lines display.
Should I use vbCrLf or vbLf in Excel VBA?
Use vbLf for a line break within worksheet cell text. Use vbCrLf for the conventional Windows carriage-return-plus-line-feed sequence, or vbNewLine when you want the platform’s newline convention.
Why does my cell not show the second line?
Turn on Wrap Text and check the row height. The line-feed character may already be in the cell even if the current formatting or height prevents both lines from being visible.
Can I use Alt+Enter in VBA?
Alt+Enter is a keyboard shortcut for inserting a break during manual cell editing in Windows Excel; it is not VBA syntax. In code, put vbLf or the appropriate newline constant into the string.
Can I undo a VBA line-break macro with Ctrl+Z?
No. Running a macro clears Excel’s normal undo history, so Ctrl+Z generally cannot reverse its changes. Save a separate copy before running a macro that edits cells.
Related
- Data Analysis in Excel: Methods, Steps, and Examples
- Excel Quick Analysis Button: What It Does and How to Use It
- Excel Solver: Find a Target Sum with Changing Cells
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Scroll Bar Form Control: Setup, Uses, and CodeIn Excel for Microsoft 365 on Windows, insert a Form Control scroll bar from Developer > Insert >…
- Excel Macro Message Box: How to Create and Use OneAn Excel macro message box is a pop-up created with VBA’s MsgBox function. In Excel for Microsoft 365…
- How to Delete Blank Rows in Excel with a VBA MacroTo delete blank rows in Excel with a macro, select the data range, run a VBA procedure that…
- Excel SUBSTITUTE in VBA: Replace or WorksheetFunctionTo substitute text in Excel VBA, use VBA’s built-in Replace function; it replaces every matching occurrence by default.…
- VBA Range Column: How to Reference ColumnsIn VBA, reference a whole worksheet column with Columns("C") or Range("C:C"); to get the column containing a particular…
- Excel VBA: How to Open the Editor and Run Macros SafelyIn Excel for Microsoft 365 on Windows, press Alt+F11 to open the VBA editor, or choose Developer >…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.