Excel VBA: How to Replace Text in String (5 Effective Ways)

Get FREE Advanced Excel Exercises with Solutions!

If you are looking for ways to replace text in a string using Excel VBA, then you will find this article useful. Replacing a certain text part may save a lot of time in typing the text strings again. So, let’s get into the main article to know the details about this replacement task.

Download Workbook


5 Ways to Replace Text in String Using Excel VBA

Here, we have the following dataset containing some records of the employees with their email ids. Our task is to replace the old domain names with the new ones. In the following methods, we will work with this dataset along with some random text strings to replace desired text with VBA codes.

Excel VBA replace text in string

We have used Microsoft Excel 365 version here, you can use any other versions according to your convenience.


Method-01: Replace Text Starting from n-th Position of a Random String

Here, we will replace text in a random text string for different starting positions.

Step-01:
➤ Go to the Developer Tab >> Code Group >> Visual Basic Option.

Excel VBA replace text in string

Then, the Visual Basic Editor will open up.
➤ Go to the Insert Tab >> Module Option.

starting from n-th position

After that, a Module will be created.

Excel VBA replace text in string

Step-02:
➤ Write the following code

Sub substitution_of_text_1()
Dim full_txt_str, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
updated_str = Replace(full_txt_str, "Cars", "Bicycles", 1)
MsgBox updated_str
End Sub

Here, we have declared full_txt_str and updated_str as String and then assigned full_txt_str to a random text string- “Hundred Cars Fifty Cars Ten Cars”. Then the VBA REPLACE function is used to replace the Cars part of this random string with Bicycles and 1 is used here to start the replacement from position 1 of this string. Finally, we have assigned this new text string to updated_str and with a message box (MsgBox) we will see the result.

starting from n-th position

➤ Press F5.
Then a message box will appear with the new text string with the replaced text Bicycles.

Excel VBA replace text in string

➤ To do the replacement process from the second instance of Cars use the following code.

Sub substitution_of_text_1()
Dim full_txt_str, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
updated_str = Replace(full_txt_str, "Cars", "Bicycles", 14)
MsgBox updated_str
End Sub

Here, we have used the starting position as 14 because we want to have the part of the string after Hundred Cars and replace the Cars here.

starting from n-th position

➤ After running the code, we will have the following message box with the text string starting from the text Fifty and with Bicycles in the position of Cars.

starting from n-th position

➤ For having only the last portion of this string we are applying the following code.

Sub substitution_of_text_1()
Dim full_txt_str, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
updated_str = Replace(full_txt_str, "Cars", "Bicycles", 25)
MsgBox updated_str
End Sub

Here, we have used the starting position as 25 because we want to have the part of the string after Fifty Cars and replace the Cars with Bicycles here.

starting from n-th position

Finally, we will have a message box with our desired part of the string having the replacement with Bicycles.

Excel VBA replace text in string

Read More: Excel VBA: Replace Character in String by Position (4 Effective Ways)


Method-02: Substitute Text for n-th Occurrence of a Random String Using Excel VBA

In this section, we will replace a text in a random string for different numbers of occurrences with the help of a VBA code.

Steps:
➤ Follow Step-01 of Method-1.
➤ Type the following code.

Sub substitution_of_text_2()
Dim full_txt_str, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
updated_str = Replace(full_txt_str, "Cars", "Bicycles", 1, 1)
MsgBox updated_str
End Sub

Here, we have declared full_txt_str and updated_str as String and then assigned full_txt_str to a random text string- “Hundred Cars Fifty Cars Ten Cars”. After that, the REPLACE function is used to replace the Cars part of this random string with Bicycles, 1 is used here to start the replacement from position 1 of this string, and the final 1 is for counting the number of occurrences. By using 1 as the counting number we are defining the replacement of the first Cars only. Finally, we have assigned this new text string to updated_str and with a message box (MsgBox) we will see the result.

n-th occurrence

➤ Press F5.
Afterward, a message box will appear with the new text Bicycles in the first position of Cars only.

Excel VBA replace text in string

➤ For replacing the first two instances of Cars with Bicycles use the following code.

Sub substitution_of_text_2()
Dim full_txt_str, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
updated_str = Replace(full_txt_str, "Cars", "Bicycles", 1, 2)
MsgBox updated_str
End Sub

Here, 2 is used as the counting number to replace the first two instances of Cars with Bicycles.

n-th occurrence

After running the code, you will have the replacement of the first two texts Cars with Bicycles.

n-th occurrence

➤ Apply the following code to replace all of the instances of the text Cars.

Sub substitution_of_text_2()
Dim full_txt_str, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
updated_str = Replace(full_txt_str, "Cars", "Bicycles", 1, 3)
MsgBox updated_str
End Sub

Here, the last argument of the REPLACE function is 3 which is the counting number indicating the replacement of all of the Cars with Bicycles in the text string.

n-th occurrence

