SUBSTITUTE Function in Excel: Replace Text by Matching

SUBSTITUTE Function in Excel Replace Text by Matching

Have you ever wanted to replace a specific word, number, or character in Excel without changing the entire cell?

For example, you may want to replace only the second hyphen in a product code, change multiple special characters, or update specific text in a list.

Excel offers different ways to replace text:

  • Find and Replace (Ctrl + H): Use this option to find specific text and replace it manually in your worksheet.
  • REPLACE Function: Use this function to replace characters based on their position in a text string.
  • SUBSTITUTE Function: Use this function to find specific text and replace it with new text using a formula.

All three methods are useful, but they work differently.

In this article, you will learn how to use the SUBSTITUTE function with simple, practical examples.

What is the SUBSTITUTE Function in Excel?

The SUBSTITUTE function in Excel helps you find specific text in a cell and replace it with new text.

You can use it to replace words, numbers, or special characters.

You can also replace only a particular occurrence of a character instead of replacing every matching character.

For example, if a cell contains LAP-2026-101 and you want to replace only the second hyphen with a slash, you can use the SUBSTITUTE function.

SUBSTITUTE Function Syntax:

The syntax of the SUBSTITUTE function is:

=SUBSTITUTE(text, old_text, new_text, [instance_num])

Let’s understand each argument:

ArgumentDescription
textThe original text or cell reference in which you want to make changes.
old_textThe text you want to replace.
new_textThe new text you want to add.
instance_numOptional. Specifies which occurrence of the old text you want to replace.

Note: If you leave out instance_num, Excel replaces all occurrences of the matching text.

Examples of the SUBSTITUTE Function in Excel:

Let’s look at some practical examples to understand how the SUBSTITUTE function works.

Example 1: Replace Only a Specific Occurrence

Suppose you have a list of product codes containing two hyphens. You want to replace only the second hyphen with a slash (/).

Product Code
LAP-2026-101
MOB-2026-201
TAB-2026-301
DES-2026-401

Enter this formula in cell B2:

=SUBSTITUTE(A2,”-“,”/”,2)

Press Enter and drag the formula down.

Result:

Replace Only a Specific Occurrence Using SUBSTITUTE Function in Excel

Here:

  • A2 is the original product code.
  • “-” is the character you want to replace.
  • “/” is the new character.
  • 2 tells Excel to replace only the second occurrence of the hyphen.

As you can see, the first hyphen remains unchanged, while the second hyphen is replaced with a slash.

Can we do this using Ctrl + H?

Ctrl + H can replace all matching hyphens, but it cannot directly replace only the second occurrence. You would need a formula or another method to target that specific occurrence.

This is where the SUBSTITUTE function is useful.

Example 2: Replace Multiple Characters Using One Formula

You can also use the SUBSTITUTE function to replace more than one type of character in a single formula.

Now, using the same product codes, let’s replace the hyphens with slashes (/) and underscores (_) with spaces. We can do both replacements using a single formula.

Enter this formula in cell B2:

=SUBSTITUTE(SUBSTITUTE(A2,”-“,”/”),”_”,” “)

Press Enter and drag the formula down.

Result:

Replace Multiple Characters Using SUBSTITUTE Function in Excel
Here is how the formula works:
  1. The first SUBSTITUTE function replaces the hyphen (-) with a slash (/).
  2. The second SUBSTITUTE function replaces the underscore (_) with a space.

Both replacements happen in one formula.

Can we do this using Ctrl + H?

Yes, but you need to perform two separate Find and Replace operations. With the SUBSTITUTE function, you can combine both replacements in one formula and apply them to multiple rows.

Example 3: Replace Text Using Cell References

You don’t always need to type the old and new text directly into the formula. You can also enter them in separate cells and use cell references.

Suppose you have a list of product descriptions in column A. You want to replace the word Old with New.

Product Description
Old Laptop
Old Monitor
Old Keyboard
Old Mouse

Enter the following details:

CellValue
D2Old
E2New

