# Substitute multiple text strings

The SUBSTITUTE and REPLACE functions can only handle one string, this article demonstrates two ways to handle more than one pair of values.

- An Excel 365 recursive LAMBDA function.
- A User-Defined Function (UDF) allows you to substitute multiple text strings with new text strings.

#### Table of Contents

## 1. Substitute multiple text strings - Excel 365 recursive LAMBDA function

This example demonstrates a formula that iterates through all values in cell range E3:F4 in order to substitute specific values in cell B3 with new values.

The SUBSTITUTE function allows you to substitute one value with another value, however, this formula lets you substitute (almost) any number of values.

Excel 365 LAMBDA function in Name Manager:

The LAMBDA function is named SUBSTR in the Name Manager.

Excel 365 formula in cell C3:

### 1.1 Explaining LAMBDA formula

#### Step 1 - Get old string

The INDEX function returns a value or reference from a cell range or array, you specify which value based on a row and column number.

Function syntax: INDEX(array, [row_num], [column_num])

INDEX(sub,n,1)

sub is a variable, in this example cell reference $E$3:$F$4

n is a number, representing the number of rows in cell reference $E$3:$F$4

1 is the first column in cell reference $E$3:$F$4

#### Step 2 - Get new string

The INDEX function returns a value or reference from a cell range or array, you specify which value based on a row and column number.

Function syntax: INDEX(array, [row_num], [column_num])

INDEX(sub,n,2)

sub is a variable, in this example cell reference $E$3:$F$4

n is a number, representing the number of rows in cell reference $E$3:$F$4

2 represents the second column in cell reference $E$3:$F$4

#### Step 3 - Substitute given values

The SUBSTITUTE function replaces a specific text string in a value. Case sensitive.

Function syntax: SUBSTITUTE(text, old_text, new_text, [instance_num])

SUBSTITUTE(str,INDEX(sub,n,1),INDEX(sub,n,2))

#### Step 4 - Make this formula recursive

The LAMBDA function is named SUBSTR in the name manager. We can call this named formula again until all values have been used.

SUBSTR(str,sub,n)

The SUBSTR function has three arguments:

