Text function
SUBSTITUTE function in Google Sheets and Excel
SUBSTITUTE replaces one piece of text with another inside a cell: every occurrence, or just the one you pick.
Checked against Microsoft's and Google's documentation on · Example calculated on a live sheet
- Excel 2016
- Excel 2019
- Excel 2021
- Excel 2024
- Microsoft 365
- Google Sheets
Syntax
SUBSTITUTE(text, old_text, new_text, [instance_num])Arguments in [brackets] are optional. Google Sheets writes it SUBSTITUTE(text_to_search, search_for, replace_with, [occurrence_number]).
Example
=SUBSTITUTE(B2," ","") reads: In Mara's phone number in B2, replace every space with nothing, so the spaces disappear.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Customer | Phone | Order | Amount | City | Phone |
| 2 | Okafor, Mara | +44 20 7946 0321 | ORD-2026-0412 | $1,250.00 | London | +442079460321 |
| 3 | Lind, Jonas | +46 40 123 456 | ORD-2026-0413 | $89.90 | Malmö | +4640123456 |
| 4 | Raman, Priya | +91 20 5550 1234 | ORD-2026-0419 | $2,040.00 | Pune | +912055501234 |
| 5 | Ruiz, Tomás | +34 954 000 111 | ORD-2026-0420 | $15.00 | Seville | +34954000111 |
| 6 | Demir, Elif | +90 232 555 0199 | ORD-2026-0425 | $310.50 | Izmir | +902325550199 |
| 7 | Mori, Kenji | +81 6 5550 7788 | ORD-2026-0431 | $799.00 | Osaka | +81655507788 |
F2 shows +442079460321: Mara's number without spaces.
How the spreadsheet calculates it
Replaces " " with empty text ("") in B2, every time it appears.
- 1. SUBSTITUTE: Replaces
" "with empty text ("") inB2, every time it appears.text=`B2`: the cell to changeold=`" "`: what to replace, in quotesnew=`""`: what to put instead; `""` deletes
More SUBSTITUTE examples
=SUBSTITUTE(C2,"-","/",2)Only the second hyphen becomes a slash: ORD-2026/0412.=LEN(B2)-LEN(SUBSTITUTE(B2," ",""))How many spaces B2 contains.=SUBSTITUTE(SUBSTITUTE(D2,"$",""),",","")An amount without its dollar sign and thousands separator.=VALUE(SUBSTITUTE(B2," ",""))Digits with spaces removed, turned into a real number.
Paste any of these into the formula explainer to see it step by step.
Tips
- Use
""as new_text to delete. - Give instance_num to change only one occurrence:
SUBSTITUTE(C2,"-","/",2)turns ORD-2026-0412 into ORD-2026/0412. - Nest SUBSTITUTEs for several replacements, innermost first.
What SUBSTITUTE is used for
Cleaning phone numbers, IDs and amounts exported with spaces or symbols, swapping separators (dots for commas, hyphens for slashes), removing unwanted words, and counting words or characters.
Arguments
| Argument | Required | What it is |
|---|---|---|
| text | Yes | The text or cell to change. |
| old_text | Yes | The text to find, in quotes. It must match exactly, including upper and lower case. |
| new_text | Yes | What to put instead. "" deletes old_text. |
| instance_num | No | Which occurrence to replace: 2 replaces only the second. Left out, every occurrence is replaced. |
Common mistakes
Swapping old and new.
Expecting it to change the original cell: it returns a new value.
Using it to replace by position: SUBSTITUTE finds text. To overwrite characters at a position, use
REPLACE.Expecting "london" to match "London": SUBSTITUTE is case-sensitive, so wrap the text in
LOWERorUPPERfirst if case varies.Getting text back when you wanted a number: after removing spaces or symbols from digits, wrap it in
VALUEto calculate with it.
In Google Sheets
Google Sheets has SUBSTITUTE with the same four arguments (text_to_search, search_for, replace_with, [occurrence_number]), including the optional occurrence number.
Errors you may see with SUBSTITUTE
- #NAME?: The text to find or the new text is missing its quotes, so the app reads it as a name.
Questions
How do I remove a character from text?
Replace it with nothing: SUBSTITUTE(B2,"-","") removes every hyphen. More clean-up recipes in the clean text guide.
How do I replace two different things?
Nest one SUBSTITUTE inside another: SUBSTITUTE(SUBSTITUTE(B2,"(",""),")","") removes both brackets.
How do I count how many times a character appears?
Compare the length before and after removing it: LEN(B2)-LEN(SUBSTITUTE(B2," ","")) counts the spaces in B2, which is 3 for Mara's number.
What's the difference between SUBSTITUTE and REPLACE?
SUBSTITUTE replaces specific text wherever it appears; REPLACE replaces whatever is at a given position and length. Use SUBSTITUTE when you know what to change, REPLACE when you know where.
Sources
Official documentation the facts on this page were checked against.