Replace string with string excel

Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel 2010 Excel 2007 Excel for Mac 2011 Excel Starter 2010 More…Less

This article describes the formula syntax and usage of the REPLACE and REPLACEB
 function in Microsoft Excel.

Description

REPLACE replaces part of a text string, based on the number of characters you specify, with a different text string.

REPLACEB replaces part of a text string, based on the number of bytes you specify, with a different text string.

Important: 

  • These functions may not be available in all languages.

  • REPLACE is intended for use with languages that use the single-byte character set (SBCS), whereas REPLACEB is intended for use with languages that use the double-byte character set (DBCS). The default language setting on your computer affects the return value in the following way:

  • REPLACE always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.

  • REPLACEB counts each double-byte character as 2 when you have enabled the editing of a language that supports DBCS and then set it as the default language. Otherwise, REPLACEB counts each character as 1.

The languages that support DBCS include Japanese, Chinese (Simplified), Chinese (Traditional), and Korean.

Syntax

REPLACE(old_text, start_num, num_chars, new_text)

REPLACEB(old_text, start_num, num_bytes, new_text)

The REPLACE and REPLACEB function syntax has the following arguments:

  • Old_text    Required. Text in which you want to replace some characters.

  • Start_num    Required. The position of the character in old_text that you want to replace with new_text.

  • Num_chars    Required. The number of characters in old_text that you want REPLACE to replace with new_text.

  • Num_bytes    Required. The number of bytes in old_text that you want REPLACEB to replace with new_text.

  • New_text    Required. The text that will replace characters in old_text.

Example

Copy the example data in the following table, and paste it in cell A1 of a new Excel worksheet. For formulas to show results, select them, press F2, and then press Enter. If you need to, you can adjust the column widths to see all the data.

Data

abcdefghijk

2009

123456

Formula

Description (Result)

Result

=REPLACE(A2,6,5,»*»)

Replaces five characters in abcdefghijk with a single * character, starting with the sixth character (f).

abcde*k

=REPLACE(A3,3,2,»10″)

Replaces the last two digits (09) of 2009 with 10.

2010

=REPLACE(A4,1,3,»@»)

Replaces the first three characters of 123456 with a single @ character.

@456

Need more help?

What Is REPLACE function In Excel?

The Excel REPLACE function replaces an old text from a string with a new string. The input required by this function is the old text, new text, and the starting and ending numbers of the characters, which must be replaced.

The REPLACE function in Excel is an inbuilt text function, similar to a SUBSTITUTE function, so we can insert the formula from the “Function Library” or enter it directly in the worksheet.

For example, in an Excel spreadsheet with numbers and text, we will replace the number “517” with the other number “987”, and retain the text “ABC”.

Using the Excel REPLACE function, we can apply the formula =REPLACE(“ABC517″,4,3,”987”). And the output will then be “ABC987”.

Table of contents
  • What Is REPLACE function In Excel?
    • Syntax of Excel REPLACE function
    • How To Use REPLACE function In Excel?
    • Important Things To Note
    • REPLACE Excel Function Video
    • Recommended Articles
  • The Excel REPLACE function is a Text function thathelps users replace an old text character or an entire string with a new set of characters or strings.
  • As a worksheet function, it can be written as a part of a formula in a worksheet cell. It can be used in macro code as a VBA function, entered through the Microsoft Visual Basic Editor integrated with MS Excel.
  • The second parameter, start_num, and the third parameter, num_chars, cannot have a non-numeric or a negative value.

Syntax of Excel REPLACE function

The syntax of the Excel REPLACE formula is,

Replace Formula in Excel

Where,

  • old_text: This is a required parameter. It is the original string to be replaced.
  • start_numIt is the starting position in the original string from where the replacement should begin.
  • num_charsIt is a numeric value and indicates the number of characters to be replaced.
  • new_text: It is another required parameter and indicates the new string/set of characters to be replaced the old_text with.

REPLACE-Function-in-Excel

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkArticle Link to be Hyperlinked
For eg:
Source: Excel REPLACE Function (wallstreetmojo.com)

How To Use REPLACE function In Excel?

The REPLACE function is used in 2 ways, namely,

  1. Access from the Excel ribbon.
  2. Enter in the worksheet manually.

Method #1: Access from the Excel ribbon

