Sub topic 6
Combining the three functions in one formula
This chapter assumes you can enter and copy formulas, understand relative references such as A2, and know the difference between text and numbers in a cell. It also assumes you have met TRIM, CLEAN, and PROPER individually. Here you will nest them into one formula, choose a sensible order, and handle the cases where the basic combination is not enough.
Why combine the functions
Cleaning a column with three separate helper columns works, but it clutters the sheet. Each step needs its own column, its own fill-down, and its own check. A single nested formula does the same work in one cell and leaves only one result column to review.
The three functions each fix a different problem:
| Function | Fixes | Leaves alone |
|---|---|---|
| CLEAN | Non-printing characters with codes 0 to 31, such as tabs and line feeds | Non-breaking spaces (code 160), normal spaces, capitalization |
| TRIM | Leading and trailing spaces, and runs of spaces between words | Non-breaking spaces, other control characters, capitalization |
| PROPER | Inconsistent capitalization | Spacing and hidden characters |
Because the problems are independent, you can chain the functions. The only question is the order.
The basic nested formula
Excel evaluates a nested formula from the inside out. The innermost function runs first, and its result becomes the argument of the next function out.
=PROPER(TRIM(CLEAN(A2)))Read this formula from the inside:
CLEAN(A2)removes control characters from the text in A2.TRIM(...)removes extra spaces from the cleaned text.PROPER(...)standardizes the capitalization of the trimmed text.
Suppose A2 contains the text jOHN smith with two leading spaces, three spaces between the words, and a tab character at the end. The result is John Smith.
Why the order matters
The order CLEAN, then TRIM, then PROPER is the usual choice, and there are reasons for each position.
- CLEAN before TRIM. A control character can sit between a space and a word. If CLEAN runs first, the control character disappears and TRIM sees the real spacing. If TRIM runs first, it may leave a space that only becomes redundant after CLEAN removes the character next to it.
- PROPER last. PROPER does not change spacing, so it can run at any point for that purpose. Running it last means it works on the final text and you only apply it once.
Capitalization and spacing do not interact, so swapping PROPER and TRIM gives the same output. The important rule is that CLEAN and the space-handling step come before you judge the result.
Checking whether a cell needs cleaning
Before you apply the formula to a whole column, you can test whether extra spaces are present. This formula counts how many characters TRIM would remove.
=LEN(A2)-LEN(TRIM(A2))A result of 0 means TRIM would change nothing. A positive number means the cell has leading, trailing, or repeated spaces. Fill this formula down next to your data to find which rows have a problem.
This check has a limit. It only measures what TRIM can remove, so a cell whose only problem is a non-breaking space shows 0 even though the text is still dirty.
Adding SUBSTITUTE for non-breaking spaces
Text copied from web pages often contains non-breaking spaces, which are character 160. They look like spaces, but TRIM and CLEAN both ignore them. To find out whether a cell ends with one, return the code of its last character.
=UNICODE(RIGHT(A2,1))A result of 160 confirms a trailing non-breaking space. A normal space returns 32.
To handle these, convert character 160 into a normal space before the other functions run. Then TRIM can treat it like any other space.
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))Wait, the quotation marks above must be plain. The correct formula is:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))SUBSTITUTE is the innermost function, so it runs first. The four-function chain now reads as: replace non-breaking spaces, remove control characters, remove extra spaces, fix capitalization.
Handling line breaks inside the formula
Line breaks in cells are usually line feeds, which are character 10. CLEAN removes them without leaving a space. For a cell that reads Anna on one line and Lee on the next, CLEAN gives AnnaLee.
When the line break was acting as a separator between words, replace it with a space instead.
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))You can add this to the full formula. Do the SUBSTITUTE steps first, then CLEAN for the remaining control characters.
=PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,CHAR(160)," "),CHAR(10)," "))))The formula now has two SUBSTITUTE calls. The inner one handles non-breaking spaces, the outer one handles line feeds, and CLEAN picks up anything else below code 32.
Use this version when line breaks separate words, such as addresses typed on several lines. Use the plain CLEAN version when the line breaks are accidental and should simply disappear.
A worked example
Here is a small data set in column A, with the formula in B2 filled down.
| Row | Raw value in A | What is wrong | Result in B |
|---|---|---|---|
| 2 | ` mARIA gonzalez ` | Extra spaces, mixed case | `Maria Gonzalez` |
| 3 | `DAVID` followed by a line feed and `OKAFOR` | Line break between words | `David Okafor` |
| 4 | `priya` followed by a non-breaking space and `NAIR` | Character 160 | `Priya Nair` |
Use the full formula from the previous section in B2:
=PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,CHAR(160)," "),CHAR(10)," "))))Copy it down through B4. Because A2 is a relative reference, each row points at its own source cell. Spot-check a few results with =LEN(B2) and compare the length with what you expect. If a result looks right but a lookup against it still fails, test the last character with UNICODE before assuming the formula is wrong.
A known problem with PROPER
PROPER capitalizes the first letter of each word and any letter that follows a non-letter character. That includes the apostrophe, so it's becomes It'S. You can patch that specific case.
=SUBSTITUTE(PROPER(A2),"'S","'s")This fix has side effects. It also changes names such as O'Sullivan to O'sullivan, because the pattern 'S appears there too. Names like McDonald, DeShawn, and abbreviations such as USA have similar problems, since PROPER lowercases every letter it does not capitalize. Review the output before you rely on it, and correct exceptions by hand or with targeted SUBSTITUTE calls.
Next steps with the result
A nested formula still depends on the original column. If you delete column A, column B shows #REF! errors. Once you are satisfied with the results, copy column B and use Paste Special, then Values, to replace the formulas with static text. Later chapters cover that step and what happens to numbers that remain text.
Common errors
- Result still has a gap. A non-breaking space is probably left. Check with
=UNICODE(RIGHT(A2,1))and add the SUBSTITUTE step. - Words run together. CLEAN removed a line feed. Replace character 10 with a space before CLEAN runs.
- Names with apostrophes look wrong. PROPER treats the letter after an apostrophe as a new word.
- Formula shows
#NAME?. A function name is misspelled, or smart quotes were pasted in place of plain quotation marks. - Wrong cell in the result. The reference was not copied correctly, so check that it is relative and that each row points to its own source.
Exercise
A2 contains ROBERT followed by a line feed and o'brien, with a non-breaking space after the last letter. Write one formula that returns Robert O'Brien, and say which step handles each problem.
Show answer
Use =PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,CHAR(160)," "),CHAR(10)," ")))). The inner SUBSTITUTE turns the non-breaking space into a normal space. The outer SUBSTITUTE turns the line feed into a space so the words stay apart. CLEAN removes any other control characters. TRIM removes the extra and trailing spaces. PROPER capitalizes each word, and because the letter after the apostrophe is capitalized, O'Brien comes out correctly.
Exercise
Why does a cell with only a trailing non-breaking space show 0 in =LEN(A2)-LEN(TRIM(A2)), and how can you confirm the real problem?
Show answer
TRIM does not remove character 160, so the length does not change and the difference is 0. Use =UNICODE(RIGHT(A2,1)). A result of 160 confirms a non-breaking space.