How to Insert a Line Break in Excel VBA
In this article
Inserting a line break in Excel VBA lets you display multi-line text in cells, message boxes, or user forms — essential for readable output when combining dynamic data or formatting reports. This is done using the vbCrLf constant, Chr(10), or Chr(13) & Chr(10), depending on your needs. No add-ins or external tools are required — this works in all modern versions of Excel with VBA enabled.
What You'll Need
- Microsoft Excel with VBA enabled (Excel 2010 or later recommended)
- Developer tab visible (enable via File > Options > Customize Ribbon > check Developer)
- A worksheet where you want to display multi-line text
- Basic understanding of VBA syntax and the Immediate Window or Module editor
Step-by-Step
- Open your Excel workbook and press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the Project Explorer window on the left, find your workbook (e.g., VBAProject (YourWorkbook.xlsx)), right-click it, and select Insert > Module. This creates a new blank code module.
- Inside the new module, type or paste the following code to insert a line break in a cell:
Range("A1").Value = "First line" & vbCrLf & "Second line" - Ensure the cell you’re writing to has Wrap Text enabled. Select the target cell (e.g., A1), go to the Home tab, and click Wrap Text in the Alignment group. Without this, the line break won’t visually appear even though it’s there.
- Press F5 while your cursor is inside the subroutine to run the code, or close the editor and return to Excel, then press Alt + F8, select your macro (e.g., Macro1), and click Run.
- To insert a line break in a Message Box, use:
MsgBox "Line one" & vbCrLf & "Line two". This will display the text across two lines in the popup dialog. - For UserForms, assign multi-line text to a Label or TextBox control using the same syntax:
Label1.Caption = "Header" & vbCrLf & "Details here". Set the TextBox’s MultiLine property to True in the Properties window for it to display breaks correctly. - To insert a line break in a cell using a variable, declare a string variable:
Dim text As String: text = "Item 1" & vbCrLf & "Item 2" & vbCrLf & "Item 3": Range("B2").Value = text - To verify the line break was inserted, click into the cell and look at the Formula Bar — you’ll see a small arrow indicating a line break. You can also use the Find function (Ctrl + F) and search for ^l (caret + lowercase L) to locate line breaks in the sheet.
- If you need to add line breaks to multiple cells in a loop, use a For Each or For Next loop. Example:
For i = 1 To 5: Cells(i, 1).Value = "Row " & i & vbCrLf & "Updated" & vbCrLf & "Done": Next i
Common Mistakes
- Forgetting to enable Wrap Text on the cell — the line break exists but is invisible because Excel treats the cell as single-line by default.
- Using Chr(10) alone on Windows — this may cause odd rendering in some contexts. Use vbCrLf (which is Chr(13) & Chr(10)) for maximum compatibility.
- Typing vbCrLf as "vbCrLf" (with quotes) — this treats it as a literal string instead of the constant, so it prints the text "vbCrLf" instead of creating a break.
- Using Chr(13) alone — this is a carriage return and may not produce a visible line break in Excel cells; it’s often paired with Chr(10) but vbCrLf is preferred.
- Trying to insert line breaks into merged cells — merged cells often ignore formatting rules like Wrap Text, making line breaks unreliable. Avoid merging cells when using multi-line text.
- Calling the macro from a cell formula — VBA functions can’t directly modify other cells unless they’re array functions or called via events. Use macros triggered by buttons or events, not worksheet formulas.
Tips
- Use vbCrLf for Windows compatibility — it’s the standard and works consistently across Excel versions.
- When building dynamic strings, build them step-by-step in a variable before assigning to a cell. This makes debugging easier.
- Use the Immediate Window (Ctrl + G in VBA editor) to test line break syntax: type
Debug.Print "Test" & vbCrLf & "Line 2"and press Enter to see output. - For CSV exports, line breaks inside cells can cause parsing errors. If exporting data, consider replacing vbCrLf with a pipe (|) or comma before export.
- Use the Len() function to count characters in a cell with line breaks — it includes the carriage return and line feed characters, so a 20-character string with one line break will return 22.
- If you’re copying text from Word or another source into a VBA string, paste it into Notepad first to strip hidden formatting that might interfere with line breaks.
- When working with UserForms, always set TextBox.MultiLine = True and adjust ScrollBars to fmScrollBarsVertical for better user experience.
Troubleshooting
- Line break doesn’t show up in the cell. First, confirm Wrap Text is turned on. Then check the cell’s row height — if it’s too short, the second line may be hidden. Double-click the row boundary to auto-fit. If still invisible, check the Formula Bar: you should see a small down arrow indicating a line break.
- Code runs but nothing changes. Verify you’re targeting the correct cell. Use
Range("A1").Selectafter your assignment to see if the cell is highlighted. Ensure the macro is saved in the correct workbook and not in a personal macro workbook that’s disabled. - Line breaks appear as squares or question marks. This usually means the font doesn’t support the character or the cell is corrupted. Try changing the font to Calibri or Arial. Also, check if the cell was previously formatted with custom number formatting — reset it to General.
- Macro is greyed out or won’t run. Make sure macros are enabled: File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros (temporarily for testing). Also, save the file as .xlsm (Macro-Enabled Workbook).
- Line breaks work in MsgBox but not in cells. This is likely due to Wrap Text being off. Message boxes handle line breaks automatically; cells require explicit formatting.
- Getting a compile error on vbCrLf. Make sure you’re not enclosing it in quotes. Also, ensure you’re not using it in a context where constants are invalid (e.g., some property assignments). Use Chr(13) & Chr(10) as an alternative if needed.