Example 1
Swap a prefix
C2 =REPLACE(A2,1,3,"BIL")
Result: BIL-0457
Three characters from position 1 are replaced with BIL. The new text could be longer or shorter than the old.
C2 =REPLACE(A2,1,3,"BIL") returns BIL-0457
Replaces a set number of characters, starting at a position you choose, with new text.
Syntax
=REPLACE(, , , )
All versions of Excel.
Try it
Edit the formula in C2 or change the inputs. The sheet recalculates as you type.
C2 =REPLACE(A2,1,3,"BIL") returns BIL-0457
REPLACE overwrites characters at a position you choose. Use it to insert a dash into a code, mask part of an account number, swap a prefix, or delete characters by position. It works by location, not by content. For content-based edits, use SUBSTITUTE.
=REPLACE("INV-0457",1,3,"BIL") removes the three characters from position 1 and puts BIL there, giving BIL-0457.
All four arguments are required, so even if you only want to insert, you must give num_chars.
Replace. Remove some characters and put others in. =REPLACE(A2,1,3,"AUS") swaps a three-letter prefix.
Insert. Set num_chars to 0. Nothing is removed, and new_text goes in before the character at start_num. =REPLACE("FX1010",3,0,"-") returns FX-1010.
Delete. Set new_text to an empty string. =REPLACE(A2,1,4,"") removes the first four characters.
REPLACE is a common way to hide part of a value. =REPLACE(A2,1,4,"****") turns 40218877 into ****8877. When the number of characters to hide changes with the length, use LEN and REPT: =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) hides everything except the last four characters.
REPLACE trusts that the position is right. If one row has a longer prefix than the others, the same formula replaces the wrong characters on that row. Where positions vary, locate the marker with FIND first: =REPLACE(A2,1,FIND("-",A2)-1,"AUS") swaps whatever comes before the first dash.
| You know | Use | |---|---| | The text to change, not where it is | SUBSTITUTE | | The position, not what is there | REPLACE | | You want to change only the second match of a character | SUBSTITUTE with instance_num |
REPLACE also converts numbers to text before working. =REPLACE(40218877,1,4,"****") returns ****8877. The result is always text.
If start_num is greater than the length of the text, REPLACE adds new_text at the end. A start_num less than 1, or a negative num_chars, returns #VALUE!. If num_chars is larger than what remains, REPLACE removes everything from start_num to the end.
A2.0 to insert new_text without removing anything."" to remove characters without adding any.Each example is a live sheet. Change the inputs or the formula and watch the result.
Example 1
C2 =REPLACE(A2,1,3,"BIL")
Result: BIL-0457
Three characters from position 1 are replaced with BIL. The new text could be longer or shorter than the old.
C2 =REPLACE(A2,1,3,"BIL") returns BIL-0457
Example 2
C2 =REPLACE(A2,3,0,"-")
Result: FX-1010
num_chars is 0, so nothing is deleted. The dash goes in before the character at position 3.
C2 =REPLACE(A2,3,0,"-") returns FX-1010
Example 3
C2 =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4))
Result: ******7712
The account has 10 characters, so LEN - 4 = 6 characters are replaced by six asterisks.
C2 =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) returns ******7712
Example 4
C2 =REPLACE(A2,1,4,"")
Result: 0457
An empty new_text deletes the four characters from position 1.
C2 =REPLACE(A2,1,4,"") returns 0457
Solve it in the sheet. Every answer is checked, with hints if you need them.
Task 1 of 2
In B2, turn the raw SKU in A2 into the form FX-1010 by inserting a dash after the second character. Fill it down to B5.
Answer in B2, then it fills down to B5.
0 as num_chars. =REPLACE(A2,3,"-") is rejected for having too few arguments.#VALUE!.VALUE if you need a number.REPLACE overwrites characters at a position you give. SUBSTITUTE finds matching text anywhere in the string and replaces it. Use REPLACE when you know where, and SUBSTITUTE when you know what.
Use REPLACE with num_chars set to 0: `=REPLACE(A2,3,0,"-")` inserts a dash before the third character without deleting anything.
Use `=REPLACE(A2,1,4,"")` to remove the first four. `=RIGHT(A2,LEN(A2)-4)` gives the same result.
Replace the part you want hidden: `=REPLACE(A2,1,4,"****")`. To keep the last four characters whatever the length, use `=REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4))`.
REPLACE adds new_text at the end of the text. A start_num below 1 returns `#VALUE!`.
Tracks take you from the basics to modelling, one graded exercise at a time.