Excel REPLACE Function – How To Use

The REPLACE function replaces the specified number of characters from the string based on the starting position with the mentioned text, string, or value. The REPLACE function is a text function; therefore, the return value is always in text format.

The REPLACE function can also be used to remove a part of the string by simply swapping the text with an empty string.

Excel REPLACE Function

Syntax

The REPLACE function accepts four arguments, and all of them are mandatory. The syntax is as follows.

=REPLACE(old_text, start_num, num_chars, new_text)

Arguments:

Understand each argument to be able to use the REPLACE function efficiently.

'old_text' – This is the input text which we wish to replace. The value of old_text can be inserted directly in double quotes or using a cell reference.
'start_num' – This argument suggests the character's starting position in the input text from where we want to replace the text.
'num_chars' – The number of characters we want to replace from the input text.
'new_text' – This argument contains the value of the replacement text or characters. The new text value that we wish to swap the old one with.

Important Characteristics of the REPLACE Function

One of the most basic aspects of the REPLACE function is that it counts each character, number, alphabet, and space as 1, irrespective of the language setting of the laptop. Other noteworthy features of the REPLACE function are mentioned below.

  • Even if the value of the old_text argument is in numerical format, the REPLACE function swaps the character as required, but the return value will be in text format.
  • When the value of the start_num argument is less than or equal to zero, the REPLACE function gives a #VALUE error.
  • A non-numeric value used as start_num or num_chars returns a #NAME? error.
  • The REPLACE function returns a #VALUE error if the value of the num_chars argument is negative.
  • If the text value of new_text is not enclosed in double quotes, the function returns a #NAME? error.

Examples of REPLACE Function

As the REPLACE function accepts several arguments, let's understand the basic functionality with the help of an example.

Example 1 – Basic Functionality of REPLACE Function

In this example, we have taken sample data of a full name, address, and phone number to understand how the REPLACE function behaves with different input text.

Basic Functionality of REPLACE Function

In the first example, we are simply replacing one character at the 1st position with the letter "S". As we can see in the return value in cell B3, one character at the first position, which is the letter 'T' is replaced with the letter 'S'. Similarly, in the second example, we intend to replace 4 characters starting from the 1st position with "XXXX", as seen in the return value in cell B4.

In the third example, the input value is in a numerical format, and we are trying to replace 1 character in the 4th position, which is "5", with "@". The return value is as expected but is now in text format, which is evident due to its left alignment.

Hopefully, the basic functionality of the REPLACE function is clear now. In the examples below, we will learn more about its applications.

Example 2 – Replacing Text when Location is Known

There are instances when companies rebrand by altering how they write their brand names or changing the name entirely. Recently, Facebook rebranded itself by changing its name to Meta. In this example, we have a few facts about Facebook, but after the rebranding, the term 'Facebook' must be replaced with 'Meta'.

Doing it manually is a daunting task and inefficient. Instead, we can use the REPLACE function to our advantage.

Replacing Text when Location is Known

The formula used will be as follows.

=REPLACE(B3,1,8,"Meta")
Replacing Text when Location is Known

As the word that needs to be replaced is at the beginning of the string, it is easier to use the REPLACE function as the value of the start_num argument can be 1 for all the strings. We also know the length of the text that needs to be replaced, which is 'Facebook'; therefore, the value of the num_chars is 8.

In the above scenario, the position of the word to be replaced was known, and it was the same throughout the data. In most cases, things are not so simple. Let's find out how to replace the text if the starting position of the intended word keeps changing.

Example 3 – Replacing Text with Variable Position

In continuation of the last example, let's consider the same scenario of Facebook rebranding to Meta. We have the email IDs of the employees that will be updated as per the new company name. In the case of email IDs, the username length varies with each email ID; therefore, the value of the start_num argument shall be different.

Replacing Text with Variable Position

To determine the position of the first character of '@facebook' in the email IDs, we can use the FIND function. The formula used will be as follows.

=FIND("@facebook",B3)

Now, let's compile the REPLACE function to swap '@facebook' with '@meta' so that we have the list of updated email IDs.

The return value of the FIND function will be the value of start_num argument of REPLACE function. The number of characters to be replaced is the length of '@facebook' which is 9.

The final formula is as follows.

=REPLACE(B3,FIND("@facebook",B3),9,"@meta")
Replacing Text with Variable Position

Example 4 – REPLACE Function with Dates

