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.

Intermediate 18 min read

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.

B2fx=PROPER(TRIM(A2))
AB
1Raw nameClean name
2 maria LOPEZ Maria Lopez
3JOHN carterJohn Carter
4aiko tanaka Aiko Tanaka

TRIM removes the extra spaces, PROPER capitalises each word.

Result: Maria Lopez

Watch out. Text copied from web pages often contains the non-breaking space (character 160), which TRIM does not remove. If TRIM seems to do nothing, use =TRIM(SUBSTITUTE(A2, CHAR(160), " ")). To check what a stray character is, =CODE(RIGHT(A2)) shows the code of the last character.
Tip. PROPER is not perfect: it turns 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

C2fx=MID(A2, 4, 4)
ABCD
1SKURegionNumberColour
2EU-4471-BLKEU4471BLK
3US-0932-WHTUS0932WHT
4AP-1180-REDAP1180RED

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 of 0932.
  • =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 john.carter@company.com.

The modern functions: TEXTBEFORE, TEXTAFTER, TEXTSPLIT

Microsoft 365 and Excel for the web added three functions that make splitting much easier:

FormulaResult 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
B2fx=TEXTSPLIT(A2, ";")
ABCD
1Order items
2Laptop;Mouse;HeadsetLaptopMouseHeadset

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 #).

Note. TEXTBEFORE, TEXTAFTER and TEXTSPLIT need Microsoft 365 or Excel for the web (and Excel 2024). In Excel 2021 and older use LEFT/MID/FIND or Text to Columns.

Replacing and joining text

  • SUBSTITUTE replaces specific text: =SUBSTITUTE(A2, "-", "") turns EU-4471-BLK into EU4471BLK.
  • REPLACE replaces by position: =REPLACE(A2, 1, 2, "DE") gives DE-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) returns Laptop, 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.

  1. Make sure there are enough empty columns to the right of the data, or the split overwrites them.
  2. Select the column, for example A2:A4 with the SKUs.
  3. Choose Data › Text to Columns. Pick Delimited and click Next.
  4. Tick Other and type -. The preview shows three columns. Click Next.
  5. Select the middle column in the preview and set Column data format: Text, so 0932 keeps its leading zero.
  6. Set Destination to $B$2 to keep the original column, and click Finish.
Tip. Text to Columns has a second life as a converter. Select a column of numbers stored as text or of dates that Excel did not recognise, open Text to Columns and click Finish immediately (or choose Column data format: Date with the right order, such as DMY). Excel re-reads every cell and converts it.

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

SituationBest tool
One-off import that you will clean onceText to Columns, Flash Fill
Data that keeps changing; results must updateFormulas (TRIM, TEXTSPLIT, TEXTBEFORE …)
The same messy file arrives every weekPower Query: record the steps once, refresh every week
Removing duplicates after cleaningRemoving 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?

2. TRIM does not seem to remove a space copied from a web page. What is it probably?

3. What does =TEXTAFTER("EU-4471-BLK", "-", -1) return?

4. What does =MID("EU-4471-BLK", 4, 4) return?

5. Which approach is best for a messy file that arrives every Monday in the same layout?

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB