Home > Formulas > Text > SUBSTITUTE or REPLACE SUBSTITUTE and REPLACE FunctionsUse Microsoft Excel SUBSTITUTE and REPLACE functions to change the contents of a text string, by removing old characters, and adding new characters. See examples, to help you decide which function will work best for your worksheet. |
SUBSTITUTE vs REPLACEIf you need a formula to replace old text with new text, which Excel function should you use - SUBSTITUTE or REPLACE? Here are a few examples of when to use each function, and there are more examples further down the page. SUBSTITUTE FunctionUse the SUBSTITUTE function if you need to make one of the following changes:
REPLACE FunctionUse the REPLACE function if you need to make one of the following changes:
NoteIn some cases, it might be quicker and easier to use the Find and Replace commands on the Excel Ribbon, instead of creating formulas. |
SUBSTITUTE FunctionThe SUBSTITUTE function is in the Text category of Excel functions, and it replaces old text with new text, in a text string. This short video shows three examples, and there are written instructions below the video. SUBSTITUTE SyntaxThe SUBSTITUTE function has the following arguments in its syntax:
Here are the details for each of the four arguments:
|
SUBSTITUTE Tips
How Could You Use SUBSTITUTE?Here are 3 examples of how you could use SUBSTITUTE, and the examples are shown in the sections below: -- Ex 01 -- Change region name in report title -- Ex 02 -- Remove non-printing characters -- Ex 03 -- Replace last space character |
Ex 1: Change Report TitleIn the following example, you can quickly customize a regional report title, by selecting a region name from a drop down list. Here's where all of the information is located on the worksheet shown in the screen shot below:
Here is the formula in cell C3, which removes the “yyy” in the title text, and replaces that string with the region name selected in cell D13. Note: The text to replace is inside double quotes in the formula, "yyy", so Excel recognizes it as a text string.
TIP: To calculate the Revenue and Costs, use SUMIFS formulas that refer to the selected region name |
Ex 2: Remove Non-Printing CharactersWhen you copy data from a website, and paste it into Excel, there might be hidden, non-printing space characters in the text
If you're cleaning up the imported text, the Excel TRIM function can remove normal space characters (32), but it ignores the non-printing space characters (160). Instead, you can use the SUBSTITUTE function to replace each of the non-printing spaces with a normal space character. Then, use TRIM to remove all the extra normal space characters SUBSTITUTE FormulaHere is the formula in cell E3, and copied down to cell E4:
|
Ex 3: Replace Last Space CharacterInstead of replacing all instances of a text string, you can use the SUBSTITUTE function’s instance_number argument to select a specific instance. For example, to replace the first occurrence of a space character in cell B3, with a hyphen, you could use this formula, with the 4th argument set as 1:
Replace Last OccurrenceIn this list of recipe ingredients, we want to replace the last space character only In cell C3, the LEN function calculates the number of characters in cell B3. The SUBSTITUTE function replaces all the space characters with empty strings, and the second LEN function finds the length of the revised string. The length is 2 characters shorter, so there are two spaces. SUBSTITUTE Formula with HelperFirst, here is the formula in cell C3, to calculated the number of space characters in cell B3:
Next, in cell D3, the SUBSTITUTE formula replaces only the 2nd space character with the new text - space, pipe character, space -- " | "
Note: Instead of using a "helper" column for this formula, you could combine the two formulas into one longer formula.
|
REPLACE FunctionThe REPLACE function is in the Text category of Excel functions, and it replaces a specified number of characters in a text string, with new text. This short video shows three examples, and there are written instructions below the video. REPLACE SyntaxThe REPLACE function has the following arguments in its syntax:
Here are the details for each of the four arguments:
|
REPLACE Tips
How Could You Use REPLACE?Here are 3 examples of how you could use REPLACE, and the examples are shown in the sections below: -- Ex 01 -- Change area code in phone number -- Ex 02 -- Replace first space with colon and space -- Ex 03 -- Use nested REPLACE to insert hyphens |
Ex 1: Change Phone Area CodeIn this example, you can change the first three digits in a list of phone numbers, where a new area code was introduced. Here's where all of the information is located on the worksheet shown in the screen shot below:
Here is the formula in cell D3, which removes 3 characters, from the old phone number, starting from character 1, and adds the new area code, from cell C3
|
Ex 2: Replace First Space CharacterIn the example shown below, we want the formula to replace the first space character with a colon and space character. In each cell, the first space could be in either position 5 or position 6, so we can't "hard code" that number into the formula. Instead, you can use the FIND function, to locate the first space character. REPLACE FormulaHere is the formula in cell C3, and copied down to cell C5:
The second argument, start_num, is the result of the FIND formula. |
Ex 3: Insert Multiple HyphensThe REPLACE function can be nested, to make multiple replacements in the old text string. In this example, each phone number needs to have two hyphens inserted:
NOTE: None of the existing characters in the phone number should be removed Nested REPLACE FormulaFirst, here is the nested REPLACE formula in cell C3, and copied down to cell C4:
The third argument, num_chars, is set a zero, so none of the existing characters in the phone number will be removed. |
Video: Add or Remove Line BreaksIn this short video, Sarah shows how to add a manual line break in a cell. Then, see how to find and replace line breaks in a group of cells on the worksheet. Written steps are on the Add or Remove Line Breaks page. Video Timeline
Note: The full video transcript is available on the Add or Remove Line Breaks page. |
Video: Find and Replace With WildcardsIn this video, you'll see how to use the * wildcard with the Excel Find and Replace commands, to clean up the names in a contact list. There are written steps, and the full video transcript on the Excel Find and Replace commands page. Video Timeline
|
Get the Practice Files
|
More Functions |
Last updated: January 28, 2023 10:36 AM