REPLACE vs SUBSTITUTE in Excel: which one to use
SUBSTITUTE swaps text you name; REPLACE swaps text at a position you give. Examples of each, and how to change only one occurrence.
SUBSTITUTE changes text you name, wherever it is. REPLACE changes the characters at a position you give, whatever they are. So use SUBSTITUTE when you know what the text says. Use REPLACE when you know where it sits.
Both work in every version of Excel. Neither changes the cell you point at. Each one gives a new text in the cell where you type the formula.
Here is a real kind of problem. A clothing shop gets product codes from its supplier. Each code has 4 parts with hyphens between them. The supplier has just changed some rules. Type the codes in A2 to A5.
| Row | A | Notes (not in the sheet) |
|---|---|---|
| 1 | Code | What it means |
| 2 | TRS-24-BLK-24 | Trousers, season 24, black, waist 24 |
| 3 | TRS-24-NVY-32 | Trousers, season 24, navy, waist 32 |
| 4 | JKT-24-GRN-M | Jacket, season 24, green, size M |
| 5 | SHT-23-WHT-L | Shirt, season 23, white, size L |
SUBSTITUTE: you know the text
The shape is =SUBSTITUTE(text, old, new). The supplier now
writes NAVY instead of NVY.
=SUBSTITUTE(A3,"NVY","NAVY") gives TRS-24-NAVY-32.
Fill it down. The other rows come back unchanged, because they have no NVY in them. That is the safe side of SUBSTITUTE. If the old text is not there, nothing happens.
To remove something, put 2 quote marks with nothing between them as the new text.
=SUBSTITUTE(A2,"-","") gives TRS24BLK24. This is how most people
use SUBSTITUTE: taking spaces out of phone numbers, or hyphens out of codes.
SUBSTITUTE cares about capitals. =SUBSTITUTE(A3,"nvy","NAVY")
gives TRS-24-NVY-32, with no change and no error. If a SUBSTITUTE seems to do nothing, check
the capitals first.
REPLACE: you know the position
The shape is =REPLACE(text, start, how many, new). The season
moves from 24 to 25. In every code, the season is characters 5 and 6.
=REPLACE(A2,5,2,"25") gives TRS-25-BLK-24.
Fill it down: TRS-25-NVY-32, JKT-25-GRN-M, SHT-25-WHT-L. The shirt was season 23, and it changes too. REPLACE does not read the old characters. It counts to position 5 and changes 2 characters.
The new text does not need the same length.
=REPLACE(A4,1,3,"COAT") changes 3 characters into 4 and gives
COAT-24-GRN-M.
A length of 0 removes nothing. It only puts the new text in. Say A7 holds TRS24BLK, with no
hyphens. =REPLACE(A7,4,0,"-") gives TRS-24BLK.
Changing only one occurrence
Look at A2 again: TRS-24-BLK-24. The first 24 is the season. The second 24 is the waist size. Somebody wants season 25 and types this:
=SUBSTITUTE(A2,"24","25") gives TRS-25-BLK-25.
The waist size changed too. SUBSTITUTE changes every match unless you tell it which one. The result looks fine, and the trousers are now listed in the wrong size.
There are two good fixes.
-
Give SUBSTITUTE a 4th argument.
=SUBSTITUTE(A2,"24","25",1)changes only the 1st match and gives TRS-25-BLK-24. With 2, it changes only the 2nd match. -
Use REPLACE.
=REPLACE(A2,5,2,"25")gives the same TRS-25-BLK-24. The position of the season never changes, so this is the better choice here.
The 4th argument also works for separators.
=SUBSTITUTE(A2,"-","/",3) changes only the 3rd hyphen and gives
TRS-24-BLK/24.
When the position moves: REPLACE with FIND
Not every code has a 3-letter start. When the position changes from row to row, let FIND work
it out. =FIND("-",A2) gives 4, the position of the first
hyphen.
=REPLACE(A2,1,FIND("-",A2)-1,"TROUSER") gives
TROUSER-24-BLK-24.
It changes all the text before the first hyphen, long or short. Here FIND gives 4. So REPLACE starts at 1 and changes 3 letters.
The FIND vs SEARCH article explains FIND in more detail.
What goes wrong with REPLACE
REPLACE trusts the layout. Say one code was typed as TRS-2024-BLK-30, with a 4-digit year.
=REPLACE(A6,5,2,"25") gives TRS-2524-BLK-30.
There is no error. The code is simply wrong. Before you fill REPLACE down a long column, check
that every value has the same shape. LEN is a quick test.
=LEN(A6) gives 15, but A2 has only 13 characters.
Questions people ask
Is REPLACE the same as Find and Replace?
No. Ctrl+H opens Find and Replace. It finds and changes text, like SUBSTITUTE. But it changes the cells themselves. The functions leave the old values and write new ones next to them. Use Ctrl+H for a one-time clean-up. Use a formula when you want to see the old and new values side by side.
Why does my number not add up after SUBSTITUTE?
SUBSTITUTE always gives text. Say an export has "1 250" with a space in it.
=SUBSTITUTE(A8," ","") gives 1250, but as text, so SUM skips it.
Put VALUE around it: =VALUE(SUBSTITUTE(A8," ","")) gives the
number 1250. The
numbers stored as text article has
more fixes.
Can SUBSTITUTE change two different things at once?
Not in one call. Put one SUBSTITUTE inside another.
=SUBSTITUTE(SUBSTITUTE(A3,"NVY","NAVY"),"TRS","TROUSER") makes
both changes. Keep it to 2 or 3 levels, or it gets hard to read.
Where can I practise these?
In the REPLACE, SUBSTITUTE and FIND lessons below. Each lesson is a real sheet you type into, and each answer is checked when you press Enter.