Now, enter this formula in cell B2:

=SUBSTITUTE(A2,$D$2,$E$2)

Press Enter and drag the formula down.

Result:

Replace Text Using Cell References Using SUBSTITUTE Function in Excel

Here:

  • A2 contains the original text.
  • $D$2 contains the text you want to replace.
  • $E$2 contains the new text.

The dollar signs keep the references to D2 and E2 fixed when you drag the formula down.

The advantage is that you can change the values in D2 and E2 whenever needed. Excel will automatically update the results without changing the formula.

Difference Between REPLACE and SUBSTITUTE Functions in Excel:

Both REPLACE and SUBSTITUTE functions are used to change text in Excel. However, they work in different ways.

REPLACESUBSTITUTE
Changes text based on its position.Changes text by matching specific text.
Requires the starting position and number of characters.Requires the text you want to replace and the new text.
Useful when you know which characters to change based on their position.Useful when you know the exact word, number, or character to replace.

When to Use REPLACE and SUBSTITUTE Functions?

The main difference between these two functions is what you need to specify in the formula.

  • REPLACE: You need to specify the starting position and how many characters you want to replace. You don’t need to know which characters are present.
  • SUBSTITUTE: You need to specify the exact word, number, or character you want to replace. You can also combine multiple SUBSTITUTE functions to replace different characters in the same text.

Let’s understand this with an example.

Example 1: When to Use REPLACE

Suppose you have the following product codes:

Product Code
ABC*1001
XYZ#1002
PQR@1003
DEF$1004

You want to replace the fourth character in every product code with a hyphen (-).

Enter this formula in cell B2:

=REPLACE(A2,4,1,”-“)

Press Enter and drag the formula down.

Result:

Remove or Replace Special Characters using  REPLACE function

Here, the formula replaces the fourth character in every product code with a hyphen, even though the original characters are different.

Example 2: When to Use SUBSTITUTE

Suppose you have product codes containing different special characters, and you want to replace:

  • Hyphens (-) with spaces.
  • Underscores (_) with spaces.
  • Forward slashes (/) with spaces.
Product codes
LAP-2026_101
MOB/2026_201
TAB-2026/301
DES_2026-401

Enter this formula in cell B2:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,”-“,” “),”_”,” “),”/”,” “)

Press Enter and drag the formula down.

Result:

When to Use SUBSTITUTE FUNCTION

Here, the formula uses three SUBSTITUTE functions:

1. The first replaces hyphens with spaces.

2. The second replaces underscores with spaces.

3. The third replaces forward slashes with spaces.

All three replacements happen using a single formula.

In simple terms:

  • Use REPLACE when you want to change characters based on their position, even if the characters are different.
  • Use SUBSTITUTE when you want to replace specific words or characters. It is especially useful when you want to replace multiple different characters in the same text using one formula.

Important Things to Know About the SUBSTITUTE Function:

  • SUBSTITUTE replaces text by matching the word, number, or character you specify.
  • If you do not specify the occurrence number, Excel replaces all matching occurrences.
  • You can use instance_num to replace only a particular occurrence.
  • You can use an empty value (“”) to remove unwanted text or characters.
  • SUBSTITUTE does not change the original text in the source cell. The result appears in the cell where you enter the formula.
  • SUBSTITUTE is case-sensitive. For example, Excel and EXCEL are treated as different text.
  • You can combine multiple SUBSTITUTE functions in one formula to make more than one replacement.

Final Thoughts:

The SUBSTITUTE function in Excel makes it easy to find and replace specific text using a formula.

You can use it to replace words, change special characters, replace a particular occurrence, and even combine multiple replacements in one formula.

Remember, use Find and Replace (Ctrl + H) for manual replacements, REPLACE to change characters based on their position, and SUBSTITUTE to replace text by matching it.

Once you understand the difference, you can choose the right method for your work and save time when updating or cleaning data.


Discover more from Excellopedia

Subscribe to get the latest posts sent to your email.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.