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. 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))
5. Function not working
The REPLACE function returns
- #VALUE! error if you a negative input value in the num_chars argument.
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!), the function will return the same error.
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. Use the SUBSTITUTE function if you want to substitute a given string with another string.
5.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
5.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference C4 converted to hard-coded value using the F9 key. The REPLACE function requires numerical values equal to or larger than 0 (zero) which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
5.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
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