- str - the string
- sub - the values to substitute
- n - the number of rows (or substitute pairs in cell ref $E$3:$F$4

SUBSTR(SUBSTITUTE(str,INDEX(sub,n,1),INDEX(sub,n,2)),sub,n-1)

str => SUBSTITUTE(str,INDEX(sub,n,1),INDEX(sub,n,2))

sub => sub (no change)

n => n - 1

n is keeping track of the row number. By subtracting one for each iteration the changes and a new pair of substitute values are processed.

#### Step 5 - Control the recursive formula

The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.

Function syntax: IF(logical_test, [value_if_true], [value_if_false])

The IF function returns the string str if n is equal to 0 (zero), however, it calls the SUBSTR function again if not. This step is repeated like a loop until n is equal to 0 (zero).

IF(n=0,str,SUBSTR(SUBSTITUTE(str,INDEX(sub,n,1),INDEX(sub,n,2)),sub,n-1))

#### Step 6 - Create the LAMBDA function and define the parameters

The LAMBDA function build custom functions without VBA, macros or javascript.

Function syntax: LAMBDA([parameter1, parameter2, â€¦,] calculation)

LAMBDA(str,sub,n,IF(n=0,str,SUBSTR(SUBSTITUTE(str,INDEX(sub,n,1),INDEX(sub,n,2)),sub,n-1)))

### 1.2 Name LAMBDA function in the Name Manager

- Go to tab "Formulas" on the ribbon.
- Press the left mouse button on the "Name Manager" button.

A dialog box opens. - Press with left mouse button on the "New..." button. A new dialog box appears.
- Name the function.

- Paste the LAMBDA formula in the "Refers to:" field.
- Press with left mouse button on OK button.

You are now ready to use the named LAMBDA function. Select an empty cell. Type =SUBSTR( and the arguments, don't forget the ending parentheses. Press Enter.

## 2. How to use the User Defined Function

You may have as many strings as you like, there is really no limit. The image above shows the UDF in cell C3 using strings from E3:E4 and F3:F4.

Formula in cell C3:

The UDF will not appear and work yet until you have copied the code below to a module in your workbook. There are instructions below.

Got it working? Now copy cell C3 and paste to cells below, the first argument contains relative cell references meaning they will change automatically when you copy and paste cell C3 to cells below.

The second and third argument are absolute cell references, they contain dollar signs meaning they are locked to cell range $D$2:$D$3 and $E$2:$E$3. These cell references will not change when you copy cell C3 and paste to the cells below.

### 2.1 User Defined Syntax

SubstituteMultiple(*text As String*, *old_text As Range*, *new_text As Range*)

### 2.2 Arguments

text |
Required. A cell reference to a cell containing the text you want to manipulate. |

old_text |
Required. A cell reference to one or many cells containing strings you want to replace. |

new_text |
Required. A cell reference to one or many cells containing strings you want instead. |

### 2.3 VBA code

'Name function and dimension argument variables and declare their data types Function SubstituteMultiple(text As String, old_text As Range, new_text As Range) 'Dimension variable and declare data type Dim i As Single 'Iterate through cells in argument old_text For i = 1 To old_text.Cells.Count 'Replace strings in value based on variable i Result = Replace(LCase(text), LCase(old_text.Cells(i)), LCase(new_text.Cells(i))) 'Save manipulated value to variable text text = Result Next i 'Return value stored in variable Result to worksheet SubstituteMultiple = Result End Function

### 2.4 Where to put the code?

- Copy above VBA code.
- Go to the Visual Basic Editor (Shortcut keys Alt + F11).
- Press with left mouse button on Insert on the top menu.
- Press with left mouse button on Module to create a module in your workbook. A module named module1 appears in the Project Explorer.
- Paste code to module.
- Exit VB Editor

### 2.5 How to save a macro-enabled workbook

### User defined function category

This article demonstrates two ways to calculate the number of times each word appears in a given range of cells. […]

This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]

This article demonstrates a user definedÂ function that lists files in a ggiven folder and subfolders. A user defined function is […]

### Excel categories

### 9 Responses to “Substitute multiple text strings”

### Leave a Reply

### 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

Do you have a version that is case sensitive?

Ari,

Great solution, thanks!

this is best solution from whole google search results , Thanks very much.

but there is little difference within Substitute and SubstituteMultiple:

Substitude Multiple Changes Texts UperCase symbols To LowCase, can someone help how to fix this?

Zurab Lomidze,

Thank you. Try this udf, it is case sensitive.

You saved my day, Thanks

omgness, thank you so much

Great post, Oscar; I use this function all the time. A problem I need help with is that the replacement keeps looping through my replaced text. For example, I have a "Remarks" column with a cell containing "A,B". Each character represents an index item from a multi-column table elsewhere.

A = ALMOND BERRY

B = BLUE COCONUT

Here is the formula I'm running:

=SubstituteMultiple("A,B",{"A";"B"},{"ALMOND BERRY";"BLUE COCONUT"})

I want to produce the following result:

"ALMOND BERRY,BLUE COCONUT"

However, the following results from the SubstituteMultiple UDF:

"ALMOND BLUE COCONUTERRY,BLUE COCONUT"

The problem is that the index character is an alphabetical character that appears in the second of the replacement phrase, thus causing a replacement loop on the second term in the "new_text".

What are your thoughts? Thanks for your effort.

This is great. I don't know why excel doesn't have inbuilt functionality for multiple substitutes (in the vein of IF vs IFS, or the above)

Is anyone able to help modify the above so it can handle an array as the Text argument? I would like the function to remove all instances of Old_text from an entire column.

Thanks in advance!