How to use the REPLACE function
What is the REPLACE function?
The REPLACE function substitutes a part of a text string based on the number of characters and length with a text string you provide.
What is the difference between the REPLACE function and the SUBSTITUTE function?
REPLACE changes existing text by replacing specific characters, while the SUBSTITUTE changes one substring with another.
What is the difference between the REPLACE function and the REPLACEB function?
The REPLACE and REPLACEB functions handle character counting differently based on your default language setting in Excel. REPLACEB is designed for double-byte languages like Chinese, Japanese, Korean. It counts each double-byte character as 2 when a DBCS language is set as the default.
REPLACE always counts each character as 1, single-byte or double-byte, regardless of language setting. So REPLACEB will count each double byte character as 2 contrary to the REPLACE for the same text in a DBCS language.
Japanese, Chinese (Simplified), Chinese (Traditional), and Korean are some examples of languages supporting DBCS.
Table of Contents
1. REPLACE Function Syntax
REPLACE(old_text, start_num, num_chars, new_text)
2. REPLACE Function Arguments
old_text | Required. The source value you want to change. |
start_num | Required. A number representing the character's position you want to replace. |
num_chars | Required. The number of characters you want to replace starting from the start_num argument. |
new_text | Required. The new value you want to insert. |
3. REPLACE Function Example
This example shown in the image above demonstrates the REPLACE function in cell C4. It replaces one character at position 11 with the string "and" based on the value in cell B4.
The character is the ampersand character, however, the REPLACE function matches no string like the SUBSTITUTE function but uses only a number representing the character's position and a number for the number of characters to replace.
Formula in cell F3:
4. REPLACE Function not working
5. Mark each character in a value with a number from left - Excel 365
The REPLACE function requires a character's position and the number of you want to replace, this can be tedious if you have lots of values to handle.
The following formula splits the value in cell B3 so each cell to the right contains a single character, the corresponding cell below contains a number from 1 to n.
This method lets you easily spot the numbers needed in the REPLACE function.
Excel 365 formula in cell B3:
Explaining formula
Step 1 - Count characters in cell
The LEN function returns the number of characters in a cell value.
Function syntax: LEN(text)
LEN(B3)
becomes
LEN("What does the fox say?")
and returns 22.
Step 2 - Create a sequence from 1 to 22 horizontally
The SEQUENCE function creates a list of sequential numbers.
Function syntax: SEQUENCE(rows, [columns], [start], [step])
SEQUENCE(,LEN(B3))
becomes
SEQUENCE(,22)
and returns
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22}
Step 3 - Split characters in cell B3 to an array
The MID function returns a substring from a string based on the starting position and the number of characters you want to extract.
Function syntax: MID(text, start_num, num_chars)
MID(B3,SEQUENCE(,LEN(B3)),1)
becomes
MID("What does the fox say?",{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22},1)
and returns
{"W","h","a","t"," ","d","o","e","s"," ","t","h","e"," ","f","o","x"," ","s","a","y","?"}
Step 4 - Stack arrays vertically
The VSTACK function combines cell ranges or arrays. Joins data to the first blank cell at the bottom of a cell range or array (vertical stacking)
Function syntax: VSTACK(array1,[array2],...)
VSTACK(MID(B3,SEQUENCE(,LEN(B3)),1),SEQUENCE(,LEN(B3)))
becomes
VSTACK({"W","h","a","t"," ","d","o","e","s"," ","t","h","e"," ","f","o","x"," ","s","a","y","?"},{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22})
and returns
{"W","h","a","t"," ","d","o","e","s"," ","t","h","e"," ","f","o","x"," ","s","a","y","?";1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22}
Step 5 - Shorten formula
The LET function lets you name intermediate calculation results which can shorten formulas considerably and improve performance.
Function syntax: LET(name1, name_value1, calculation_or_name2, [name_value2, calculation_or_name3...])
VSTACK(MID(B3,SEQUENCE(,LEN(B3)),1),SEQUENCE(,LEN(B3)))
x - SEQUENCE(,LEN(B3))
LET(x,SEQUENCE(,LEN(B3)),VSTACK(MID(B3,x,1),x))
Get Excel file
Useful resources
Excel REPLACE Function
REPLACE, REPLACEB functions - Microsoft support
Functions in 'Text' category
The REPLACE function function is one of 29 functions in the 'Text' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form