Tabellab

Trimming Spaces & Fixing Case: TRIM, PROPER, UPPER

Lesson 1 of 20Lab

All lessons
  1. Trimming Spaces & Fixing Case: TRIM, PROPER, UPPERLabFree
  2. Splitting Codes & Names: LEFT, MID, FINDLab Locked
  3. Converting Text Numbers & Text Dates into Real ValuesLab Locked
  4. Standardising Categories with a Mapping Table (XLOOKUP)Lab Locked
  5. Finding Duplicates & First Occurrences with COUNTIFSLab Locked
  6. Handling Blanks & Outliers: IF, MEDIAN and FlagsLab Locked
  7. Validating Rows: Rule Checks with AND, OR and an Error CountLab Locked
  8. Mid-course project: an Analysis-Ready Table and a Region × Month SummaryLab Locked
  9. Decimal Commas, Currency Signs & Units: Converting a European ExportLab Locked
  10. Restoring Leading Zeros & Standardising IDs and Phone NumbersLab Locked
  11. Report-Style Exports: Filling Down Group Labels & Removing Subtotal RowsLab Locked
  12. Near-Duplicates: Building a Match Key for Company NamesLab Locked
  13. Dates Stored as Numbers, Text Timestamps & US Text DatesLab Locked
  14. Mixed Units in One Column: Splitting Value & Unit and Converting to KilogramsLab Locked
  15. Several Values in One Cell: Counting, Splitting & Searching Tag ListsLab Locked
  16. Cross-Field Checks: Tax, Gross and Order Totals That Must Add UpLab Locked
  17. A Data Quality Scorecard: Completeness, Validity & UniquenessLab Locked
  18. Outliers Within Groups: Comparing Each Price with Its Category MedianLab Locked
  19. Reshaping a Wide Report into a Long Table with INDEX, INT and MODLab Locked
  20. Final Project: From Raw Web-Shop Export to a Clean Channel DashboardLab Locked

You are not signed in — the lab works, but your progress is not saved. Sign up

Lesson 1 of 20 is free. Unlock all 20 hands-on labs, the final exam and your certificate.

Buy course — $9.90
Read first

Other quick fixes in Excel

  • Find & Replace (Ctrl+H): replace two spaces with one, or remove a unit like " USD". Repeat until Excel finds nothing.
  • Non-breaking spaces from web pages (character 160) survive TRIM. Remove them with =TRIM(SUBSTITUTE(A2,CHAR(160)," ")). CLEAN removes line breaks and other non-printing characters.
  • Flash Fill (Ctrl+E): type the clean version of the first one or two rows, press Ctrl+E and Excel fills the pattern. Fast, but it does not update when the data changes. Formulas do.
  • In Power Query (Data > From Table/Range): right-click a column > Transform > Trim, Clean or Capitalize Each Word. The steps are saved and run again on next month's export with one click on Refresh.
  • When the clean columns are right: copy them, Paste Special > Values, then delete the raw columns (keep the raw file).
Loading the spreadsheet…

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 like EXACT and FIND are 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.
The golden rule of cleaning: never overwrite the raw column. Build the clean version in a new column next to it, check it, and keep the raw data so you can always trace a number back to its source.

One name, two functions: =PROPER(TRIM(A2))A2 (raw): 19 characters, 2 leading spaces shown as dots··northwind·tradersTRIM: outer spaces go, double spaces become one=TRIM(A2): 17 charactersnorthwind·tradersPROPER: a capital at the start of each word=PROPER(TRIM(A2)): clean and ready for lookupsNorthwind·TradersLEN(A2) = 19, LEN(TRIM(A2)) = 17, so the row is counted as having extra spaces.The raw column stays untouched. The clean value lives in its own column (E).
What happens inside =PROPER(TRIM(A2)) for the first customer. Spaces are shown as dots so you can see them.

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.