In this example, we have the list of fixed holidays for the year 2022. Now, we need to update the calendar for 2023. The solution is simply to replace 2022 with 2023, as the fixed holiday dates remain the same.

REPLACE Function with Dates

As we know, the REPLACE function works on numbers, but it changes the format to text. So, when REPLACE function is used with dates, the values do get replaced but the original formatting is replaced with text format.

Let's use the REPLACE function on the dates given in column B. The formula used will be as follows:

=REPLACE(B3,8,4,2023)
REPLACE Function with Dates

As observed, the return value of the REPLACE function in column C is clearly not a date. The reason is that Excel stores dates in numerical sequence and the date we see is just mere formatting. We can use the TEXT and the REPLACE functions to ensure that the return value is also in date format.

First, we will convert the date in column B to text format using the TEXT function with the format "dd-mmm-yyyy". We will then replace the 4 characters of the year i.e., 2022 with 2023 starting from the 8th position. The final formula to be used is as follows.

=REPLACE(TEXT(B3,"dd-mmm-yyyy"),8,4,2023)
REPLACE Function with Dates

We now have the updated calendar for 2023.

Example 5 – Removing Text using REPLACE Function

With the help of all the above examples, we have seen how to use the REPLACE function to swap the existing text with the required one. Another useful application of the REPLACE function is removing characters from an existing text. This can be achieved by simply replacing the intended text with an empty string.

In this example, we have downloaded a list of must-read books for the upcoming holiday. But every book name has a prefix 'Book n:'. We wish to share the same booklist on social media and want to remove the prefix.

Removing Text using REPLACE Function

We will use the simple application of the REPLACE function by replacing the initial 8 characters i.e., 'Book n:' with empty text starting from the 1st position. The formula used will be as follows:

=REPLACE(B3,1,8,"")
Removing Text using REPLACE Function

We now have the booklist without the prefix of book numbering.

Example 6 – Nested REPLACE function

There are instances when we need to replace several characters in a string. One way to do that is by performing one replacement and then performing the second replacement on the return value of the previous one. However, we can avoid these iterations by using the nested REPLACE function.

In this example, we have received a list of customer phone numbers, but they are not in the regular US format.

Nested REPLACE function

To make it more presentable, we will add the country code at the beginning (+1) and then punctuate the phone number using a hyphen(-) to distinguish the area code. Let's understand it step by step.

First, we will add the country code at the beginning of the phone number by replacing 0 characters with "+1" starting from the first position. The formula used will be as follows.

=REPLACE(C3,1,0,"+1")
Nested REPLACE function

The second step is to add a hyphen after the country code to the return value of the first REPLACE function. We will do so by replacing 0 characters with "-" in the 3rd position.

The value of the first argument old_text will be the previously used REPLACE function. The formula used will be as follows.

=REPLACE(REPLACE(C3,1,0,"+1"),3,0,"-")
Nested REPLACE function

In the next steps, we add more REPLACE functions to the previous formula to add a hyphen and segregate the area code. Replace 0 characters with a hyphen in the 7th position and then finally replace 0 characters with a hyphen in the 11th position. The final formula will be as follows resulting in nested REPLACE functions.

=REPLACE(REPLACE(REPLACE(REPLACE(C3,1,0,"+1"),3,0,"-"),7,0,"-"),11,0,"-")
Nested REPLACE function

Recommended Reading: How to Format Phone Number in Excel

REPLACE Function vs SUBSTITUTE Function

The REPLACE function swaps a part of the text with the given text depending on the starting position and the number of characters we wish to replace.

The SUBSTITUTE function is very similar to the REPLACE function where it substitutes a text by searching for it in the input string. Therefore, the REPLACE function is used when the location of the text to be replaced is known and fixed whereas the SUBSTITUTE function is useful when the location is not known.

The REPLACE function can be used in combination with the FIND function if the location of the text is not known, as seen in the example above.

In addition to the stated difference, the SUBSTITUTE function also accepts an optional argument (instance_num) that indicates which occurrence of the text to be replaced should be changed to the replacement text.

We hope that this comprehensive coverage of the REPLACE function along with all the applications was beneficial for you. Practice the function to discover new ways to use it to your advantage. We will soon be back with another interesting and useful Excel function.

About Shubhra Jain

Meet Shubhra Anand Jain, a dedicated Excel enthusiast with over 5 years of expertise, specializing in data analysis, complex formula development, and automation. Based in Sweden, she's on a mission to simplify Excel, one formula at a time. Check out Shubhra's extended profile here.