Sub topic 9
Line Breaks: Remove or Replace with a Space
This chapter assumes you can enter and copy formulas, use relative references such as A2, and tell text from numbers in a cell. It also assumes you know what TRIM, CLEAN, and PROPER do on their own. Here you will handle one specific problem. A cell contains a line break, and the way you remove it decides whether your words stay separate or run together.
What a line break is inside a cell
A line break is a character stored in the cell's text. It is not a row boundary and not a formatting setting. You create one in Excel by pressing Alt+Enter while editing a cell. Imported data brings them in too: addresses exported from a database, notes copied from a web form, or text pasted from an email.
Two characters can make up a line break:
- Line feed, character code 10. Excel itself uses this one. In a formula you write it as
CHAR(10). - Carriage return, character code 13. It often arrives in files from other systems. Windows files commonly use a carriage return followed by a line feed, so a single visual break can be two characters.
A cell with a break only looks like several lines when Wrap Text is on. With Wrap Text off, the break seems to vanish, and the cell shows one long line. This is why people often discover the problem only when a lookup fails or a sort looks wrong.
What CLEAN and TRIM do with line breaks
CLEAN removes characters with codes 0 through 31, and that range includes both 10 and 13. TRIM only touches the ordinary space, character 32. The two functions therefore behave differently on a line break.
Suppose A2 holds this text, with a line feed after "Lopez":
| Cell content | Meaning |
|---|---|
| `Maria Lopez` + line feed + `42 Oak Street` | Name and street on two lines |
Compare the formulas:
=TRIM(A2)TRIM leaves the line break alone, so the result still has two lines.
=CLEAN(A2)CLEAN deletes the line break and puts nothing in its place. The result is Maria Lopez42 Oak Street. The name and the street number are now joined, and the break is gone with no gap where it was.
This is the main reason CLEAN alone is the wrong tool for line breaks. It removes the character but also removes the separation that the character provided.
Replacing a line break with a space
To keep the words apart, use SUBSTITUTE to swap each line feed for a space, then let TRIM tidy the result.
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))SUBSTITUTE takes the text, the old text to find, and the new text to insert. Here the old text is CHAR(10) and the new text is one space. For the example above, the result is Maria Lopez 42 Oak Street.
TRIM is still useful in this formula. If the break had a space on either side, or if there were two breaks in a row, SUBSTITUTE would produce several spaces in a row. TRIM reduces every run to a single space and removes any at the start or end.
Why the order matters
Do the SUBSTITUTE step before CLEAN. Once CLEAN has run, the line breaks are already gone and there is nothing left to replace. Nesting works from the inside out, so the SUBSTITUTE goes innermost.
=TRIM(CLEAN(SUBSTITUTE(A2,CHAR(10)," ")))CLEAN still has a job here. It removes other non-printing characters that may be in the cell, such as tabs or stray control codes.
Handling carriage returns as well
If the data came from another system, the breaks may be character 13 or a 13-and-10 pair. Replacing only character 10 would leave the 13 behind. A leftover 13 would then be removed by CLEAN without a space, which brings back the joined-words problem.
Nest two SUBSTITUTE calls so that both characters become spaces:
=TRIM(SUBSTITUTE(SUBSTITUTE(A2,CHAR(13)," "),CHAR(10)," "))For a Windows-style pair, the 13 becomes one space and the 10 becomes another. TRIM then collapses the two spaces into one, so you get a single space for each visual break.
The following flowchart summarizes how to choose a treatment.
Detecting line breaks before you clean
Before you pick a formula, find out which characters are present. Two checks are useful.
To count line feeds in a cell, compare the length with the length after removing them:
=LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(10),""))A result of 0 means there are no line feeds. A result of 2 means two of them. Run the same formula with CHAR(13) to count carriage returns.
To see where the first line feed sits, use FIND. It returns an error when the character is not there, so wrap it in IFERROR:
=IFERROR(FIND(CHAR(10),A2),0)This returns the position of the first line feed, or 0 when there is none. You can fill it down the column and filter for values above 0 to list the affected rows.
If you are not sure which character you are looking at, use the technique from the previous chapter. Extract a character with MID, RIGHT, or LEFT and pass it to UNICODE. A result of 10 or 13 confirms a line break. A result of 160 means a non-breaking space instead.
=UNICODE(RIGHT(A2,1))Combining everything into one cleanup formula
A full cleanup for imported text deals with non-breaking spaces and line breaks first, then removes other control characters, then trims, then fixes capitalization. Here is one version:
=PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,CHAR(160)," "),CHAR(13)," "),CHAR(10)," "))))Read it from the inside out:
- Each SUBSTITUTE changes one problem character (160, then 13, then 10) into a normal space.
- CLEAN removes any remaining control characters.
- TRIM removes leading and trailing spaces and collapses repeated ones.
- PROPER standardizes capitalization.
Check the PROPER step against your data. As covered earlier, it mishandles some names and abbreviations, so inspect the results before relying on them.
Other replacement text
A space is not always the right substitute. For an address that you want on one line, a comma and a space may read better:
=TRIM(SUBSTITUTE(A2,CHAR(10),", "))Two things can go wrong with this version. A break at the very end of the cell leaves a trailing comma, because TRIM removes only the space after it. Two breaks in a row give two commas. If your data may have either pattern, first replace the breaks with spaces and trim, and add commas only where you know the structure.
You can also leave the text alone and change only how it displays. Turning on Wrap Text shows each break as a new line, which is the right choice when the breaks are intentional, such as in a multi-line address that you want to keep.
Breaks that CLEAN and CHAR(10) do not catch
Most cases involve codes 10 and 13, but a few exceptions are worth knowing:
| Character | Code | Handled by CLEAN? | Fix |
|---|---|---|---|
| Line feed | 10 | Yes, removes it | `SUBSTITUTE(A2,CHAR(10)," ")` |
| Carriage return | 13 | Yes, removes it | `SUBSTITUTE(A2,CHAR(13)," ")` |
| Vertical tab | 11 | Yes, removes it | `SUBSTITUTE(A2,CHAR(11)," ")` |
| Line separator | 8232 | No, outside 0 to 31 | `SUBSTITUTE(A2,UNICHAR(8232)," ")` |
The vertical tab sometimes comes from text pasted out of word processors, where it acts as a soft line break. The line separator, code 8232, comes from some web pages and PDF copies. CLEAN skips it because its code is above 31. If a cell still looks broken after your formula, run UNICODE on the characters near the problem to identify what is left.
=SUBSTITUTE(A2,UNICHAR(8232)," ")Using Find and Replace instead
If you only need a one-time cleanup and do not need a formula, Find and Replace can do the same work in place. Select the cells, press Ctrl+H, click in the Find what box, and press Ctrl+J. The box looks empty, but Ctrl+J enters a line feed. Put a single space in Replace with, then choose Replace All.
This changes the original cells directly, with no helper column and no way back except Undo. It also does not trim extra spaces, so you may still need TRIM afterward. When you want a record of what changed, or you plan to repeat the cleanup on new data, a formula in a helper column is the safer choice.
Common mistakes
- Using TRIM alone and expecting it to remove line breaks. It only handles the ordinary space.
- Using CLEAN alone when words sit on either side of the break.
- Replacing only
CHAR(10)when the data also containsCHAR(13). - Placing SUBSTITUTE outside CLEAN, so the breaks are already gone before it runs.
- Judging a cell by how it looks with Wrap Text off. Test with
LENor the count formula instead.
Exercise
Cell A2 contains "Red" followed by a line feed and then "Car". Write a formula that returns "Red Car" and explain why =CLEAN(A2) gives a worse result.
Show answer
Use =TRIM(SUBSTITUTE(A2,CHAR(10)," ")). SUBSTITUTE swaps the line feed for a space and TRIM cleans up any extra spaces. =CLEAN(A2) removes the line feed and leaves nothing in its place, so the result is "RedCar".
Exercise
A column came from a Windows system, so each break may be a carriage return followed by a line feed. Write a formula that replaces each break with one space, and explain why the result does not contain two spaces.
Show answer
Use =TRIM(SUBSTITUTE(SUBSTITUTE(A2,CHAR(13)," "),CHAR(10)," ")). The carriage return becomes one space and the line feed becomes another, giving two spaces in a row. TRIM collapses runs of spaces into a single space, so only one remains.
Exercise
How can you find out how many line feeds are in cell A2 without changing the cell?
Show answer
Use =LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(10),"")). The difference between the original length and the length with all line feeds removed equals the number of line feeds.