Confident CellsExcel practice Start free

Cleaning text: practice exercises

TRIM, UPPER, SUBSTITUTE, LEN. 4 exercises on real-looking data, each marked the moment you press Check. The first exercise is below to try now; the full lesson is part of Pro.

Your turn

These names were pasted in from an email, and some have extra spaces at the start, at the end or in the middle. In B2, tidy each name so it has single spaces only.

AB
1Pasted nameTidy name
2 Nadia Patel
3 Ella Bennett
4Marek Ahmed
5 Ollie Khan
6 Alex Wood
7Sam Jones
8 Will Mensah
9Tanya Hall
10 Tom Hughes
11 Lucy Baker

How it works

Data pasted from emails, forms and other systems is often untidy: extra spaces, odd capitals, dashes in phone numbers. Fix it with a formula in a new column, so the original stays as it was.

TRIM removes spaces at the start and end and turns double spaces into single ones. SUBSTITUTE(text, old, new) swaps every old for new; put one SUBSTITUTE inside another to make two swaps. LEN counts characters, spaces included.

=TRIM(A3) =PROPER(TRIM(A3)) =SUBSTITUTE(D3,"-","") =LEN(TRIM(A3))

Tidy in a new column

Never type over messy data. Put a formula in the next column that tidies it, check it, and if you need to, paste the results back as values.

  • TRIM takes spaces off both ends and turns runs of spaces into one.
  • UPPER, LOWER and PROPER fix capitals. PROPER gives Each Word A Capital.
  • Functions can go inside each other: UPPER(TRIM(A2)) trims first, then capitalises.

Worked example

ABCDEF
1As typedTidiedPostcodePhonePostcode tidiedPhone digits
2 Amira Khan ls1 4ap0113-496 0123
3ben ODE m60 2lb0161 496-0456
4Cara Lin B1 1BB0121-496-0789
  1. =PROPER(TRIM(A2)) in B2. TRIM removes the extra spaces, and PROPER fixes the capitals: Amira Khan, Ben Ode and Cara Lin.
  2. =UPPER(TRIM(C2)) in E2. Postcodes should be in capitals with no stray spaces: LS1 4AP.
  3. =SUBSTITUTE(SUBSTITUTE(D2," ",""),"-","") in F2. The inner SUBSTITUTE removes the spaces and the outer one the dashes, leaving just the digits.

Checking lengths

LEN counts characters, spaces included. Once a value is tidied, its length tells you whether it's the right shape: a payroll number should be 8 characters, a UK mobile 11 digits.

=IF(LEN(TRIM(A2))<>8,"Check","") flags anything that isn't 8 characters long once the spaces are gone.

What you'll practise

  1. These names were pasted in from an email, and some have extra spaces at the start, at the end or in the middle. In B2, tidy each name so it has single spaces only.
  2. Customers typed their own postcodes. In C2, put each postcode in capitals with no extra spaces.
  3. The phone numbers were typed in different ways. In C2, take out every space and every dash, so each number is just its 11 digits.
  4. Every product code should be 8 characters long, like PX-40213. In C2, show Check next to any code that isn't 8 characters once the extra spaces are taken off, and leave the cell blank otherwise.

Guides with worked examples: Change case in Excel: UPPER, LOWER and PROPER · Split first and last names in Excel · Finding and removing duplicates in Excel · TRIM: removing extra spaces and tidying text · VLOOKUP not working: 6 common problems and how to fix them · Combine first and last names in Excel: &, CONCAT and TEXTJOIN · LEFT, MID and RIGHT: taking part of a code or a postcode