Sub topic 6
Combining the Three Functions in One Formula
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 each do on their own.
Why combine them
Cleaning in three helper columns works, but it clutters the sheet. One nested formula does the same job in a single column. A nested formula is a formula that uses one function as the input to another.
Excel evaluates nested functions from the inside out. The innermost function runs first, and its result feeds the next one.
The basic formula
=PROPER(TRIM(CLEAN(A2)))Excel works through it in this order:
CLEAN(A2)removes non-printing characters with codes 0 through 31.TRIM(...)removes leading and trailing spaces and reduces runs of spaces between words to one space.PROPER(...)capitalizes the first letter of each word and lowercases the rest.
Why the order matters
The order is a convention, not a hard rule. Still, this order is the usual choice for a basic cleanup, for these reasons:
- CLEAN first means hidden characters are gone before TRIM looks at the spacing.
- TRIM before PROPER means the capitalization step works on already tidy text.
- PROPER last means nothing later can disturb its result.
A worked example
Suppose A2 contains jOHN smith with two leading spaces and three spaces between the words. Each step changes it like this:
| Step | Formula | Result |
|---|---|---|
| Raw | `=A2` | ` jOHN smith` |
| After CLEAN | `=CLEAN(A2)` | ` jOHN smith` |
| After TRIM | `=TRIM(CLEAN(A2))` | `jOHN smith` |
| After PROPER | `=PROPER(TRIM(CLEAN(A2)))` | `John Smith` |
CLEAN changed nothing here because the cell had no control characters. Including it is harmless, and it protects you when imported data does contain them.
Adding SUBSTITUTE for non-breaking spaces
Web pages and some exports use a non-breaking space, which is character 160. TRIM does not remove it. Put SUBSTITUTE innermost to convert it to a normal space first:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160),anything))))That draft is wrong, because the replacement must be a text string. The correct version is:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))SUBSTITUTE goes innermost because TRIM can only remove a space it recognizes. Once character 160 has become a normal space, TRIM handles it along with the other extra spaces.
Common errors
- Reversed order.
=TRIM(PROPER(...))usually gives the same visible result, but putting TRIM or CLEAN outside SUBSTITUTE means the non-breaking spaces survive. - Joined words. CLEAN removes a line feed without adding a space, so two lines of text can run together. To avoid this, replace line feeds with spaces first:
=TRIM(SUBSTITUTE(A2,CHAR(10)," ")). - Wrong capitalization from PROPER. PROPER turns "it's" into "It'S". The fix
=SUBSTITUTE(PROPER(A2),"'S","'s")also changes names such as O'Sullivan, so check the results. - Pasting over the source. If you replace the original column with formulas that refer to it, you create a circular reference. Keep the formulas in a separate column, then convert them to values.
Checking your result
To see whether a cell still has extra spaces, compare lengths:
=LEN(A2)-LEN(TRIM(A2))A result above zero means TRIM would remove something. To find out whether a stubborn trailing character is a non-breaking space, look at its code:
=UNICODE(RIGHT(A2,1))A result of 160 confirms it, and the SUBSTITUTE version of the formula is the one you need.
Exercise
Cell A2 contains mARY o'neil followed by a non-breaking space (character 160). Write one formula that returns a cleaned version, and state which function must be innermost and why.
Show answer
Use =PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," ")))). SUBSTITUTE must be innermost because TRIM does not remove character 160. Converting it to a normal space first lets TRIM remove it. The result is Mary O'Neil.
Exercise
Why can =CLEAN(A2) join two words that were on separate lines, and what formula avoids this?
Show answer
CLEAN deletes the line feed (character 10) without inserting a space. Replace line feeds with spaces first, for example =TRIM(SUBSTITUTE(A2,CHAR(10)," ")).