First, choose an empty cell – select the “Formulas” tab – go to the “Function Library” group – click the “Text” option drop-down – select the “REPLACE” function, as shown below.

Excel Replace Function - Formulas - Replace

The “Function Arguments” window opens. Enter the arguments in the “Old_text, Start_num, Num_chars, New_text” fields, and click “OK”, as shown below.

Excel Replace Function - Formulas - Arguments

Method #2: Enter in the worksheet manually

  1. Choose an empty cell for the output.
  2. Type =REPLACE( in the cell. [Alternatively, type =R or =RE, and select the REPLACE function from the suggestions given by Excel]
  3. Enter the arguments as cell values or cell references.
  4. Close the brackets, and press the “Enter” key to execute the formula.

Examples

We will consider some specific scenarios and their corresponding examples, such as,

  • Replace a string
  • Replace a Substring
  • Replace a Single Character
  • Replace numbers
  • Remove a string
  • Common Problem with REPLACE function

You can download this REPLACE Function Excel Template here – REPLACE Function Excel Template

Example #1 – Replace a string

In this example, cell C4 has a REPLACE formula associated with it. So, C4 is a result cell.

REPLACE Function Example 1

The steps to apply the REPLACE formula are as follows:

  1. The first argument of the REPLACE function is B4, which contains the original string to be replaced.
  2. The second argument is 1, which indicates the starting letter of the original string.
  3. The third argument is 4, which is the number of characters to be replaced.
  4. The fourth and last parameter is “Stephen”, a new string to be replaced with.

The old string is “John”, and the new string is “Stephen”.

Example #2 – Replace a Substring

REPLACE Function Example 2

In this example, cell C6 has a formula associated with it. So, C6 is a result cell.

  • The first argument of the REPLACE function is B6, which contains the original string to be replaced.
  • The second argument is 5, which indicates the starting letter of the original string.
  • The third argument is 5, which is the number of characters to be replaced.
  • The fourth and last parameter is yahoo, a new string to be replaced with.

The old string is “gmail”, and the new string is “yahoo”. As a result, C6 is updated with “[email protected].”

Example #3 – Replace a Single Character

Example 3

In this example, cell C8 has a REPLACE formula associated with it. So, C8 is a result cell.

  • The first argument of the REPLACE function is B8, which contains the original string to be replaced.
  • The second argument is 1, which indicates the starting letter of the original string.
  • The third argument is 1, which is the number of characters to be replaced.
  • The fourth and last parameter is “s,” which is a new character to be replaced with.

Here, the old character is n, and the new character is s. As a result, C8 is updated with “set.”

Example #4 – Replace numbers

Example 4

In this example, cell C10 has a REPLACE formula associated with it. So, C10 is a result cell.

  • The first argument of the REPLACE function is B10, which contains the original string to be replaced.
  • The second argument is 7, which indicates the starting letter of the original string.
  • The third argument is 4, which is the number of characters to be replaced.
  • The fourth and last parameter is “2000,” a new string to be returned with.

The old string is “1989.” The new string is “2000.” As a result, C8 is updated with “23-12-2000.”

Example #5 – Remove a string

Example 5

In this example, cell C12 has a REPLACE formula associated with it. So, C12 is a result cell.

  • The first argument of the REPLACE function is B12, which contains the original string to be replaced.
  • The second argument is 1, which indicates the starting letter of the original string.
  • The third argument is 11, which is the number of characters to be replaced.
  • The fourth and last parameter is “” which is a new string (an empty string) to be replaced with.

Here, the old string is “Remove this,” and the new string is “”. As a result, C12 is updated to a blank cell as all the characters are replaced with blanks.

Example #6 – Common Problem with REPLACE Function

example 6

In this example, cell C14 has a REPLACE formula associated with it. So, C14 is a result cell.

  • The first argument of the REPLACE function is B14, which contains the original string to be replaced.
  • The second argument is 0.

However, any string in an Excel worksheet cell starts with 1, index 1. So, the result in cell C14 is an error which is #VALUE! indicating that there is an error in the value.

Important Things To Note

  • We get the #VALUE! error, when either of the second, third, or fourth argument value is not provided.
  • If the proper cell value or cell reference is not selected, we get the “#NAME?” error.

REPLACE Excel Function Video

Frequently Asked Questions (FAQs)

1. How to insert REPLACE function in Excel?

We can insert the REPLACE function in Excel as follows:
1. Choose an empty cell for the output.
2. Type =REPLACE( in the cell. [Alternatively, type =R or =RE, and select the REPLACE function from the suggestions given by Excel]
3. Enter the arguments as cell values or cell references.
4. Close the brackets, and press the “Enter” key to execute the formula.

2. Where is the REPLACE function in Excel?

The REPLACE function is found as follows:
First, choose an empty cell – select the “Formulas” tab – go to the “Function Library” group – click the “Text” option drop-down – select the “REPLACE” function, as shown below.

Excel Replace Function - Formulas - Replace

3. Why is the REPLACE function in Excel not working?

The REPLACE function may not work for the following reasons,
We have not selected a proper cell value or cell reference.
We have not entered the start_num, and num_chars, argument values.

Download Template

This article must help understand Excel REPLACE function with its formulas and examples. You can download the template here to use it instantly.

Recommended Articles

This article has been a guide to Excel REPLACE Function. Here we replace/substitute existing string/text with another string, examples & downloadable excel template. You may also look at these useful functions in Excel: –

  • Excel VBA Find and Replace
  • VBA Replace String
  • Substring Function in VBA
  • Left Function in VBA | Examples
  • Excel MAXIFS

Содержание

  1. Функция Replace
  2. Синтаксис
  3. Параметры
  4. Возвращаемые значения
  5. Замечания
  6. См. также
  7. Поддержка и обратная связь
  8. SUBSTITUTE function
  9. Description
  10. Syntax
  11. Example
  12. SUBSTITUTE Function
  13. Related functions
  14. Summary
  15. Purpose
  16. Return value
  17. Arguments
  18. Syntax
  19. Usage notes
  20. Examples
  21. Related functions
  22. How to Use Excel REPLACE Function (Examples + Video)
  23. Syntax of the REPLACE Function
  24. Example 1 – Replace Text with Blank
  25. Example 2: Extract the User Name from the Domain name
  26. Example 3: Replace One Text String with Another
  27. Difference between Replace and Substitute functions
  28. Excel REPLACE Function – Video Tutorial
  29. REPLACE Function
  30. Related functions
  31. Summary
  32. Purpose
  33. Return value
  34. Arguments
  35. Syntax
  36. Usage notes
  37. Examples
  38. Related functions

Функция Replace

Возвращает строку, которая является подстрокой строкового выражения, начинающегося с начальной позиции (по умолчанию — 1), в которой указанная подстрока была заменена другой подстрокой указанное количество раз.

Синтаксис

Replace(expression, find, replace, [ start, [ count, [ compare ]]])

Синтаксис функции Replace содержит следующие именованные аргументы:

Part Описание
выражение Обязательно. Строковое выражение, содержащее заменяемую подстроку.
Найти Обязательно. Искомая подстрока.
Заменить Обязательно. Подстрока замены.
start Необязательно. Начальная позиция для поиска и возврата подстроки выражения . Если элемент опущен, предполагается, что он равен 1.
count Необязательный параметр. Число выполняемых замен подстроки. Если этот параметр опущен, значение по умолчанию — -1, то есть выполните все возможные подстановки.
compare Необязательно. Числовое значение, указывающее тип сравнения, который будет использоваться при оценке подстрок. Значения см. в разделе «Параметры».

Параметры

Аргумент compare может принимать следующие значения:

Константа Значение Описание
vbUseCompareOption –1 Выполняет сравнение, используя параметр оператора Option Compare.
vbBinaryCompare 0 Выполняется двоичное сравнение.
vbTextCompare 1 Выполняется текстовое сравнение.
vbDatabaseCompare 2 Только Microsoft Access. Выполняется сравнение на основе сведений из базы данных.

Возвращаемые значения

Функция Replace возвращает следующие значения:

Если Функция «Replace» возвращает
Элемент expression имеет нулевую длину Пустая строка («»)
Элемент expression равен Null Ошибка.
Элемент find имеет нулевую длину Копия expression.
Элемент replace имеет нулевую длину Копия выражения с удаленными вхождениями поиска .
Начать>Len(выражение) Строка нулевой длины. Замена строк начинается с позиции, указанной в начале.
Элемент count равен 0 Копия expression.

Замечания

Возвращаемое значение функции Replace — это строка с подстановками, которая начинается с позиции, указанной start , и завершается в конце строки выражения . Это не копия исходной строки от начала до конца.

См. также

Поддержка и обратная связь

Есть вопросы или отзывы, касающиеся Office VBA или этой статьи? Руководство по другим способам получения поддержки и отправки отзывов см. в статье Поддержка Office VBA и обратная связь.

Источник

SUBSTITUTE function

This article describes the formula syntax and usage of the SUBSTITUTE function in Microsoft Excel.

Description

Substitutes new_text for old_text in a text string. Use SUBSTITUTE when you want to replace specific text in a text string; use REPLACE when you want to replace any text that occurs in a specific location in a text string.

Syntax

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

The SUBSTITUTE function syntax has the following arguments:

Text Required. The text or the reference to a cell containing text for which you want to substitute characters.

Old_text Required. The text you want to replace.

New_text Required. The text you want to replace old_text with.

Instance_num Optional. Specifies which occurrence of old_text you want to replace with new_text. If you specify instance_num, only that instance of old_text is replaced. Otherwise, every occurrence of old_text in text is changed to new_text.

Example

Copy the example data in the following table, and paste it in cell A1 of a new Excel worksheet. For formulas to show results, select them, press F2, and then press Enter. If you need to, you can adjust the column widths to see all the data.

=SUBSTITUTE(A2, «Sales», «Cost»)

Substitutes Cost for Sales (Cost Data)

=SUBSTITUTE(A3, «1», «2», 1)

Substitutes first instance of «1» with «2» (Quarter 2, 2008)

=SUBSTITUTE(A4, «1», «2», 3)

Substitutes third instance of «1» with «2» (Quarter 1, 2012)

Источник

SUBSTITUTE Function

Summary

The Excel SUBSTITUTE function replaces text in a given string by matching. For example =SUBSTITUTE(«952-455-7865″,»-«,»») returns «9524557865»; the dash is stripped. SUBSTITUTE is case-sensitive and does not support wildcards.

Purpose

Return value

Arguments

  • text — The text to change.
  • old_text — The text to replace.
  • new_text — The text to replace with.
  • instance — [optional] The instance to replace. If not supplied, all instances are replaced.

Syntax

Usage notes

The Excel SUBSTITUTE function can replace text by matching. Use the SUBSTITUTE function when you want to replace text based on matching, not position. Optionally, you can specify the instance of found text to replace (i.e. first instance, second instance, etc.).

SUBSTITUTE is case-sensitive. To replace one or more characters with nothing, enter an empty string («»).

Examples

Below are the formulas used in the example shown above:

The SUBSTITUTE function cannot replace more than one string at a time. However, SUBSTITUTE can be nested inside of itself to accomplish the same thing. For example, with the text «a (dog)» in cell A1, the formula below will strip parentheses () from text:

This same approach can be used in a more complex formula to normalize telephone numbers.

Use the REPLACE function to replace text at a known location in a text string. Use the SUBSTITUTE function to replace text by searching when the location is not known. Use FIND or SEARCH to determine the location of specific text.

Источник

How to Use Excel REPLACE Function (Examples + Video)

In this tutorial, I will show you how to use the REPLACE function in Excel (with examples).

Replace is a text function that allows you to quickly replace a string or a part of the string with some other text string.

This can be really useful when you’re working with a large dataset and you want to replace or remove a part of the string. But the real power of the replace function can be unleashed when you use it with other formulas in Excel (as we will in the examples covered later in this tutorial).

This Tutorial Covers:

Before I show you the examples of using the function, let me quickly cover the syntax of the REPLACE function.

Syntax of the REPLACE Function

Input Arguments

  • old_text – the text that you want to replace.
  • start_num – the starting position from where the search should begin.
  • num_chars – the number of characters to replace.
  • new_text – the new text that should replace the old_text.

Now let’s have a look at some examples to see how the REPLACE function can be used in Excel.

Example 1 – Replace Text with Blank

Suppose you have the following data set and you want to replace the text “ID-” and only want to keep the numeric part.

You can do this by using the following formula:

The above formula replaces the first three characters of the text in each cell with a blank.

Suppose you have a dataset as shown below and you want to remove the domain part (the one that follows the @ sign).

To do this, you can use the below formula:

The above function uses a combination of REPLACE, LEN and FIND function.

It first uses the FIND function to get the position of the @. This value is used as the Start Number argument and I want to remove the entire text string starting from the @ sign.

Another thing I need to remove this string is the total number of characters after the @ so that I can specify these many characters to be replaced with a blank. This is where I have used the formula combination of LEN and FIND.

Example 3: Replace One Text String with Another

In the above two examples, I showed you how to extract a part of the string by replacing the remaining with blank.

Here is an example where you change one text string with another.

Suppose you have the below dataset and you want to change the domain from example.net to example.com.

You can do this using the below formula:

Difference between Replace and Substitute functions

There is a major difference in the usage of the REPLACE function and the SUBSTITUTE function (although the result expected from these may be similar).

The REPLACE function requires the position from which it needs to start replacing the text. It then also requires the number of characters you need to replace with the new text. This makes REPLACE function suitable where you have a clear pattern in the data and want to replace text.

A good example of this could be when working with email ids or address or ids – where the construct of the text is consistent.

SUBSTITUTE function, on the other hand, is a little more versatile. You can use it to replace all the instances of an occurrence of a string with some other string.

For example, I can use it to replace all the occurrence of character Z with J in a text string. And at the same time, it also gives you the flexibility to only change a specific instance of the occurrence (for example, only substitute the first occurrence of the matching string or only the second occurrence).

Note: In many cases, you can do away with using the REPLACE function and instead use the FIND and REPLACE functionality. It will allow you to change the data set without using the formula and getting the result in another column/row. REPLACE function is more suited when you want to keep the original dataset and also want the resulting data to be dynamic (such that updates in case you change the original data).

Excel REPLACE Function – Video Tutorial

Related Excel Functions:

You may also like the following Excel Tutorials:

Источник

REPLACE Function

Summary

The Excel REPLACE function replaces characters specified by location in a given text string with another text string. For example =REPLACE(«XYZ123″,4,3,»456») returns «XYZ456».

Purpose

Return value

Arguments

  • old_text — The text to replace.
  • start_num — The starting location in the text to search.
  • num_chars — The number of characters to replace.
  • new_text — The text to replace old_text with.

Syntax

Usage notes

The REPLACE function replaces characters in a text string by position. The REPLACE function is useful when the location of the text to be replaced is known or can be easily determined.

REPLACE function takes four separate arguments. The first argument, old_text, is the text string to be processed. The second argument, start_num is the numeric position of the text to replace. The third argument, num_chars, is the number of characters that should be replaced. The last argument, new_text, is the text to use for the replacement.

Examples

To replace the «C» in the path below with a «D»:

To replace 3 characters starting at the 4th character:

You can use REPLACE to remove text by specifying an empty string («») for new_text. The formula below uses REPLACE to remove the first character from the string «XYZ»:

The example below removes the first 4 characters:

Use the REPLACE function to replace text at a known location in a text string. Use the SUBSTITUTE function to replace text by searching when the location is not known. Use FIND or SEARCH to determine the location of specific text.

Источник

This Tutorial demonstrates how to use the Excel SUBSTITUTE Function in Excel to find and replace text.

substitute Main Function

SUBSTITUTE Function Overview

The SUBSTITUTE Function Finds and replaces existing text with a new text string. Case-sensitive.

To use the SUBSTITUTE Excel Worksheet Function, select a cell and type:

substitute formula syntax

(Notice how the formula inputs appear)

SUBSTITUTE function Syntax and inputs:

=SUBSTITUTE(text,old_text,new_text,instance_num)

text – The original string of text.

old_text – The text string that you wish to find and replace.

new_test – The replacement text.

instance_num – OPTiONAL. The instance number to replace. If blank, all instances are replaced.

What is SUBSTITUTE?

The SUBSTITUTE function in Excel is used to substitute a string of characters in a text string with a different specified text string.

Let us look at the following example substituting the string “quick brown” with “slow white”.

=SUBSTITUTE(C2,C3,C4)

Substitute-EX-01

SUBSTITUTE looks in the string in C2, finds “quick brown” and replaces it with “slow white”.

How to use SUBSTITUTE

The SUBSTITUTE function takes 3 required arguments and 1 optional one:

  • Text: The text that you want to replace characters
  • Old_text: The text you want to replace
  • New_text: The text you want to replace Old_text with
  • Instance_num (optional): Specifies the occurrence of Old_text you want to replace with New_text. By default, all occurrences of Old_text will be replaced if an instance is not specified.

Nesting SUBSTITUTE

What if we had to substitute two parts of the same string? Let us see how we would combine two SUBSTITUTE functions.

=SUBSTITUTE(SUBSTITUTE(C2,C3,C4),C5,C6)

Substitute EX 02

Notice how the result of SUBSTITUTE(C2,C3,C4) is used as the first argument for the second SUBSTITUTE. If we break the formula apart:

=SUBSTITUTE(C2,C3,C4) evaluates to “The slow white fox jumps over the lazy dog”.

Then, SUBSTITUTE(“The slow white fox jumps over the lazy dog”, C5,C6) which evaluates to “The slow white lion jumps over the lazy dog”.

Difference between SUBSTITUTE and REPLACE

A similar function to SUBSTITUTE is REPLACE. In most cases, these can be used interchangeably however, the following rule of thumb applies.

The SUBSTITUTE function should be used when the string of text to be replaced is known, or a whole word or words are being replaced, like our fox example used earlier.

=SUBSTITUTE(C2,C3,C4)

Substitute-EX-01

The REPLACE function should be used when the position of the text characters to be replaced in the string are known, or when a part of a word or string is being replaced. For example, removing a hyphen from a string of numbers.

Substitute-EX-03

The REPLACE function can also be nested like SUBSTITUTE however, it is important to note that the relative position of the start character may change based on the length of the string to be replaced in the first step of the nested formula.

For example, replacing “quick brown” with “slow white” is replacing 11 characters with 10 characters. Therefore, if nested, the starting character for the second REPLACE would be 1 character off from the initial character positions.

SUBSTITUTE Tips

Notice the 3030 is incorrect as both “20” and “20” in “2020” were replaced with “30”.

Interested in More Text functions?

See our other articles for more on REPLACE, or how other text functions in Excel such as LEFT, MID and RIGHT are used.

SUBSTITUTE in Google Sheets

The SUBSTITUTE Function works exactly the same in Google Sheets as in Excel:

substitute Google Function

Additional Notes

The SUBSTITUTE Function is case sensitive!. You can use the LOWER or UPPER Functions to convert your strings of text into a consistant case before using SUBSTITUTE Function. Then, if desired, you can use the PROPER Function to capitalize the first letter of each word.

Alternatively, you can use the Functions SEARCH and REPLACE to simulate a non case-sensitive SUBSTITUTE. Use SEARCH Function to find the starting position of the text string. The SEARCH Function is not case-sensitive. Then use REPLACE Function to replace the text. You will need to use the result from the SEARCH Function as the start_num input in REPLACE. You will also need to define the number of characters to replace ( num_chars). You can manually count them or use the LEN Function to count the number of characters. Example:

Substitute – Remove Hyphens

A common question is how to remove hyphens from text. The trick is to Substitute the hyphen with nothing.

For this example, let’s assume cell A1 has text with hyphens in it. Here’s how to remove them

1. In a blank helper cell type =SUBSTITUTE(
2. Click on the cell you would like to substitute characters in or A1 (this automatically fills your formula)
3. Type, (comma)
4. Type “-”
5. Type, (comma)
6. Type “” and hit enter
Your end result in your helper cell should look like this:
=SUBSTITUTE(A1,”-“,””)

3 More Substitute Examples:

Looking at cell B3, we want to replace the word “sick” with “large”. We can use the following expression:

=SUBSTITUTE(B3,"sick","large",1)

If the text that we want to replace occurs more than once then we need to specify the instance that we mean. In the second example, we have two occurences of the word “old”. So if we want to change second instance to the word “grey” then we have to use:

=SUBSTITUTE(B4,"old","grey",2)

Note that if the occurrence is not specified then all instances are replaced. So:

=SUBSTITUTE(B4,"old","grey")

Would get rid of all instances of the word “old” and replace them with the word “grey”. Note that if the old text cannot be found then the string is unchanged. So looking at the last example:

=SUBSTITUTE(B5,"black","grey",1)

Means that we try and replace the word “black” with the word “grey”. However, as “black” does not occur the original string remained levitra shipped in the united states unchanged:

To download the .XLSX file from this article, click here

SUBSTITUTE Examples in VBA

You can also use the SUBSTITUTE function in VBA. Type:

application.worksheetfunction.substitute(text,old_text,new_text,instance_num)

For the function arguments (text, etc.), you can either enter them directly into the function, or define variables to use instead.

Vba SUBSTITUTE function

When the following code is executed

Range("A1") = WorksheetFunction.Substitute(Range("A1"), "using", "", 1)

The contents of cell A1 will become:

Vba SUBSTITUTE excel

So, the word “using” was replaced by an empty string. The last parameter (1) indicates which instance of the text should be replaced, in case the text is present more than one times. So, for example if we execute the following statement

Range("A1") = WorksheetFunction.Substitute(Range("A1"), "the", "an", 1)

The A1 cell will look like this

Excel SUBSTITUTE vba

Notice that the first occurrence of “the” was replaced by “an”, while the second “the” was not affected. If we omitted the last parameter, both “the” words would have been replaced

Return to the List of all Functions in Excel

Skip to content

How to Replace Text in Excel with the REPLACE function (2023)

How to Replace Text in Excel with the REPLACE function (2023)

Need to replace text in multiple cells?

Excel’s REPLACE and SUBSTITUTE functions make the process much easier.

Let’s take a look at how the two functions work, how they differ, and how you put them to use in a real spreadsheet🔍

If you want to follow along with what I show you, download my workbook here.

Replacing characters in text with the REPLACE function

The REPLACE function substitutes a text string with another text string.

Let’s say your boss tells you that the product IDs for a product line must be changed.

But only a part of the product ID should be changed – not all of it.

Here, the “29FA” part of all the product IDs needs to be changed to “39LU”.

To do that with the REPLACE function, we’ll walk through the syntax of the REPLACE function, which goes like this:

=REPLACE(old_text, start_num, num_chars, new_text)

Don’t worry, it’s not as daunting as it looks. It’s actually pretty straightforward👍

Step 1: Old text

The old text argument is a reference to the cell where you want to replace some text. Write:

=REPLACE(A2

Excel replace function - reference to old text argument of syntax

And put a comma to wrap up the first argument, and let’s move on to the next.

Step 2: Start num

The start_num argument determines where the REPLACE function should start replacing characters from.

In our case, the “29FA” part starts on the 3rd character in the text.

Excel replace function to replace text from a specified position

So, write:

=REPLACE(A2, 3,

Now, we’ve established where the REPLACE function should start to replace text.

Still with me? Then let’s dive into the next argument of the REPLACE syntax🤿

Step 3: Num chars

Also called the “number of characters” argument, this determines how many characters should be replaced with the new text.

Typically, this should be the length of the text you want to replace the old text with.

So, it ties together with the next argument.

If you want to replace “29FA” with “39LU”, then you’re replacing the next 4 characters.

Write:

=REPLACE(A2, 3, 4

Excel replace function - number of characters to replace

And wrap it up with a comma🎁

There are situations where this num_chars argument should be a different length than the new_text argument. I’ll tell you more about that later.

Kasper Langmann, Microsoft Office Specialist

Step 4: New text

The new_text argument is the replacement text for the old text.

So, simply write the new characters that should replace the old 4 characters:

=REPLACE(A2, 3, 4, “39LU”

Excel replace function - new text argument

Remember the double quotes when the replacement text is letters or a combination of numbers and letters.

Wrap up the formula with an end parenthesis and press Enter.

Now, the “29FA” part of the old product ID is replaced with “39LU”.

Excel replace function - visual example

PRO TIP: Length of num_chars vs new_text

If you need the replacement text to be shorter or longer than the text it’s replacing, you can have a different length in the 3rd and 4th argument of REPLACE. Let me give you a few formula examples of that:

If you wanted to replace the “29FA” with “39L” instead, you’d write:

=REPLACE(A2, 3, 4, “39L”)

But the length of the 4th argument would be shorter than the 4 characters defined in the 3rd argument.

On the other hand, if you wanted to replace “FA” with “LLUU”, you’d write this:

=REPLACE(A2, 5, 2, “LLUU”)

Formula example Different length in number of characters and new text

Replacing text strings with the SUBSTITUTE function

If the string you want to replace doesn’t always appear in the same place, you’re better off using the SUBSTITUTE function.

The syntax of SUBSTITUTE goes like this:

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

This is a little different from the last syntax of the REPLACE function, so be careful not to get them mixed up⚠️

Step 1: Text

The text argument is just a cell reference to the cell where you want to replace text.

Write:

=SUBSTITUTE(A2

Excel substitute function to replace text - text argument

And of course, put a comma to go to the next argument.

Step 2: Old text

The SUBSTITUTE function doesn’t replace characters from a fixed position in a cell.

Instead, it cleverly searches for a text string and begins replacing characters from there🔍

So, if you want to replace the “FA” part of the product ID with “LU”, write:

=SUBSTITUTE(A2, “FU”

Excel substitute function - old text argument

Step 3: New text

The new_text argument is what the old text should be replaced with.

For this example, that’s “LU”.

=SUBSTITUTE(A2, “FA”, “LU”

Excel replace text - old text and new text

The new text doesn’t have to be the same length as the old text.

Kasper Langmann, Microsoft Office Specialist

Step 4: Instance num

The optional instance num argument decides how many times the text should be replaced.

This is relevant if there is more than one instance of the old text.

The instance_num argument is optional. If you leave it blank, every instance of the old text is replaced by the new text😊

In the following formula example, the first 2 product IDs each have 2 instances of the “FA”. So, the instance num argument determines whether only the first instance of “FA” is replaced with “LU” or both instances of “FA” is replaced with “LU”.

As you can see from the picture below, write 1 if you want only the first instance of “FA” to be replaced.

=SUBSTITUTE(A2, “FA”, “LU”, 1)

Or don’t use the instance num argument if every instance of “FA” should be replaced:

=SUBSTITUTE(A2, “FA”, “LU”)

Excel substitute formula examples with instance number argument for multiple replacements

And that’s how to replace text dynamically based on the location of the text you want to replace.

The difference between REPLACE and SUBSTITUTE

There are a few subtle differences between these two “replace functions”.

Both of them replace one or more characters in a text string with another text string.

The difference lies in how the first string is identified.

REPLACE selects the first string based on the position. So you might replace four characters, starting with the sixth character in the string.

SUBSTITUTE selects based on whether the string matches a predefined search. You might tell Excel to replace any instance of “FA” with “LU” for example.

Other than that, the two functions are identical👬🏻

Replace text using Find and Replace

Another way to replace text is with the ‘Find and Replace’ feature of Excel.

It’s a way to substitute characters in the original cell instead of having to add additional columns with formulas.

1. Select all the cells that contain the text to replace.

2. From the ‘Home’ tab, click ‘ Find and Select’.

Find and Select Excel feature

3. From the Find and Replace dialog box (in the replace tab) write the text you want to replace, in the ‘Find what:’ field.

4. Still within the ‘Find and Replace’ dialog box, write the new text to replace the old text with in the ‘Replace with:’ field.

Replace text by using the find what and replace with fields from Find and Replace dialog box

5. When you click the ‘Replace all’ button, Excel replaces all instances of the old text with the new text, in the selected cells.

If you instead want to replace all instances of the text within the entire workbook, just select a single cell before opening the ‘Find and Replace’ dialog box.

(Instead of selecting multiple cells).

Kasper Langmann, Microsoft Office Specialist

That’s it – Now what?

With the REPLACE and SUBSTITUTE functions, you can replace very specific strings with other strings. You can use letters, numbers, or other characters.

In short, you can replace text with extreme accuracy. And that saves you a great deal of time when you need to make a lot of edits.

Additionally, you can use the Find and Replace tool, which is the most underrated feature of Excel.

But no one got a job offer just based on their skills to replace characters in a text in Microsoft Excel.

Luckily, there are other areas of Excel that are magnets for job offers🧲

Click here to learn IF, SUMIF, VLOOKUP, and pivot tables (yup, that’s the magnets) for FREE in my 30-minute online Excel course.

Other resources

Replacing text is often used to clean up data so it’s ready for analysis, formulas, pivot tables, etc.

Other ways of cleaning up data are with other important Excel functions like LEFT, RIGHT, MID, and LEN.

Or with one of the two ways of deleting blank rows (one better than the other). Read all about it here.

Kasper Langmann2023-01-19T12:24:59+00:00

Page load link

Like this post? Please share to your friends:
  • Replace special characters in word
  • Replacing a spaces in excel
  • Replace spaces in word
  • Replacement text in word
  • Replace sentence with one word