Why messy text quietly breaks your reports
You export customer data from a CRM, build a quick SUMIFS for Chicago, and the number looks fine. It is not. Some rows say "chicago", some " Chicago " with spaces you cannot see, and some "CHICAGO". To you they are the same city. To Excel, a value with a stray space is a different value, so it simply drops out of your total, without any error message. This free lesson is for anyone who works with exported data: sales and marketing analysts, office managers, accountants, CRM administrators and students who have ever wondered why a lookup "can't find" something that is clearly there.
Data cleaning is not glamorous, but it is where most analysis time goes and where most wrong numbers are born. The good news is that the most common problems, extra spaces and inconsistent capitalisation, can be fixed with three short functions: TRIM, PROPER and UPPER. In this lesson you clean the same CRM export as in the lab above and then prove, with a SUMIFS before and after, how much the cleaning changes the answer. In the example it changes the Chicago sales by more than $25,000.
The core idea: Excel compares characters, not meaning
Every text cell is just a sequence of characters, and a space is a character like any other. "Chicago" has 7 characters, " chicago " has 9. When SUMIFS, COUNTIFS, XLOOKUP, MATCH or a PivotTable compare two values, they compare those characters. Two details matter:
- Case is ignored by most functions. SUMIFS, COUNTIFS, XLOOKUP, MATCH and PivotTables treat
"CHICAGO"and"chicago"as equal. So capitalisation mostly hurts readability and consistency, not totals. (Only functions likeEXACTandFINDare case-sensitive.) - Spaces are never ignored.
" chicago "does not match"Chicago", and neither does"chicago "with two trailing spaces. That is why TRIM matters most. - TRIM(text) removes all spaces at the start and end of the text and turns runs of spaces inside the text into a single space.
=TRIM(" Summit Outdoor Co")returns"Summit Outdoor Co". - PROPER(text) writes Each Word With A Capital and the rest in lower case. UPPER and LOWER convert the whole text. Use PROPER for names and cities, UPPER for codes like state abbreviations (IL, MA, CO, TX).
- Nesting combines them:
=PROPER(TRIM(A2))first trims, then capitalises. The order does not change the result here, but trimming first is the habit that matters.
Worked example: cleaning a CRM export
The lab sheet holds twelve customer rows from a fictional CRM export, with sales in column D. In the table below, every space in the raw values is shown as a dot (·) so you can see what Excel sees. The three right-hand columns show the result of the cleaning formulas you enter in E, F and G.
| Row | A: Customer (raw) | B: City (raw) | C: State | D: Sales | E: =PROPER(TRIM(A2)) | F: City | G: =UPPER(TRIM(C2)) |
|---|---|---|---|---|---|---|---|
| 2 | "··northwind·traders" | "chicago" | "il" | $18,450.00 | Northwind Traders | Chicago | IL |
| 3 | "BLUE·HARBOR·FOODS" | "·Boston" | "MA" | $9,320.50 | Blue Harbor Foods | Boston | MA |
| 4 | "Summit··Outdoor·Co" | "Denver·" | "co" | $12,780.00 | Summit Outdoor Co | Denver | CO |
| 5 | "maple·&·oak··interiors·" | "CHICAGO" | "·IL" | $7,615.25 | Maple & Oak Interiors | Chicago | IL |
| 6 | "Riverbend·Dental" | "Austin" | "tx" | $5,240.00 | Riverbend Dental | Austin | TX |
| 7 | "·GREENLEAF··GROCERS" | "·chicago·" | "IL" | $21,305.80 | Greenleaf Grocers | Chicago | IL |
| 8 | "Pioneer·Logistics" | "denver" | "CO·" | $14,960.00 | Pioneer Logistics | Denver | CO |
| 9 | "harbor·view·hotel" | "Boston" | "ma" | $8,875.40 | Harbor View Hotel | Boston | MA |
| 10 | "Copper·Kettle·Cafe··" | "Austin·" | "TX" | $3,410.75 | Copper Kettle Cafe | Austin | TX |
| 11 | "SILVERLINE··MEDIA" | "Chicago" | "il" | $11,290.00 | Silverline Media | Chicago | IL |
| 12 | "Lakeside·Pet·Clinic" | "·AUSTIN" | "Tx" | $6,025.60 | Lakeside Pet Clinic | Austin | TX |
| 13 | "··Orchard·Street·Bakery" | "chicago··" | "IL" | $4,180.30 | Orchard Street Bakery | Chicago | IL |
Look at the raw city column. The twelve rows contain only four real cities, but Excel sees ten different values: "chicago", "CHICAGO" and "Chicago" count as one (case is ignored), while " chicago " and "chicago··" are separate, and so are " Boston", "Denver ", "Austin " and " AUSTIN". In Excel 365 you can check this with =ROWS(UNIQUE(B2:B13)), which returns 10. On the clean column, =ROWS(UNIQUE(F2:F13)) returns 4.
Now the test that matters. A SUMIFS on the raw column, =SUMIFS(D2:D13,B2:B13,"Chicago"), returns $37,355.25. It finds rows 2, 5 and 11, but misses row 7 (" chicago ", $21,305.80) and row 13 ("chicago··", $4,180.30). The same formula on the clean column, =SUMIFS(D2:D13,F2:F13,"Chicago"), returns $62,841.35. The difference is $25,486.10, about 41% of Chicago's real sales, and nothing in the raw formula warned you about it.
Lab 8 task counts how many customer names contain extra spaces. The idea is simple: if trimming a name makes it shorter, it had extra spaces. =SUMPRODUCT(--(LEN(A2:A13)<>LEN(TRIM(A2:A13)))) compares the length of every raw name with the length of its trimmed version. The comparison returns TRUE or FALSE for each row, the double minus -- turns those into 1 and 0, and SUMPRODUCT adds them up. The result is 7 of 12 names. That is a useful data-quality figure to report back to whoever owns the CRM.
=LEN(A2)-LEN(TRIM(A2)). It shows how many spaces TRIM removed from each name. Filter it for values above 0 and you have a to-do list for fixing the source system.Proving that the cleaning worked
Cleaning is only finished when you can show that it worked. Three quick checks do that, and they take less than a minute once you know them.
- The totals must reconcile. Add up the sales of all clean cities, for example with four SUMIFS, and compare the result with
=SUM(D2:D13). On the clean column F the four city totals add up to exactly $123,453.60, the total of column D. On the raw column B the same four SUMIFS add up to only $66,430.65, so almost half of the sales are missing from the city view. - No extra spaces may remain. Run the same counting formula on the clean column:
=SUMPRODUCT(--(LEN(E2:E13)<>LEN(TRIM(E2:E13))))must return 0. If it does not, some cells contain characters that TRIM cannot see, usually non-breaking spaces. - The list of categories must be short and recognisable.
=SORT(UNIQUE(F2:F13))spills the distinct clean cities into the cells below (Excel 365 and 2021). You should see Austin, Boston, Chicago and Denver, and nothing else. A fifth entry means a spelling variant that needs a mapping, which is the topic of a later lesson.
Why not simply use Find & Replace and be done? Ctrl+H is useful for one-off fixes, such as replacing two spaces with one or removing a unit like " USD", but it changes the raw data in place and leaves no record of what was changed. A formula column documents the rule, updates when the data changes, and can be checked by a colleague. For a file you clean once and never see again, Find & Replace is fine. For anything that comes back every week or month, use formulas or Power Query.
How to do it in Excel, step by step
- Insert clean columns next to the raw ones and give them clear headers, for example "Customer", "City" and "State". Keep the raw headers unchanged so the source stays recognisable.
- Click E2 and type
=PROPER(TRIM(A2)), then press Enter. Click E2 again and double-click the small fill handle in the bottom-right corner of the cell to copy the formula down to the last row (or select E2:E13 and press Ctrl+D). - In F2 type
=PROPER(TRIM(B2))for the city and copy it down the same way. - In G2 type
=UPPER(TRIM(C2))for the state code and copy it down. State codes should be all capitals, so UPPER is the right choice here, not PROPER. - Check the result. Compare a few rows by eye, and use
=SUMPRODUCT(--(LEN(A2:A13)<>LEN(TRIM(A2:A13))))to count how many raw values were affected. - Point every analysis formula at the clean columns:
=SUMIFS(D2:D13,F2:F13,"Chicago")instead of column B. - If you need fixed values (for example to send the file to someone else), select E2:G13, copy with Ctrl+C, then Home > Paste > Paste Values (or Ctrl+Alt+V on Windows, then V and Enter). Save the raw export separately before you delete any raw column.
- For a monthly export, consider Data > From Table/Range to open Power Query. Right-click a column header > Transform > Trim, Clean or Capitalize Each Word, then Home > Close & Load. Next month, paste the new data and click Data > Refresh All.

