Skip to content

REPLACE

Replaces a set number of characters, starting at a position you choose, with new text.

Syntax

=REPLACE(, , , )

Text

All versions of Excel.

Try it

Edit the formula in C2 or change the inputs. The sheet recalculates as you type.

C2
A
B
C
1
Old ref
Amount
New ref
2
INV-0457
1,840
BIL-0457
3
INV-0458
625
4

C2 =REPLACE(A2,1,3,"BIL") returns BIL-0457

How REPLACE works

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.

The arguments

  • old_text is the text to change.
  • start_num is where to start. The first character is position 1.
  • num_chars is how many characters to remove from that point.
  • new_text is what to put in their place. It does not need to be the same length as what it replaces.

=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.

Three patterns

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.

Masking

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.

Fixed positions are the weakness

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.

REPLACE or SUBSTITUTE?

| 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.

Edge cases

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.

Arguments

old_text
The text to change. Usually a cell such as A2.
start_num
The position of the first character to replace. The first character of the text is position 1.
num_chars
How many characters to remove, starting at start_num. Use 0 to insert new_text without removing anything.
new_text
The text to put in. Use "" to remove characters without adding any.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

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
A
B
C
1
Old ref
Amount
New ref
2
INV-0457
1,840
BIL-0457
3
INV-0458
625
4

C2 =REPLACE(A2,1,3,"BIL") returns BIL-0457

Example 2

Insert a character without removing any

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
A
B
C
1
Raw SKU
Product
Formatted
2
FX1010
Nitrile gloves
FX-1010
3
FX1085
Floor cleaner
4

C2 =REPLACE(A2,3,0,"-") returns FX-1010

Example 3

Mask all but the last four characters

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
A
B
C
1
Account
Owner
Masked
2
4021887712
Harbour Supplies
******7712
3
4

C2 =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) returns ******7712

Example 4

Delete characters by position

C2 =REPLACE(A2,1,4,"")

Result: 0457

An empty new_text deletes the four characters from position 1.

C2
A
B
C
1
Ref
Owner
Number only
2
INV-0457
Finance
0457
3
4

C2 =REPLACE(A2,1,4,"") returns 0457

Your turn.

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.

B2
A
B
C
D
1
Raw SKU
Formatted
Account
Masked
2
FX1010
40218877
3
FX1085
40731204
4
FX2040
40559316
5
FX3012
40128745
6
7

Common mistakes

  • All four arguments are required. To insert, give 0 as num_chars. =REPLACE(A2,3,"-") is rejected for having too few arguments.
  • REPLACE works by position. When prefixes or fields differ in length, the same formula hits the wrong characters in some rows. Use FIND to calculate the position or length.
  • Positions count from 1, not 0. A start_num of 0 returns #VALUE!.
  • The result is text, even when it looks like a number. Wrap the formula in VALUE if you need a number.
  • REPLACE and SUBSTITUTE are easy to mix up. If your goal is "change every X to Y", you want SUBSTITUTE.

Questions people ask

What is the difference between REPLACE and SUBSTITUTE?

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.

How do I insert a character into text in Excel?

Use REPLACE with num_chars set to 0: `=REPLACE(A2,3,0,"-")` inserts a dash before the third character without deleting anything.

How do I delete the first few characters from a cell?

Use `=REPLACE(A2,1,4,"")` to remove the first four. `=RIGHT(A2,LEN(A2)-4)` gives the same result.

How do I hide part of a number, like a card or account number?

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))`.

What happens if start_num is beyond the end of the text?

REPLACE adds new_text at the end of the text. A start_num below 1 returns `#VALUE!`.

Learn REPLACE in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.