TRIM, CLEAN and PROPER each fix a different problem: extra spaces, invisible characters and inconsistent capitals. Run in a helper column, they turn a messy import into a usable list in seconds. They will not catch everything, though, and the order you apply them in changes the result.
The formula most teams need is =PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,UNICHAR(10)," "),UNICHAR(160)," ")))). It has four operations, and the sequence matters. This article explains what each function does, where it fails, how to choose between it and other tools, and how to apply it to a 500-row customer import from start to finish.
What each function does, and what it leaves alone
TRIM removes surplus spaces, but only ordinary ones
TRIM does two things. It strips spaces from the start and end of a value, and it reduces any run of internal spaces to one. The text " Mary Jones " is fifteen characters long. After TRIM it is "Mary Jones", ten characters.
TRIM only recognises the standard space character (code 32). A non-breaking space (NBSP), which web pages and some accounting exports insert, passes straight through. A cell that looks clean can still fail a lookup because of one of these.
CLEAN removes control characters, and it does not add spaces
CLEAN removes the first 32 codes in the character set. These are control characters, including tab (code 9), line feed (code 10) and carriage return (code 13). They are the characters that appear when text is copied from a text file, or when a cell contains a line break entered with Alt+Enter.
CLEAN does not insert a space where it removes a line break. "Flat 3[line break]High Street" becomes "Flat 3High Street". That is why the formula below replaces line breaks with a space before CLEAN runs.
CLEAN also does not remove NBSP, which is code 160 and sits outside the range it handles.
PROPER sets capitals, and it makes assumptions
PROPER capitalises the first letter of each word and lowercases the rest. "ACME LTD" becomes "Acme Ltd". "jean-luc picard" becomes "Jean-Luc Picard".
It treats digits, spaces, hyphens, apostrophes and brackets as word breaks. That makes "3rd floor" into "3Rd Floor" and "don't" into "Don'T". Section 6 covers these cases in detail.
Quick reference
| Function | Acts on | Leaves alone | Input example | Output |
|---|---|---|---|---|
| TRIM | Leading, trailing and repeated spaces (code 32) | Non-breaking spaces, tabs, line breaks | " Acme Ltd " | "Acme Ltd" |
| CLEAN | Codes 0 to 31, including tab and line feed | Non-breaking spaces, zero-width spaces | "Flat 3[LF]High St" | "Flat 3High St" |
| PROPER | Letter case, word by word | Spacing and punctuation | "mcDONALD" | "Mcdonald" |
| UPPER | Every letter to capitals | Spacing and punctuation | "sw1a 1aa" | "SW1A 1AA" |
| LOWER | Every letter to lower case | Spacing and punctuation | "Sales@Acme.co.uk" | "sales@acme.co.uk" |
| SUBSTITUTE with UNICHAR(160) | Non-breaking spaces only | Everything else | "ACME[NBSP]LTD" | "ACME LTD" (after replacing with a space) |
Skip the setup
Every client, lead and follow-up in one sheet, with drop-downs and status colours already set up.
Glossary
- Text string: any value stored as text, including numbers that have been entered or converted as text.
- Whitespace: characters that take up space but show nothing, such as spaces, tabs and line breaks.
- Non-printing character: a control character with no visible form. CLEAN handles the common ones.
- Non-breaking space (NBSP): a space that stops a line from wrapping. It is Unicode code 160, and it looks identical to a normal space.
- Zero-width space: Unicode code 8203. It is invisible and not removed by any of the three functions.
- Helper column: a column of formulas beside the raw data. It is kept separate so the source can be checked.
- Paste as values: replacing formulas with their results, so the cleaned text no longer depends on the raw column.
Diagnose the text before you fix it
Run diagnostics first. Fixing text you have not measured means you cannot prove the fix worked.
SOP 1.1: Count extra spaces. In a check column, enter =LEN(A2)-LEN(TRIM(A2)). The result is the number of characters TRIM would remove. Zero means the cell is clean for this problem.
SOP 1.2: Count control characters. Enter =LEN(A2)-LEN(CLEAN(A2)). Any value above zero means tabs or line breaks are present.
SOP 1.3: Count non-breaking spaces. Enter =LEN(A2)-LEN(SUBSTITUTE(A2,UNICHAR(160),"")). This is the check most people skip, and it is the one that most often explains a failed lookup.
SOP 1.4: Count zero-width spaces. Enter =LEN(A2)-LEN(SUBSTITUTE(A2,UNICHAR(8203),"")).
Use UNICHAR rather than CHAR for these checks. CHAR(160) returns NBSP on Windows, but on Mac Excel the same code maps to a different character, so the formula behaves differently across machines. UNICHAR gives the same character on both, and Google Sheets supports it too.
Once the check columns are filled, filter for values above zero. Sort or filter the column and read the flagged rows directly. Reading ten flagged rows will tell you more than a summary total.
Choose the right tool
Formulas are not always the answer. The right method depends on how often the job recurs, how many rows there are, and whether someone else needs to audit the result.
| Method | Best for | Repeatable | Audit trail | Skill needed | Typical set-up time |
|---|---|---|---|---|---|
| Helper column formulas | One-off clean-ups, up to a few thousand rows | Only if the formulas are kept | Visible in each cell | Low | 5 to 15 minutes |
| Find and Replace | A single known character, such as NBSP | No | None | Very low | Under a minute |
| Power Query | Files that arrive on a schedule | Yes, by refreshing | Steps listed in the query | Medium | 30 to 60 minutes to build, then seconds per refresh |
| Script (Apps Script or Python) | Very large files or fully automated pipelines | Yes | The code itself | High | Several hours |
Use these rules to pick a method:
- If you are cleaning one column once and need to check each result, use helper formulas.
- If the same file arrives every week, build a Power Query. Its Transform tab includes Trim, Clean and Capitalise Each Word, and each step is recorded.
- If only one character is wrong, use Find and Replace. In Excel, type Alt+0160 on the numeric keypad into the Find box to enter an NBSP.
- If the file is so large that formulas slow the workbook, move the job to Power Query or a script, then paste the result as values.
Build the cleaning formula in the right order
The full formula in the introduction performs five operations. Each one has a reason for its position.
SOP 3.1: Replace line breaks with a space. SUBSTITUTE(A2,UNICHAR(10)," ") turns each line break into a space. If CLEAN ran first, "Flat 3[LF]High Street" would become "Flat 3High Street".
SOP 3.2: Replace non-breaking spaces with a space. SUBSTITUTE(…,UNICHAR(160)," ") converts each NBSP into a normal space. TRIM can then see it and collapse it.
SOP 3.3: Remove remaining control characters. CLEAN(…) removes tabs and any other codes from 0 to 31 that remain.
SOP 3.4: Collapse spacing. TRIM(…) removes leading and trailing spaces and reduces internal runs to one. It must come after SOP 3.2, or the NBSP-derived spaces would survive.
SOP 3.5: Apply capitalisation last. PROPER(…) runs last. Apply it only to columns where it suits the content.
The complete formula, in its general form, is:
=PROPER(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,UNICHAR(10)," "),UNICHAR(160)," "))))
Copy it into B2 and fill down. Use the same structure in Google Sheets. Google Sheets supports UNICHAR, CLEAN, TRIM and PROPER with the same behaviour.
A note on numbers. TRIM always returns text. A value of 1250 stored as a number becomes the text "1250", which SUM and some charts ignore. Leading zeros, such as the product code 00417, survive as text, which is usually what you want. For numeric columns, wrap the result in VALUE and check that totals still match the raw data.
Common mistakes
Overwriting the source column
The most damaging error is cleaning the raw data in place. A bad formula then destroys the only copy of the original. Keep the raw export on its own sheet, marked read-only if possible, and write every cleaned value to a separate column or sheet.
Assuming CLEAN removes non-breaking spaces
CLEAN is the function people reach for when text “looks wrong”, and it does not handle NBSP. A cell can pass through CLEAN and TRIM and still contain a non-breaking space. Always run SOP 1.3 on imported data, not just the other checks.
Applying PROPER to everything
PROPER is suited to names and titles. It damages acronyms, product codes, postcodes and some company names. Section 6 covers this in full. Apply it column by column, not to the whole sheet.
Letting numbers and dates turn into text
A date column run through TRIM will stop being a date. The cell still shows 03/04/2025, but it no longer sorts or calculates as a date. Clean text columns, and leave date and numeric columns for their own conversion steps.
Fixing the spreadsheet and not the source
If the same export is produced every week, the problem returns every week. Record the diagnostic results, tell the system owner which character is being introduced (NBSP from a web form is common), and check whether the source can be configured to stop it. Cleaning at the source is cheaper than cleaning at each import.
Breaking lookups with invisible spaces
A VLOOKUP for "Acme Ltd" will return #N/A if the lookup table holds "Acme Ltd ", with a trailing space. The error looks as though the key is missing, so people search for the wrong problem. Before debugging a lookup, run SOP 1.1 and SOP 1.3 on both lists.
Worked example: a 500-row customer import
A web form export contains 500 rows and four columns: Name, Company, Address and Email. The file must be ready for a mail merge and a CRM upload by Friday. The figures below are for this example only, not a general benchmark.
Six sample rows show the kinds of problem found in the raw data:
| Row | Column | Raw value | Cleaned value | Method |
|---|---|---|---|---|
| 2 | Name | " jOHN smith " | "John Smith" | Full formula |
| 3 | Company | "ACME[NBSP]LTD" | "Acme Ltd" | Full formula |
| 4 | Address | "Flat 3[LF]High Street" | "Flat 3 High Street" | Full formula |
| 5 | Name | "mary o'neill" | "Mary O'Neill" | Full formula |
| 6 | Address (postcode) | "sw1a 1aa" | "SW1A 1AA" | UPPER(TRIM(…)) |
| 7 | " Sales@Acme.co.uk " | "sales@acme.co.uk" | LOWER(TRIM(…)) |
Row 6 shows the exception rule. PROPER would have returned "Sw1A 1Aa", so the postcode column uses UPPER.
Step 1. Paste the raw export into a sheet named Raw. Do not edit it.
Step 2. On a sheet named Checks, run the diagnostics from Module 1 across each text column. In the example, the checks flagged 212 rows with extra spaces, 38 with NBSPs and 9 with line breaks.
Step 3. On a sheet named Clean, build helper columns. Use the full formula for Name, Company and Address. Use LOWER(TRIM(…)) for Email and UPPER(TRIM(…)) for the postcode.
Step 4. Fill down, then check 20 random rows against the Raw sheet. Check the rows the diagnostics flagged first.
Step 5. Filter the Name column for likely exceptions: names containing Mc, Mac, O', van, de or la. Review the PROPER output for each. In the example, 14 surnames needed correction, and these went into a corrections table.
Step 6. Copy the cleaned columns and paste them as values into a sheet named Final. Apply the corrections table to the Final sheet.
Step 7. Run a verification check. The formula =SUMPRODUCT(--(LEN(Final!A2:A501)<>LEN(TRIM(Final!A2:A501)))) should return 0 for every text column. Any value above 0 means a space problem survives.
Step 8. Run the email column against the CRM list. Matches should rise, because the trailing-space and case mismatches are gone.
The Final sheet is the upload file. The Raw sheet stays unchanged for audit.
Questions readers ask next
Does TRIM remove line breaks?
No. TRIM removes only the standard space character. Line breaks need CLEAN, and a line break between two words needs a space substitution before CLEAN runs.
Can I undo PROPER after pasting values?
Not from the cleaned sheet alone. Keep the raw data until the final output has been loaded and checked. If you lose the original, the case information cannot be recovered.
Do these functions work in Google Sheets?
Yes. TRIM, CLEAN, PROPER, UPPER, LOWER, SUBSTITUTE and UNICHAR all work in Google Sheets with the same syntax. The formula in this article can be pasted directly.
Pre-release checklist
Use this list before a cleaned column goes into a mail merge, a CRM or a database:
- The raw export is stored on its own sheet and has not been edited.
- Diagnostic checks (SOP 1.1 to SOP 1.4) have been run, and flagged rows reviewed.
- NBSP has been replaced before TRIM runs, and line breaks have been replaced before CLEAN runs.
- PROPER has been applied only to personal names and free-text titles.
- Postcodes, codes and abbreviations use UPPER or are excluded from the clean.
- Known exceptions are listed in a corrections table and applied.
- Numeric and date columns have not been converted to text by accident.
- The verification check returns 0 for every cleaned text column.
- Cleaned values have been pasted as values into the final sheet, with the raw sheet kept for audit.
- Lookups have been re-run against the destination list and match counts have been recorded.
Get the finished file
Tagtally Reseller Profit Tracker
Log every item, platform fee and shipping cost, and see your real profit per sale without doing the maths.