Common mistakes and how to spot them
- Summing on the raw column. The total looks plausible, so nobody notices. Spot it by comparing the sum of your category totals with
=SUM(D2:D13). If the parts do not add up to the whole, some rows did not match any category. - Non-breaking spaces from web pages. Text copied from a website or some PDF exports contains character 160, which TRIM does not remove. If
=LEN(TRIM(A2))is still longer than expected, use=TRIM(SUBSTITUTE(A2,CHAR(160)," ")).=CODE(LEFT(A2,1))shows the code of the first character, 160 means a non-breaking space. - Line breaks and hidden characters. Values from multi-line form fields can contain line breaks.
=CLEAN(A2)removes them, and=TRIM(CLEAN(A2))handles both problems in one go. - PROPER breaking names and brands. PROPER capitalises after every non-letter and lowercases everything else. "IBM" becomes "Ibm", "McDonald" becomes "Mcdonald" and "ACME LLC" becomes "Acme Llc". Use PROPER for messy names, then fix known exceptions with a small mapping table or a SUBSTITUTE.
- Overwriting the raw data. If you clean in place and something goes wrong, you cannot go back. Always build new columns and keep the original export.
- Forgetting the lookup key on the other side. Cleaning your data but not the table you look values up in still gives mismatches. Clean both sides of every join.
- Using Flash Fill for recurring data. Ctrl+E is fast, but its results are fixed values. When next month's export arrives, nothing updates. Formulas or Power Query steps do.
Quick reference: functions and shortcuts used
| Function, tool or shortcut | What it does |
|---|---|
TRIM(text) | Removes leading and trailing spaces and reduces runs of spaces inside to one |
PROPER(text) | Capitalises the first letter of each word, lowercases the rest |
UPPER(text) / LOWER(text) | All capitals / all small letters |
LEN(text) | Number of characters, spaces included |
SUBSTITUTE(text,CHAR(160)," ") | Turns non-breaking spaces into normal spaces so TRIM can remove them |
CLEAN(text) | Removes line breaks and other non-printing characters (codes 0 to 31) |
EXACT(a,b) | Case-sensitive comparison, TRUE only if both texts are identical |
SUMPRODUCT(--(LEN(r)<>LEN(TRIM(r)))) | Counts cells in range r that contain extra spaces |
ROWS(UNIQUE(range)) | Number of distinct values (Excel 365 / 2021) |
| Ctrl+H | Find & Replace |
| Ctrl+E | Flash Fill |
| Ctrl+Alt+V (Windows) / Ctrl+Cmd+V (Mac) | Paste Special, then choose Values |
Real-world uses
- Sales reporting by region or city. A SUMIFS or PivotTable by city is only correct when the city names are clean. As the example shows, the error can be large and invisible.
- Merging two systems. When you match customers from a CRM with invoices from accounting using XLOOKUP, trimmed keys are the difference between 95% matches and 60% matches.
- Mailing lists and letters. "dear JOHN smith" in a mail merge looks careless.
=PROPER(TRIM(A2))fixes the salutation for every row at once. - Data-quality reporting. The count of rows with extra spaces, per source system, is a simple and convincing metric when you ask IT to fix an input form.
- Preparing data for a PivotTable or dashboard. Clean categories mean one row per city in the PivotTable, not three versions of Chicago.
What you will build in the rest of this course
Lesson 1 is free. The remaining 19 lessons are part of the paid course ($9.90, one-time). Each one tackles a problem you meet in real exports, always with a hands-on lab and a Coach that checks your formulas.
Splitting Codes & Names: LEFT, MID, FIND. You split product codes into category and item number, and turn "Last, First" contacts into clean first-name and last-name columns that still work when the text lengths vary.
Converting Text Numbers & Text Dates into Real Values. You turn an invoice export with text amounts and European text dates into real numbers and dates that Excel can add and sort, then total March correctly.
Standardising Categories with a Mapping Table (XLOOKUP). You map 14 free-text expense labels to four standard categories, catch the labels the mapping does not know yet, and total by category.
Finding Duplicates & First Occurrences with COUNTIFS. You find order lines that were exported twice, keep the first copy of each and calculate the true revenue without deleting anything by hand.
Handling Blanks & Outliers: IF, MEDIAN and Flags. You fill two missing days with the median, set an outlier fence with the IQR rule and flag a typo that would otherwise wreck every average.
Validating Rows: Rule Checks with AND, OR and an Error Count. You check 12 orders against four business rules, count the rule breaks per row and report an error rate that a manager can act on.
Mid-course project: an Analysis-Ready Table and a Region × Month Summary. You bring every technique together: you convert a raw sales export with text dates, messy regions and text amounts into a clean table and build a Region × Month summary with SUMIFS.
Summary
Excel treats a value with an extra space as a different value, so stray spaces silently remove rows from SUMIFS, lookups and PivotTables. TRIM removes them, PROPER and UPPER make capitalisation consistent, and =PROPER(TRIM(A2)) does both in one clean column next to the raw data. In the example, cleaning raised the Chicago total from $37,355.25 to $62,841.35, and a single SUMPRODUCT showed that 7 of 12 customer names had extra spaces.
Scroll up to the lab and clean columns E, F and G yourself, then calculate the Chicago total and the extra-space count. Try the SUMIFS on column B first and then on column F: seeing the two totals side by side is the fastest way to never trust a raw export again.
Frequently asked questions
What does the TRIM function do in Excel?
TRIM removes all spaces at the start and end of a text and reduces runs of spaces inside the text to a single space. It does not remove non-breaking spaces (character 160).
Why does TRIM not remove all spaces?
Text copied from web pages often contains non-breaking spaces (CHAR(160)), which TRIM ignores. Use =TRIM(SUBSTITUTE(A2,CHAR(160)," ")) to remove them as well.
How do I capitalise the first letter of each word in Excel?
Use =PROPER(A2), or =PROPER(TRIM(A2)) to remove extra spaces at the same time. Check names like McDonald or abbreviations like IBM afterwards, because PROPER changes them too.
Is SUMIFS case-sensitive?
No. SUMIFS, COUNTIFS, XLOOKUP and MATCH ignore upper and lower case, but they do not ignore spaces, so " Chicago" and "Chicago" are different values.
How do I count cells with extra spaces in Excel?
Use =SUMPRODUCT(--(LEN(A2:A13)<>LEN(TRIM(A2:A13)))). It counts the cells whose length changes when they are trimmed.
How do I replace formulas with their cleaned values?
Select the clean columns, copy them with Ctrl+C and use Home > Paste > Paste Values. Keep the original export saved separately before you delete the raw columns.
Keep going: Data Cleaning & Preparation in Excel
This free lab is lesson 1 of 20. Unlock the other 19 hands-on labs, the final exam and your certificate for a one-time €9.90.
- Splitting Codes & Names: LEFT, MID, FIND
- Converting Text Numbers & Text Dates into Real Values
- Standardising Categories with a Mapping Table (XLOOKUP)
- Finding Duplicates & First Occurrences with COUNTIFS
- Handling Blanks & Outliers: IF, MEDIAN and Flags
- Validating Rows: Rule Checks with AND, OR and an Error Count
- Mid-course project: an Analysis-Ready Table and a Region × Month Summary
- Decimal Commas, Currency Signs & Units: Converting a European Export
- Restoring Leading Zeros & Standardising IDs and Phone Numbers
- Report-Style Exports: Filling Down Group Labels & Removing Subtotal Rows
- Near-Duplicates: Building a Match Key for Company Names
- Dates Stored as Numbers, Text Timestamps & US Text Dates
- Mixed Units in One Column: Splitting Value & Unit and Converting to Kilograms
- Several Values in One Cell: Counting, Splitting & Searching Tag Lists
- Cross-Field Checks: Tax, Gross and Order Totals That Must Add Up
- A Data Quality Scorecard: Completeness, Validity & Uniqueness
- Outliers Within Groups: Comparing Each Price with Its Category Median
- Reshaping a Wide Report into a Long Table with INDEX, INT and MOD
- Final Project: From Raw Web-Shop Export to a Clean Channel Dashboard