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.
Table of Contents
1. Introduction
What is the difference between the REPLACE function and the SUBSTITUTE function?
REPLACE changes existing text by replacing specific characters based on a number representing the position, while the SUBSTITUTE changes one sub string 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.
What other text manipulation functions are there in Excel?
The REPLACE function is one of many text manipulation functions in Excel. Here is a list of the other functions:
- The LEFT function returns a specified number of characters from the left side of a text string.
- The RIGHT function returns a specified number of characters from the right side of a text string.
- The MID function returns a specified number of characters from a text string, starting at a specified position.
- The SUBSTITUTE function replaces a specified text string with another text string within a given text.
- The TEXTBEFORE function returns all characters in a text string before a specified delimiter or text.
- The TEXTAFTER function returns all characters in a text string after a specified delimiter or text.
Many of these functions use a given position in a string counting from left to right. For example, 2 represents B in string ABC because it is in the second position counting from left to right. The following functions are very useful for determining positions of characters in a given string.
- The LEN function returns the number of characters in a text string.
- The SEARCH function returns the position of a specified text string within a given text, and is not case-sensitive.
- The FIND function returns the position of a specified text string within a given text, and is case-sensitive.
2. Syntax
REPLACE(old_text, start_num, num_chars, new_text)
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. Example
This example shown in the image above demonstrates the REPLACE function in cell C4. Cell B4 contains "Happiness & joy!". Row 10 shows each character in a box with a corresponding number below that represents the position if you count the characters from left to right.
Formula in cell C4:
The formula in cell C4 returns "Happiness and joy!". The input value is in cell B4, the function t replaces one character "&" at position 11 with the string "and".
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.
4. REPLACE Function not working
- The REPLACE function returns #NAME! error if you misspelled the function name.
- Make sure you count the characters carefully in the string you want to edit. The LEN function lets you count the number of characters in a string or cell value.
Section 5 below demonstrates a formula that splits the string to a cell each and creates a sequence below, from 1 to n where n is the total number of characters in the string. - Use the SUBSTITUTE function if you want to substitute a given string with another string.
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:
This formula could be useful for analyzing text such as finding character positions or performing character-by-character operations. Here is a quick break-down of the formula:
- LEN(B3) - counts characters in cell value B3
- SEQUENCE(,LEN(B3)) - creates a sequence from 1 to n. Where n is the total number of characters in cell B3.
- MID(B3,SEQUENCE(,LEN(B3)),1) - splits the string in cell B3 to an array containing a character in each container.
- VSTACK(MID(B3,SEQUENCE(,LEN(B3)),1),SEQUENCE(,LEN(B3))) - stacks two arrays vertically. The first array contains the characters and the second contains the sequence from 1 to n.
- LET(x,SEQUENCE(,LEN(B3)),VSTACK(MID(B3,x,1),x)) - optimizes the formula by replacing repetitive parts with a variable (x). This shortens the formula and improves the calculation speed.
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