# REPLACE function in Excel

> REPLACE swaps out a set number of characters at a given position, whatever those characters are.

Source: https://spreadsheetpractice.com/functions/replace · Updated October 28, 2026 · Formulas and results on this page were run by the Spreadsheet Practice formula engine on November 20, 2026

## Quick answer

- **What it does:** REPLACE swaps out a set number of characters at a given position, whatever those characters are.
- **Syntax:** `=REPLACE(old_text, start_num, num_chars, new_text)`
- **Most common use:** `=REPLACE(A2,1,12,"************")` returns ************4444 (Masks all but the last four digits)
- **Available in:** Every current version
- **Category:** [Text functions](https://spreadsheetpractice.com/functions/category/text)
- **Returns:** Text with the replacement made.
- **Google Sheets:** Works the same in Google Sheets.

## What it does

REPLACE swaps out a set number of characters at a given position, whatever those characters are. Use it to mask card numbers, change a fixed prefix, or insert a character into a code, such as a dash after the second character.

## Syntax

`=REPLACE(old_text, start_num, num_chars, new_text)`

| Argument | Required? | What it is |
|---|---|---|
| `old_text` | Required | The text or cell to change. |
| `start_num` | Required | Position of the first character to replace. The first character is 1. |
| `num_chars` | Required | How many characters to replace. Use 0 to insert without removing anything. |
| `new_text` | Required | The text to put in. |

## Examples

Each result was calculated by running the formula on the sample sheet.

| Cell | Formula | Result | What it shows |
|---|---|---|---|
| D2 | `=REPLACE(A2,1,12,"************")` | ************4444 | Masks all but the last four digits |
| D3 | `=REPLACE(B3,1,3,"651")` | 651-555-0193 | A new area code in the first three characters |
| D4 | `=REPLACE(C4,3,0,"-")` | EF-9012 | num_chars of 0 inserts a dash without removing anything |

### The sample sheet

Every formula above runs on this data:

|  | A | B | C | D |
|---|---|---|---|---|
| 1 | Card number | Phone | Code | Result |
| 2 | 4111222233334444 | 612-555-0148 | AB1234 | ************4444 |
| 3 | 5500111122223333 | 763-555-0193 | CD5678 | 651-555-0193 |
| 4 | 3400999988887777 | 952-555-0107 | EF9012 | EF-9012 |

## Common mistakes

| What you see | Why, and how to fix it |
|---|---|
| Using it like find-and-replace | REPLACE doesn't search. =REPLACE(A2,"-",1,"") returns #VALUE! because "-" isn't a position. Use SUBSTITUTE to replace text by what it says. |
| Values of different lengths | A fixed start_num breaks when values vary in length. Calculate it instead: =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) masks all but the last four characters of any length. |
| Numbers turn into text | REPLACE returns text, so a changed number won't sum until you convert it with VALUE. |

## Tips

- Set num_chars to 0 to insert text at a position without deleting anything.

## Related functions

- [SUBSTITUTE](https://spreadsheetpractice.com/functions/substitute): SUBSTITUTE replaces specific text with new text everywhere it appears, or only at the occurrence you choose.
- [MID](https://spreadsheetpractice.com/functions/mid): MID returns characters from the middle of a text string, starting at a position you choose.
- [LEFT](https://spreadsheetpractice.com/functions/left): LEFT returns a set number of characters from the start of a text string.

## Questions

### How do I replace the first character in Excel?

=REPLACE(A2,1,1,"X") replaces the first character with X. To delete it, use =REPLACE(A2,1,1,"").

### How do I mask a credit card number in Excel?

=REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) hides everything but the last four characters. Store card numbers as text, because Excel keeps only 15 digits of a number.

## Practice

A graded REPLACE exercise ("Mask account numbers") is on [the REPLACE page](https://spreadsheetpractice.com/functions/replace). It is checked instantly, then re-checked on changed data.

---

Spreadsheet Practice (https://spreadsheetpractice.com) teaches spreadsheet formulas by practice: every exercise is graded in a live grid and re-checked on changed data.
