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.
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.
| A | B | |
|---|---|---|
| 1 | Pasted name | Tidy name |
| 2 | Nadia Patel | |
| 3 | Ella Bennett | |
| 4 | Marek Ahmed | |
| 5 | Ollie Khan | |
| 6 | Alex Wood | |
| 7 | Sam Jones | |
| 8 | Will Mensah | |
| 9 | Tanya 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.
TRIMtakes spaces off both ends and turns runs of spaces into one.UPPER,LOWERandPROPERfix capitals. PROPER gives Each Word A Capital.- Functions can go inside each other:
UPPER(TRIM(A2))trims first, then capitalises.
Worked example
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | As typed | Tidied | Postcode | Phone | Postcode tidied | Phone digits |
| 2 | Amira Khan | ls1 4ap | 0113-496 0123 | |||
| 3 | ben ODE | m60 2lb | 0161 496-0456 | |||
| 4 | Cara Lin | B1 1BB | 0121-496-0789 |
=PROPER(TRIM(A2))in B2. TRIM removes the extra spaces, and PROPER fixes the capitals: Amira Khan, Ben Ode and Cara Lin.=UPPER(TRIM(C2))in E2. Postcodes should be in capitals with no stray spaces: LS1 4AP.=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
- 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.
- Customers typed their own postcodes. In C2, put each postcode in capitals with no extra spaces.
- 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.
- 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