Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA String Line Break: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert a line break inside a string in Excel VBA, use the built-in constant vbCrLf (carriage return + line feed) or vbLf (line feed only) and concatenate it between the text segments. For example, MsgBox "Line 1" & vbCrLf & "Line 2" displays two lines. The same approach works when writing to a cell: set the cell's WrapText property to True so the break is visible. This guide walks through the exact syntax, where to use each constant, and how to avoid the common pitfalls that make breaks disappear.

What You'll Need

  • Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
  • Basic familiarity with the VBA editor (Alt+F11).
  • A workbook saved as .xlsm if you want to keep your macros.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab > Visual Basic). In the Project Explorer, right-click your workbook and choose Insert > Module. Labels in the editor can differ slightly by Excel version and platform, but the Insert menu is consistent.
  2. Decide which break constant to use. Use vbCrLf when the string will be shown in a message box, written to a text file for Windows, or placed in a cell where you want the standard Windows line ending. Use vbLf when the target is a cell that will be read by Excel's own line-break logic (Excel stores in-cell breaks as a line feed) or when sending text to a Unix-based system. vbNewLine is platform-dependent and generally maps to vbCrLf on Windows, so it is safe but less explicit.
  3. Build the string with concatenation. In the module, type a simple procedure:
    Sub ShowBreak()
        Dim msg As String
        msg = "First line" & vbCrLf & "Second line"
        MsgBox msg
    End Sub
    Press F5 to run it. The message box shows two lines. If you see a single line with a small square or nothing, the constant was probably typed as text (for example "vbCrLf" inside quotes) instead of the built-in constant.
  4. Insert a break into a cell value. Write a procedure that assigns the string to a cell and turns on wrapping:
    Sub CellBreak()
        Dim s As String
        s = "Name: Alice" & vbLf & "Role: Editor"
        With Range("A1")
            .Value = s
            .WrapText = True
        End With
    End Sub
    Run it. Cell A1 now shows two lines. The WrapText line is essential: without it, Excel may display the break as a space or hide the second line depending on column width. You can also set WrapText manually via Home > Wrap Text.
  5. Use a break inside a formula string. If you need a formula that contains a line break, use CHAR(10) in the formula text. In VBA, build the formula like this:
    Range("B1").Formula = "=\"Line 1\" & CHAR(10) & \"Line 2\""
    Then set Range("B1").WrapText = True. CHAR(10) is the line feed character that Excel recognises inside cells.
  6. Write breaks to a text file. When using Print # or Write #, include vbCrLf explicitly if you want a blank line or a break inside a record. For line-by-line output, Print # already adds a line terminator; adding vbCrLf at the end of each string will create double spacing. Test with a small file first.
  7. Remove or replace existing breaks. To strip breaks from a string, use Replace:
    clean = Replace(original, vbCrLf, " ")
    clean = Replace(clean, vbLf, " ")
    Run both replacements because a string may contain either form. To turn breaks into a different separator, replace with "; " or another delimiter.
  8. Check the result in the Immediate Window. Press Ctrl+G in the VBA editor and type ? Range("A1").Value to see the stored string. A line break appears as a small square or as separate lines depending on the font; you can also use ? Len(Range("A1").Value) to confirm the break characters are present.

Common Mistakes

  • Quoting the constant. Writing "vbCrLf" inserts the literal letters v-b-C-r-L-f, not a break. Remove the quotes.
  • Forgetting WrapText. A cell can contain a line feed but display it as a single line if wrapping is off. Always set WrapText = True when the break must be visible.
  • Using the wrong constant for the target. vbCrLf in a cell sometimes shows an extra blank line or a box depending on how the string was created; Excel's own in-cell breaks are line feeds. If a cell shows odd characters, try vbLf.
  • Double breaks in text files. Print # adds its own line terminator, so appending vbCrLf creates blank lines between records.
  • Assuming a break survives a copy. Copying a cell value to another application may convert the break to a space or remove it. Test the destination.

Tips

  • Define a module-level constant such as Const BR As String = vbCrLf to keep long concatenations readable.
  • Use Join with an array when building many lines: msg = Join(Array("A", "B", "C"), vbCrLf).
  • For message boxes, vbCrLf is the most reliable choice on Windows; on Mac Excel, vbLf or vbNewLine may behave more consistently.
  • When debugging, replace the break with a visible marker like "|" temporarily to see exactly where it sits.
  • Remember that Excel's formula bar shows a line feed as a small square; that is normal and does not mean the break is wrong.

Troubleshooting

If the break does not appear where expected, work through these checks in order. First, confirm the constant is unquoted and spelled correctly. Second, verify WrapText is True on the destination cell and that the row height is tall enough; a fixed row height can clip the second line. Third, inspect the actual string with Len and InStr in the Immediate Window to see whether the break characters exist. Fourth, if the string came from another source, it may contain Chr(13) only, Chr(10) only, or both; normalise it with two Replace calls. Fifth, if a message box shows a box character, the font may not render the break; switch to a standard font or test with Debug.Print. Finally, if the macro runs without error but nothing changes, check that you are writing to the intended sheet and that the cell is not protected or part of a merged range that hides the result.

Frequently Asked Questions

What is the difference between vbCrLf and vbLf?

vbCrLf is a carriage return followed by a line feed (two characters). vbLf is a single line feed. Windows text files traditionally use vbCrLf; Excel cells store in-cell breaks as a line feed, so vbLf often works best inside cells.

Why does my cell show a small square instead of a line break?

The square is how Excel displays a line feed in the formula bar. In the cell itself, the text should wrap to a new line if WrapText is on. If it does not, enable wrapping and increase row height.

Can I insert a line break with a keyboard shortcut in VBA?

No, VBA code is text; you insert breaks with constants or Chr functions. In a cell you can press Alt+Enter manually, but that is not a VBA shortcut.

How do I split a string that already contains line breaks?

Use Split(text, vbCrLf) after normalising with Replace to convert all break types to one form. Then loop through the resulting array.

Does vbNewLine work the same as vbCrLf?

On Windows, vbNewLine returns vbCrLf. On other platforms it may return a different sequence, so use the explicit constant when you need predictable behaviour.

Why do I get double spacing in a text file?

Print # adds a line terminator automatically. If you also append vbCrLf, each record ends with two breaks. Remove the extra constant or use Write # as appropriate.