formulastreak

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

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.

CRM export · edit F2 and watch it recalculate
Click a cell (or use the arrow keys) to insert its address · Enter shows the resultF2 = +442079460321
ABCDEF
1CustomerPhoneOrderAmountCityPhone
2Okafor, Mara+44 20 7946 0321ORD-2026-0412$1,250.00London +442079460321
3Lind, Jonas+46 40 123 456ORD-2026-0413$89.90Malmö+4640123456
4Raman, Priya+91 20 5550 1234ORD-2026-0419$2,040.00 Pune+912055501234
5Ruiz, Tomás+34 954 000 111ORD-2026-0420$15.00Seville +34954000111
6Demir, Elif+90 232 555 0199ORD-2026-0425$310.50Izmir+902325550199
7Mori, Kenji+81 6 5550 7788ORD-2026-0431$799.00Osaka  +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. 1. SUBSTITUTE: Replaces " " with empty text ("") in B2, every time it appears.
    • text = `B2`: the cell to change
    • old = `" "`: what to replace, in quotes
    • new = `""`: 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

ArgumentRequiredWhat it is
textYesThe text or cell to change.
old_textYesThe text to find, in quotes. It must match exactly, including upper and lower case.
new_textYesWhat to put instead. "" deletes old_text.
instance_numNoWhich 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 LOWER or UPPER first if case varies.

  • Getting text back when you wanted a number: after removing spaces or symbols from digits, wrap it in VALUE to 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.

Practise SUBSTITUTE on a live sheet

Lesson 30, SUBSTITUTE, CLEAN and invisible spaces (Unit 8, Cleaning text at scale), has 6 graded exercises, checked instantly, with steps and hints when you're stuck. Pro