Text functions
REPLACE
Matches all 8 recorded answers
Recorded: 8 workbook cells. How we measure this · See the Rust code
Local names in 39 languages
| Language | Name in Excel |
|---|---|
| Arabic | REPLACE |
| Bulgarian | REPLACE |
| Danish | ERSTAT |
| German | REPLACE |
| Greek | REPLACE |
| English (Australia) | REPLACE |
| English (United Kingdom) | REPLACE |
| English (United States) | REPLACE |
| Spanish | REEMPLAZAR |
| Estonian | REPLACE |
| Finnish | KORVAA |
| French | REMPLACER |
| Hebrew | REPLACE |
| Croatian | REPLACE |
| Hungarian | CSERE |
| Indonesian | REPLACE |
| Italian | REPLACE |
| Japanese | REPLACE |
| Korean | REPLACE |
| Lithuanian | REPLACE |
| Latvian | REPLACE |
| Norwegian | REPLACE |
| Dutch | VERVANGEN |
| Polish | ZASTĄP |
| Portuguese (Brazil) | REPLACE |
| Portuguese (Portugal) | SUBSTITUIR |
| Romanian | REPLACE |
| Russian | ЗАМЕНИТЬ |
| Slovak | REPLACE |
| Slovenian | REPLACE |
| Serbian (Latin) | REPLACE |
| Swedish | ERSÄTT |
| Thai | REPLACE |
| Turkish | DEĞİŞTİR |
| Ukrainian | REPLACE |
| Vietnamese | REPLACE |
| Chinese (Simplified) | REPLACE |
| Chinese (Hong Kong) | REPLACE |
| Chinese (Traditional) | REPLACE |
Names from Microsoft's support site.
What it does
REPLACE(old_text, start_num, num_chars, new_text)
REPLACE(old_text, start_num, num_chars, new_text) returns old_text with the window of
num_chars characters beginning at position start_num removed and new_text put in its
place. Positions are 1-based.
Equivalently, in terms of the slicing functions:
REPLACE(t, s, k, new) = LEFT(t, s - 1) & new & MID(t, s + k, LEN(t))
Two consequences of that identity are worth reading off it directly, because they are the two things people get wrong about this function:
REPLACEdoes not look at what it is replacing. It never inspects the characters in the window. It cannot fail to find anything, because it does not search.- The result length is not the input length.
new_textneed not benum_charslong. Withnum_charsof 0,REPLACEinserts without deleting; withnew_textempty, it deletes without inserting. These are not tricks — they fall straight out of the definition, and they are the idiomatic way to do both operations.
REPLACEB is the byte-addressed sibling; see REPLACEB.
Arguments
| Argument | Meaning |
|---|---|
old_text | The string to modify. Required. |
start_num | 1-based position of the first character to replace. Required. |
num_chars | How many characters to remove. Required. |
new_text | What to put in their place. Required. |
All four are required — there is no "replace to the end" default and no optional argument.
start_num and num_chars are numeric slots subject to ordinary to-number coercion; see
Coercion and lifting.
The commonly misread position is num_chars. It is a count of characters to remove from
old_text, not an end position and not the length of new_text. REPLACE("abcdef", 2, 3, "XY") removes bcd and yields aXYef.
Try it
Quirks
Result and edge cases
Returns Text.
Microsoft's REPLACE page states the signature and the meaning of each argument but does not
enumerate boundary behaviours the way the MID page does. The following are therefore
expected rather than documented, and the Handbook has not checked any of them:
start_numpast the end ofold_text— plausibly an append; unverified.num_charsrunning past the end ofold_text— plausibly a clamp to the end; unverified.num_charsof 0 — insertion, as above; this follows from the definition but is not stated on the page.start_numless than 1 — the sibling functions document#VALUE!for this;REPLACE's page does not.
REPLACE versus SUBSTITUTE
These two are the most-confused pair in the text category, and the distinction is clean once
stated. Microsoft states it on the SUBSTITUTE page itself:
Use
SUBSTITUTEwhen you want to replace specific text in a text string; useREPLACEwhen you want to replace any text that occurs in a specific location in a text string.
REPLACE | SUBSTITUTE | |
|---|---|---|
| Addresses the target by | position (start_num, num_chars) | content (old_text matched literally) |
| Number of edits | exactly one window | every occurrence, or one chosen occurrence |
| If the target is not there | meaningless — there is no target to find | returns the input unchanged |
| Case sensitivity | not applicable | case-sensitive |
| Wildcards | not applicable | none |
| Length preserved | no | no |
The one-line test: if your formula computes start_num with FIND or SEARCH, you probably
wanted SUBSTITUTE. If your positions come from a fixed-width record layout, you wanted
REPLACE.
See SUBSTITUTE for the other side of this pair.
Errors
Microsoft's REPLACE, REPLACEB page does not carry an error table. The errors reachable here
are the shared ones: an error value in any argument propagates, and non-numeric text in
start_num or num_chars surfaces #VALUE! under the shared coercion rules. A result
exceeding the 32,767 code-unit text cap is the other candidate — the Handbook's value-universe
chapter records #VALUE! for the over-cap formula path observed with REPT, but whether
REPLACE publishes the same way is unverified.
How to implement
Notes for implementers
The three-piece concatenation identity above is the implementation, and the only subtleties
are at its edges: what LEFT(t, s-1) does when s-1 exceeds LEN(t), and what MID(t, s+k, LEN(t)) does when s+k exceeds it. Both must be defined so that a window entirely past the
end degrades gracefully rather than producing a #VALUE! from an intermediate step.
Because REPLACE can grow its input, it is one of the functions that can manufacture a string
over the 32,767 code-unit cap from inputs that are each individually legal. Whatever the
publication behaviour turns out to be, it has to be applied at the function's own boundary and
not left to the cell.
Counting is in UTF-16 code units, consistent with LEN and MID. A REPLACE window can
therefore split a surrogate pair, and the implementation has to survive producing ill-formed
UTF-16 — the Handbook's value-universe chapter records that state as reachable in practice.
Rust code
Show the CalcFunc source of this function
Quoted from the public repository DnaCalc/DnaCalc, crates calc-func and calc-fml, at commit 9e1884369a67. Comments are the source's own. A kernel shared with other functions appears once, on its module page.
REPLACE · 3 lines · calc-func/src/catalogue/text_functions.rs on GitHub24 replace_fn "REPLACE" (4, 4) [V "old_text", V "start_num", V "num_chars", V "new_text"] |s, a, sv| {
replace(s, a, false)
}
Kernels shared with other functions
count_surrogate_pairscatalogue/text_functionsstartcatalogue/text_functionsreplacecatalogue/text_functionscappedcatalogue/text_functionstextported/text_kernelsunit_widthported/text_kernelscharacter_boundaryported/text_kernelstruncatedported/text_kernelspositiveported/text_kernelsnonnegativeported/text_kernelsreplaceported/text_kernels
Argument and result plumbing
No implementation has been published with a Handbook test suite for this function.
Version differences
No comparison between specific Excel builds is recorded here yet. This does not establish that all builds behave alike.
Recorded variants
| Excel build | CPU | Recorded behaviour | Our Rust | Evidence |
|---|---|---|---|---|
| Compatibility Version 1, all Excel versions | Any | A surrogate pair counts as two characters. | Implemented; selected by the workbook's Compatibility Version. Code | https://support.microsoft.com/en-gb/excel/compatibility-versions · |
| Compatibility Version 2, Excel for Microsoft 365 | Any | A surrogate pair counts as one character. Characters combined from several code points still count separately. | Implemented; selected by the workbook's Compatibility Version. Code | https://support.microsoft.com/en-gb/excel/compatibility-versions · |
Alternatives
Relationships
- SUBSTITUTE — the by-content counterpart. See the section above.
- MID — the same
(start_num, num_chars)addressing, reading instead of writing.MIDextracts the windowREPLACEoverwrites. - REPLACEB — the byte-addressed sibling, documented by Microsoft on the same page and published by the Handbook as a separate entry.
FINDandSEARCHare what suppliesstart_numwhen the window is not known in advance.SEARCHis case-insensitive and takes wildcards;FINDis neither. Choosing between them changes whatREPLACEoverwrites.TEXTBEFORE/TEXTAFTERcover manyREPLACE-with-a-computed-position formulas more directly.
Named variants
No named variants are recorded yet.
Suggest a correction or contribute an implementation · For agents