Text Functions for Cleaning Data
Clean imported text with TRIM and PROPER, extract parts with LEFT, MID, FIND and TEXTBEFORE, and combine values with TEXTJOIN and TEXT.
Welcome back!
Step 1 of 11Learn
The export nobody wants to clean
A customer list exported from an old CRM arrives looking like this:
| Name | Invoice |
|---|---|
SMITH, john | INV-2026-00417 |
garcia, MARIA | INV-2025-01193 |
Names are in random case, padded with spaces, surname first. Invoice numbers hide a year you'd like in its own column. Lookups against this data return #N/A because "SMITH, john " with a trailing space is not "Smith, John".
You could fix 40 rows by hand. You can't fix 40,000, and you can't fix them again next month when a fresh export arrives. Text functions turn the cleanup into formulas: write them once in helper columns, and every future export cleans itself the moment you paste it in.
This lesson works through the toolkit in the order you'd use it: tidy the text, measure and locate pieces of it, extract the pieces, and put them back together the way you want. Along the way, keep one rule in mind: the result of a text function is always text, even when it looks like a number.
Step 2 of 11Learn
Tidy first: TRIM, case and LEN
TRIM(text) removes spaces from the start and end of text and collapses runs of spaces between words into one. =TRIM(" garcia, MARIA ") returns "garcia, MARIA". It's the single most useful cleaning function, and the first thing to try when a lookup fails on data that's obviously there.
Three functions fix capitalization:
UPPER("ab-12")returns "AB-12", useful for codes.LOWER("Ana@Example.com")returns "ana@example.com", useful for emails.PROPER("maria GARCIA")returns "Maria Garcia", capitalizing each word. Watch names like "McDonald" (PROPER gives "Mcdonald") and "van der Berg".
LEN(text) counts characters, including spaces. It's a quick diagnostic: if a code should be 6 characters and LEN says 7, there's a hidden space or character.
TRIM handles ordinary spaces only. Data copied from web pages often contains non-breaking spaces (character 160), which TRIM ignores. =TRIM(SUBSTITUTE(A2, CHAR(160), " ")) swaps them for normal spaces first. CLEAN removes non-printing characters such as stray line breaks.
Step 3 of 11Practice · Work it out
A2 contains the text " Ana Ruiz " (two spaces before, three between, one after). What does =LEN(TRIM(A2)) return?
Step 4 of 11Learn
Extract with LEFT, RIGHT, MID and FIND
Three functions pull characters out by position:
LEFT(text, n)takes the first n characters:=LEFT("INV-2026-00417",3)is "INV".RIGHT(text, n)takes the last n:=RIGHT("INV-2026-00417",5)is "00417".MID(text, start, n)takes n characters starting at position start:=MID("INV-2026-00417",5,4)is "2026".
Fixed positions work when every value has the same layout. When pieces vary in length, find the separator first. FIND(find_text, within_text) returns the position of the first occurrence: =FIND(",", "Smith, John") is 6. SEARCH does the same but ignores case and accepts wildcards; FIND is case-sensitive.
Combining them extracts the surname from "Smith, John" whatever its length:
=LEFT(A2, FIND(",", A2) - 1)
The comma is at position 6, so LEFT takes 5 characters: "Smith". The -1 excludes the comma itself, and forgetting it is the classic off-by-one bug. If the separator is missing, FIND returns #VALUE!, which is a useful signal that a row doesn't fit the pattern.
Remember that MID returns text: "2026" won't add up until you convert it with VALUE or --.
Step 5 of 11Practice · Work it out
A2 contains SKU-48213-BLU. What number do you see from =VALUE(MID(A2, 5, 5))?
Step 6 of 11Learn
Newer helpers: TEXTBEFORE, TEXTAFTER, TEXTSPLIT, TEXTJOIN
Microsoft 365 and Excel 2024 add functions that replace most LEFT/FIND gymnastics:
=TEXTBEFORE("Smith, John", ", ")returns "Smith".=TEXTAFTER("Smith, John", ", ")returns "John".=TEXTAFTER("INV-2026-00417", "-", 2)returns "00417": the optional third argument picks which occurrence of the delimiter to use.=TEXTSPLIT("red;green;blue", ";")spills the three colors into three cells.
Going the other way, there are three ways to join text:
- The
&operator:=B2&" "&A2turns "John" and "Smith" into "John Smith". CONCAT(B2, " ", A2)does the same with a function.TEXTJOIN(delimiter, ignore_empty, text1, ...)joins a whole range with a separator:=TEXTJOIN(", ", TRUE, C2:C10)builds "Ana, Ben, Cho" and skips blank cells when the second argument is TRUE.
A full cleanup of " SMITH, john " into "John Smith" chains these together:
=PROPER(TEXTAFTER(TRIM(A2), ", ") & " " & TEXTBEFORE(TRIM(A2), ","))
Read it inside out: trim, split at the comma, swap the pieces, fix the case. For anything this involved, consider helper columns or the LET function covered at the end of this module.
Step 7 of 11Practice · Match the pairs
Match each task to the best function.
Tap an answer, then tap the place where it belongs (or drag it there).
- Remove extra spaces before a lookup
- Find the position of "@" in an email address
- Join 30 tags into one cell with commas, skipping blanks
- Take everything after the first comma
- Turn "jOHN sMITH" into "John Smith"
Step 8 of 11Learn
TEXT and VALUE: numbers in and out of text
When you join a number or date with text, Excel uses the raw stored value, not the formatting you see. If A2 shows 3/15/2026, ="Due: "&A2 produces "Due: 46096", the date's serial number. The TEXT function applies a format as it converts:
="Due: "&TEXT(A2, "mmm d, yyyy") gives "Due: Mar 15, 2026".
Useful format codes:
| Code | Example result |
|---|---|
"0.00" | 1234.50 |
"#,##0" | 1,235 |
"$#,##0.00" | $1,234.50 |
"0%" | 8% |
"yyyy-mm-dd" | 2026-03-15 |
"dddd" | Sunday |
"00000" | 00417 (pads with zeros) |
VALUE goes the other way, turning text like "48213" or "12.5" into a real number. A shortcut is the double negative, --A2, which forces the same conversion and is common in older formulas.
The "00000" code solves a frequent lookup failure: ZIP codes and IDs where one list stores 417 as a number and the other stores "00417" as text. TEXT(A2,"00000") makes the two match.
Step 9 of 11Note
Step 10 of 11Practice · Fill in the blanks
Fill in the blanks
=PROPER("mARIA lopez") returns .
Step 11 of 11Practice · Choose one
A2 contains "Ruiz, Ana" (surname, comma, space, first name). Which formula returns just "Ana" in Microsoft 365?
Lesson complete!