Sub topic 9
Line breaks: remove or replace with a space
This chapter assumes you can enter and copy formulas and know how TRIM, CLEAN, and PROPER behave on a single cell. It covers one specific problem: a line break inside a cell. You will decide whether to delete it or turn it into a space.
What a line break is
A line break is a character inside the cell. When you press Alt+Enter while typing, Excel inserts a line feed, which is character code 10. Text imported from web pages, databases, and CSV files often contains line feeds, and sometimes carriage returns (code 13) as well.
A cell with a line break can look fine. With Wrap Text off, Excel shows everything on one line, so the break is easy to miss. It still breaks lookups and comparisons, because "Ann Lee" does not match "Ann" + line feed + "Lee".
Why CLEAN alone can be wrong
CLEAN removes characters with codes 0 through 31, and that range includes line feeds. It deletes them without leaving anything behind.
=CLEAN(A2)If A2 holds "Ann", a line feed, then "Lee", the result is "AnnLee". The two words are now joined. TRIM cannot fix this, because it only handles spaces, not line feeds.
Choose: remove or replace
Decide based on what the break was doing.
| Situation | What the break meant | Best treatment |
|---|---|---|
| Break sits between words or lines of an address | A separator | Replace with a space |
| Break sits at the very end of the text | Stray character | Remove, or replace and let TRIM clean up |
| Break splits a word in the middle | Formatting artifact | Remove |
Most of the time, replacing with a space is the safer choice. A leftover extra space is easy to remove with TRIM. Joined words are hard to repair.
Replacing a line break with a space
Use SUBSTITUTE to swap CHAR(10) for a normal space, then TRIM to tidy the spacing.
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))The order matters. SUBSTITUTE must run before CLEAN. If CLEAN runs first, it deletes the line feed and the words are already joined.
Handling carriage returns too
Files that came from Windows systems may use a carriage return followed by a line feed. That is two characters per break. If you only replace code 10, the code 13 stays behind, and CLEAN would delete it later without harm. Replacing both gives a more predictable result.
=TRIM(SUBSTITUTE(SUBSTITUTE(A2,CHAR(13)," "),CHAR(10)," "))Here the pair becomes two spaces, and TRIM reduces them to one.
Checking whether a cell has line breaks
To count line feeds in a cell, compare its length with the length after removing them.
=LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(10),""))A result of 0 means the cell has none. A result of 2 means two line breaks. Fill this down a helper column to find which rows need attention before you clean anything.
Adding it to the full cleanup
Put the line break replacement innermost so it runs first. Then CLEAN removes other control characters, TRIM fixes spacing, and PROPER sets capitalization.
=PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,CHAR(13)," "),CHAR(10)," "))))If the data also has non-breaking spaces, add another SUBSTITUTE for CHAR(160) in the same innermost position.
Common mistakes
- Running CLEAN before SUBSTITUTE, which joins words.
- Expecting TRIM to remove line breaks. It only works on spaces.
- Replacing only CHAR(10) when the data also contains CHAR(13).
- Forgetting that the cleaned results are formulas. Convert them to values before deleting the original column.
Exercise
Cell A2 contains "12 Oak Street", a line feed, then "Springfield". Write a formula that returns "12 Oak Street Springfield" with a single space between the parts, and explain why =CLEAN(A2) is the wrong choice here.
Show answer
Use =TRIM(SUBSTITUTE(A2,CHAR(10)," ")). SUBSTITUTE replaces the line feed with a space, and TRIM removes any extra spaces. =CLEAN(A2) deletes the line feed without adding a space, so the result is "12 Oak StreetSpringfield".