➤ Press F5.
Afterward, we will have the following message box with the replaced text Bicycles in the string.

Excel VBA replace text in string


Similar Readings


Method-03: Replace Text in a Random String with InputBox

Here, we will replace a certain text of a random string with a text which will be defined by a user with the help of the VBA InputBox function.

Steps:
➤ Follow Step-01 of Method-1.
➤ Type the following code.

Sub substitution_of_text_3()
Dim full_txt_str, new_txt, updated_str As String
full_txt_str = "Hundred Cars Fifty Cars Ten Cars"
new_txt = InputBox("Write down the new text to replace")
updated_str = Replace(full_txt_str, "Cars", new_txt)
MsgBox updated_str
End Sub

Here, we have declared full_txt_str, new_txt, and updated_str as String and then assigned full_txt_str to a random text string- “Hundred Cars Fifty Cars Ten Cars”. To have the user-defined input as the text to be replaced with the Cars in the random string, we have used the InputBox function and then assigned this value to new_txt. Then the REPLACE function is used to replace the Cars part of this random string with new_txt. Finally, we have assigned this new text string to updated_str and with a message box (MsgBox) we will see the result.

substitute with input box

➤ Press F5.
After that, an Input Box will appear where you can enter any text part which you want to have in the new string.
➤ Type Bicycles or any other text you want and then press OK.

substitute with input box

Finally, you will have the following result with the new text string having new text Bicycles in the position of Cars.

Excel VBA replace text in string

Read More: How to Replace Text in Excel Formula (7 Easy Ways)


Method-04: Replace Text in a Range of Strings with Excel VBA

Here, we will substitute the gmail part of the email ids with the domains in the New Domain column, and to accumulate the new email ids we have inserted a new column; Final Email Id.

Excel VBA replace text in string

Steps:
➤ Follow Step-01 of Method-1.
➤ Type the following code.

Sub substitution_of_text_4()
For i = 4 To 13
If InStr(1, Cells(i, 4).Value, "gmail") > 0 Then
Cells(i, 6).Value = Replace(Cells(i, 4).Value, "gmail", Cells(i, 5).Value)
Else
Cells(i, 6).Value = ""
End If
Next i
End Sub

Here, we have used the FOR loop to execute the operation from Row 4 to Row 13. With the help of the IF-THEN statement, we have checked whether the email ids of Column D contain “gmail” or not, and for fulfilling this criterion the “gmail” portion of the email ids will be replaced with the new domains of Column E to create the new ids in Column F. Otherwise you will have a blank in the corresponding cells of Column F.

replace text in a range

➤ Press F5.
Then, you will have the new email ids in the Final Email Id column.

replace text in a range

Read More: Excel VBA to Find and Replace Text in a Column (2 Examples)


Method-05: Replace Text in a Range of Strings with User Input to Find Text

You can replace the following email ids with the new domains and declare what to replace in the previous ids a user input can be used by following this method.

Excel VBA replace text in string

Steps:
➤ Follow Step-01 of Method-1.
➤ Type the following code.

Sub substitution_of_text_5()
Dim partial_text As String
partial_text = Application.InputBox("Enter the string to be replaced")
For i = 4 To 13
If InStr(1, Cells(i, 4).Value, LCase(partial_text)) > 0 Then
Cells(i, 6).Value = Replace(Cells(i, 4).Value, LCase(partial_text), Cells(i, 5).Value)
Else
Cells(i, 6).Value = ""
End If
Next i
End Sub

Here, we have defined partial_text as a String and then assigned it to a string that will be given by a user through the Input Box.
Afterward, we used the FOR loop to execute the operation from Row 4 to Row 13, and using the IF-THEN statement, we checked whether the email ids of Column D contain “gmail” or not. And for fulfilling this criterion the “gmail” portion of the email ids will be replaced with the new domains of Column E to create the new ids in Column F. Otherwise you will have a blank in the corresponding cells of Column F.

range with input box

➤ Press F5.
After that, you will have an Input Box where you have to type the text which you want to search in the range of email ids (here we have entered gmail) and then press OK.

range with input box

Finally, we are having our updated email ids in the Final Email Id column.

range with input box

Read More: Find and Replace a Text in a Range with Excel VBA (Macro and UserForm)


Practice Section

For doing practice by yourself we have provided a Practice section like below in a sheet named Practice. Please do it by yourself.

Practice


Conclusion

In this article, we tried to cover the ways to replace text in a string using Excel VBA. Hope you will find it useful. If you have any suggestions or questions, feel free to share them in the comment section.


Related Articles

Tanjima Hossain

Tanjima Hossain

Hello everyone, This is Tanjima Hossain. I have completed my graduation from BUET. Then I have started working as a technical writer in SOFTEKO. I have grown interest in technical content writing, research topics, numerical analysis related field and so I am here. Besides this I love to interact with different people and I love to spend my spare time by reading, gardening ,cooking etc.

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo