Excel course · Lesson 19 of 34
Text cleanup: TRIM, CLEAN, TEXTSPLIT, Flash Fill and Text to Columns
Clean and reshape messy text: remove spaces and hidden characters, fix capitals, split and join columns with functions, Flash Fill and Text to Columns.
Data rarely arrives clean. Exports from accounting systems, web forms and CRMs bring extra spaces, odd capitalisation, invisible characters, codes squeezed into one column and numbers that are really text. Dirty text breaks lookups, splits one customer into three in a PivotTable and makes totals come out wrong. This lesson gives you a toolkit: formulas that clean and split text, and two point-and-click tools, Text to Columns and Flash Fill.
Removing spaces and hidden characters
TRIM removes spaces at the start and end of the text and reduces runs of spaces between words to a single space. CLEAN removes non-printing characters such as line breaks and control codes that come with imported data. PROPER, UPPER and LOWER fix the capitals.
| A | B | |
|---|---|---|
| 1 | Raw name | Clean name |
| 2 | maria LOPEZ | Maria Lopez |
| 3 | JOHN carter | John Carter |
| 4 | aiko tanaka | Aiko Tanaka |
TRIM removes the extra spaces, PROPER capitalises each word.
Result: Maria Lopez
=TRIM(SUBSTITUTE(A2, CHAR(160), " ")). To check what a stray character is, =CODE(RIGHT(A2)) shows the code of the last character.McDonald into Mcdonald and O'NEILL into O'Neill (correct) but van der Berg into Van Der Berg. Review names after converting.Checking text with LEN and EXACT
- LEN returns the number of characters.
=LEN(A2)on"P-103 "gives 6, revealing the trailing space that breaks a lookup. - EXACT compares two texts case-sensitively:
=EXACT("abc","ABC")is FALSE, while="abc"="ABC"is TRUE.
Extracting parts of text
The classic functions: LEFT, RIGHT, MID, FIND
| A | B | C | D | |
|---|---|---|---|---|
| 1 | SKU | Region | Number | Colour |
| 2 | EU-4471-BLK | EU | 4471 | BLK |
| 3 | US-0932-WHT | US | 0932 | WHT |
| 4 | AP-1180-RED | AP | 1180 | RED |
B2 =LEFT(A2,2), C2 =MID(A2,4,4), D2 =RIGHT(A2,3).
Result: 4471
=LEFT(A2, 2)takes the first 2 characters:EU.=MID(A2, 4, 4)takes 4 characters starting at position 4:4471. The result is text; wrap it in VALUE (or add--in front) to get the number 4471, though that drops the leading zero of0932.=RIGHT(A2, 3)takes the last 3 characters:BLK.
Fixed positions only work when every code has the same layout. For variable lengths, find the separator with FIND (case-sensitive) or SEARCH (not case-sensitive, allows wildcards). To get the domain of an email address: =MID(A2, FIND("@", A2)+1, LEN(A2)) returns company.com from [email protected].
The modern functions: TEXTBEFORE, TEXTAFTER, TEXTSPLIT
Microsoft 365 and Excel for the web added three functions that make splitting much easier:
| Formula | Result for EU-4471-BLK |
|---|---|
| =TEXTBEFORE(A2, "-") | EU |
| =TEXTAFTER(A2, "-") | 4471-BLK |
| =TEXTAFTER(A2, "-", -1) | BLK (the -1 means: after the last dash) |
| =TEXTBEFORE(TEXTAFTER(A2, "-"), "-") | 4471 |
| =TEXTSPLIT(A2, "-") | EU | 4471 | BLK spilled into three cells |
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Order items | |||
| 2 | Laptop;Mouse;Headset | Laptop | Mouse | Headset |
TEXTSPLIT spills the items across B2:D2.
TEXTSPLIT can split into columns and rows at once: =TEXTSPLIT("Anna,52;Ben,47", ",", ";") returns a 2×2 grid. Its results spill, so leave empty cells to the right and below (Dynamic arrays: spill, FILTER, SORT, UNIQUE, SEQUENCE and #).
Replacing and joining text
- SUBSTITUTE replaces specific text:
=SUBSTITUTE(A2, "-", "")turnsEU-4471-BLKintoEU4471BLK. - REPLACE replaces by position:
=REPLACE(A2, 1, 2, "DE")givesDE-4471-BLK. - The
&operator joins text:=B2&"-"&C2. - TEXTJOIN joins a whole range with a delimiter and can skip empty cells:
=TEXTJOIN(", ", TRUE, B2:D2)returnsLaptop, Mouse, Headset. Available from Excel 2019. - CONCAT joins a range without a delimiter; the older CONCATENATE only accepts single cells.
Text to Columns
Data › Data Tools › Text to Columns splits one column into several, in place, without formulas. It is ideal for a one-off cleanup of an import.
- Make sure there are enough empty columns to the right of the data, or the split overwrites them.
- Select the column, for example
A2:A4with the SKUs. - Choose Data › Text to Columns. Pick Delimited and click Next.
- Tick Other and type
-. The preview shows three columns. Click Next. - Select the middle column in the preview and set Column data format: Text, so
0932keeps its leading zero. - Set Destination to
$B$2to keep the original column, and click Finish.
Flash Fill for one-off patterns
Flash Fill (Ctrl+E, Data › Data Tools › Flash Fill) learns from an example you type. For the SKU list, type 4471 next to the first SKU and press Ctrl+E; Excel extracts the numbers from all rows. It is fast for irregular patterns like 'initial plus surname' (M. Lopez), but the results are fixed values and do not update. See Entering and editing data: AutoFill, Flash Fill and Undo.
Numbers stored as text
Imported numbers sometimes arrive as text: left-aligned, with a green triangle, ignored by SUM. Fixes, from quickest to most robust:
- Select the cells, click the warning icon and choose Convert to Number.
- Text to Columns › Finish, as above.
- A formula:
=VALUE(A2), or=--A2. For numbers with a different decimal separator, NUMBERVALUE lets you specify it:=NUMBERVALUE("1.234,50", ",", ".")returns 1234.5. - For a large or repeated import, do the conversion in Power Query (Power Query basics: import, clean, append, merge, refresh).
Which tool when
| Situation | Best tool |
|---|---|
| One-off import that you will clean once | Text to Columns, Flash Fill |
| Data that keeps changing; results must update | Formulas (TRIM, TEXTSPLIT, TEXTBEFORE …) |
| The same messy file arrives every week | Power Query: record the steps once, refresh every week |
| Removing duplicates after cleaning | Removing duplicates and cleaning data |
When you clean with formulas in helper columns, finish by turning the results into values if you want to delete the raw data: copy the clean column and use Paste Special › Values on top of it. Otherwise deleting the raw column breaks the formulas with #REF!.
Check yourself
1. Which function removes extra spaces between words and at both ends?
TRIM leaves single spaces between words and none at the ends.
2. TRIM does not seem to remove a space copied from a web page. What is it probably?
Replace CHAR(160) with a normal space using SUBSTITUTE, then TRIM.
3. What does =TEXTAFTER("EU-4471-BLK", "-", -1) return?
A negative instance number counts from the end: after the last dash.
4. What does =MID("EU-4471-BLK", 4, 4) return?
Starting at character 4, take 4 characters.
5. Which approach is best for a messy file that arrives every Monday in the same layout?
Power Query repeats recorded cleaning steps on each new file.