RowA: Customer (raw)B: City (raw)C: StateD: SalesE: =PROPER(TRIM(A2))F: CityG: =UPPER(TRIM(C2))
2"··northwind·traders""chicago""il"$18,450.00Northwind TradersChicagoIL
3"BLUE·HARBOR·FOODS""·Boston""MA"$9,320.50Blue Harbor FoodsBostonMA
4"Summit··Outdoor·Co""Denver·""co"$12,780.00Summit Outdoor CoDenverCO
5"maple·&·oak··interiors·""CHICAGO""·IL"$7,615.25Maple & Oak InteriorsChicagoIL
6"Riverbend·Dental""Austin""tx"$5,240.00Riverbend DentalAustinTX
7"·GREENLEAF··GROCERS""·chicago·""IL"$21,305.80Greenleaf GrocersChicagoIL
8"Pioneer·Logistics""denver""CO·"$14,960.00Pioneer LogisticsDenverCO
9"harbor·view·hotel""Boston""ma"$8,875.40Harbor View HotelBostonMA
10"Copper·Kettle·Cafe··""Austin·""TX"$3,410.75Copper Kettle CafeAustinTX
11"SILVERLINE··MEDIA""Chicago""il"$11,290.00Silverline MediaChicagoIL
12"Lakeside·Pet·Clinic""·AUSTIN""Tx"$6,025.60Lakeside Pet ClinicAustinTX
13"··Orchard·Street·Bakery""chicago··""IL"$4,180.30Orchard Street BakeryChicagoIL

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.

Sales by city: SUMIFS on raw vs clean columnraw column Bclean column F (TRIM + PROPER)Chicago$37,355$62,841Denver$14,960$27,740Boston$8,875$18,196Austin$5,240$14,6760k10k20k30k40k50k60k70kThe raw column misses $57,022.95 of $123,453.60 in total (46%).
The same SUMIFS, once on the raw city column and once on the cleaned one. Every city is understated before cleaning.

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.

To see which rows are affected, add a helper column with =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.

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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).
  3. In F2 type =PROPER(TRIM(B2)) for the city and copy it down the same way.
  4. 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.
  5. 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.
  6. Point every analysis formula at the clean columns: =SUMIFS(D2:D13,F2:F13,"Chicago") instead of column B.
  7. 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.
  8. 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.
Power Query's Trim removes spaces at the start and end only. Unlike the Excel TRIM function, it does not reduce double spaces inside the text. If inner spaces matter, add a Replace Values step (two spaces to one) or use the worksheet function.

Data Cleaning and Preparation in Excel course: raw export columns next to cleaned columns
Data Cleaning & Preparation in Excel: turning raw exports into analysis-ready tables.

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 shortcutWhat 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+HFind & Replace
Ctrl+EFlash 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.

  1. Splitting Codes & Names: LEFT, MID, FIND
  2. Converting Text Numbers & Text Dates into Real Values
  3. Standardising Categories with a Mapping Table (XLOOKUP)
  4. Finding Duplicates & First Occurrences with COUNTIFS
  5. Handling Blanks & Outliers: IF, MEDIAN and Flags
  6. Validating Rows: Rule Checks with AND, OR and an Error Count
  7. Mid-course project: an Analysis-Ready Table and a Region × Month Summary
  8. Decimal Commas, Currency Signs & Units: Converting a European Export
  9. Restoring Leading Zeros & Standardising IDs and Phone Numbers
  10. Report-Style Exports: Filling Down Group Labels & Removing Subtotal Rows
  11. Near-Duplicates: Building a Match Key for Company Names
  12. Dates Stored as Numbers, Text Timestamps & US Text Dates
  13. Mixed Units in One Column: Splitting Value & Unit and Converting to Kilograms
  14. Several Values in One Cell: Counting, Splitting & Searching Tag Lists
  15. Cross-Field Checks: Tax, Gross and Order Totals That Must Add Up
  16. A Data Quality Scorecard: Completeness, Validity & Uniqueness
  17. Outliers Within Groups: Comparing Each Price with Its Category Median
  18. Reshaping a Wide Report into a Long Table with INDEX, INT and MOD
  19. Final Project: From Raw Web-Shop Export to a Clean Channel Dashboard

Buy course — $9.90 See the full course