Excel VBA Replace String
Excel VBA Replace looks for a substring within a list and replaces the substring with a second substring. When we use Excel VBA Replace Strings in Data there are some situations when we need to replace some character or a string from a sentence. If we have a large set of data that we want to replace it can be a tedious task to do it manually but with this function, it is very easy to provide the arguments and replace the string.
Syntax:
The syntax for Replace String function:
Replace ( Expression as String, Old String, New String, Start, Count, Compare)
Now the expression is the main string from where we want to remove the old string and new string which will replace the old string. The other arguments are optional. If we compare this function to excel worksheet substitute function we will find that Replace in VBA is the replacement of the substitute function in excel.
How to Replace Text in String Using VBA?
We will learn how to Replace Text in String using the VBA Code in Excel.
Example #1
Let us start with a basic example of using the replace function where we will replace a simple word name from the sentence “My name is Anand”. Here we will replace Anand to Aran. For this, follow the below steps:
Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.
Step 2: Insert a new subprocedure for example 1.
Code:
Sub Example1() End Sub
Step 3: Declare a variable as a string.
Code:
Sub Example1() Dim Str As String End Sub
Step 4: Now let us use replace function while providing a string as the expression to the variable.
Code:
Sub Example1() Dim Str As String Str = Replace("My name is Anand", "Anand", "Aran") End Sub
Step 5: Let us use the Msgbox function to display the value we have in this variable.
Code:
Sub Example1() Dim Str As String Str = Replace("My name is Anand", "Anand", "Aran") MsgBox Str End Sub
Step 6: When we execute the code.
Example #2
Let us begin with the second example where we can use the arguments used in the replace function as the variables. We will use a different statement for this example. For this, follow the below steps:
Step 1: In the same module let us start another subprocedure.
Code:
Sub Example2() End Sub
Step 2: Now let us declare three different variables as a string.
Code:
Sub Example2() Dim Str, Str1, Str2 As String End Sub
Step 3: In the first variable we can provide any string statement.
Code:
Sub Example2() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" End Sub
Step 4: Now in the second variable we can use the Input Box function to get input from the user.
Code:
Sub Example2() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" Str1 = InputBox("Enter a String") End Sub
Step 5: Now in the third variable we can use the replace function to replace the second string to the desired string.
Code:
Sub Example2() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" Str1 = InputBox("Enter a String") Str2 = Replace(Str, "Boy", Str1) End Sub
Step 6: We will use the Msgbox function to display the result in the third string.
Code:
Sub Example2() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" Str1 = InputBox("Enter a String") Str2 = Replace(Str, "Boy", Str1) MsgBox Str2 End Sub
Step 7: When we execute the code above and give input as “girl”.
Example #3
Now we will discuss one of the optional arguments in the function which is the start option where we can define from which position we want. For this, follow the below steps:
Step 1: In the same module let us start another subprocedure.
Code:
Sub Example3() End Sub
Step 2: Now let us declare three different variables as a string.
Code:
Sub Example3() Dim Str, Str1, Str2 As String End Sub
Step 3: In the first variable we can provide any string statement.
Code:
Sub Example3() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" End Sub
Step 4: Now in the second variable we can use the Input Box function to get input from the user.
Code:
Sub Example3() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" Str1 = InputBox("Enter a String") End Sub
Step 5: Now in the third variable we can use the replace function to replace the second string to the desired string and give start position as 7.
Code:
Sub Example3() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" Str1 = InputBox("Enter a String") Str2 = Replace(Str, "Boy", Str1, Start:=7) End Sub
Step 6: We can use the Msgbox function to show the result and when we execute the code which will ask us for a string.
Code:
Sub Example3() Dim Str, Str1, Str2 As String Str = " I am a Good Boy" Str1 = InputBox("Enter a String") Str2 = Replace(Str, "Boy", Str1, Start:=7) MsgBox Str2 End Sub
Step 7: When we press Ok we will see the final result.
Example #4
Now there is another optional argument which is the number of times we want to replace a string. For this, follow the below steps:
Step 1: In the same module let us start another subprocedure.
Code:
Sub Example4() End Sub
Step 2: Now let us declare three different variables as a string.
Code:
Sub Example4() Dim Str, Str1, Str2 As String End Sub
Step 3: In the first variable we can provide any string statement.
Code:
Sub Example4() Dim Str, Str1, Str2 As String Str = " I am a Good Boy, but you are a bad boy" End Sub
Step 4: Now in the second variable we can use the Input Box function to get an Input from the user.
Code:
Sub Example4() Dim Str, Str1, Str2 As String Str = " I am a Good Boy, but you are a bad boy" Str1 = InputBox("Enter a String") End Sub
Step 5: Now in the third variable we can use the replace function to replace the second string to the desired string as shown in the below screenshot and use the number of instances we want to replace the string as follows and then use msgbox function to display the result.
Code:
Sub Example4() Dim Str, Str1, Str2 As String Str = " I am a Good Boy, but you are a bad boy" Str1 = InputBox("Enter a String") Str2 = Replace(Str, "Boy", Str1, Count:=1) End Sub
Step 6: Use the number of instances we want to replace the string as follows and then use the Msgbox function to display the result.
Code:
Sub Example4() Dim Str, Str1, Str2 As String Str = " I am a Good Boy, but you are a bad boy" Str1 = InputBox("Enter a String") Str2 = Replace(Str, "Boy", Str1, Count:=1) MsgBox Str2 End Sub
Step 7: Once we execute the code and provide a second string as “girl” we will see the following result.
Explanation of VBA Replace String:
We have seen from the above examples that the Replace function is the replacement of the substitute function of excel. It has three mandatory arguments and other arguments are optional. We can use the optional arguments together or one at a time. Start refers to the position of the string whereas count refers to the number of times we want to replace the old string with the new string.
How to Use VBA Replace String?
The method to use this function is very simple and similar to excel worksheet function. This function needs three mandatory arguments:
- First is the expression or the main string.
- Then we have to provide a target string which needs to replaced.
- And the final argument is the string which is the new string that needs to be replaced.
Things to Remember
There are few things which we need to remember about Replace string in VBA such as follows:
- There are three arguments in the Replace function of excel and there are two optional arguments.
- The two optional arguments are either for the number of times we want to replace or the position where we want to replace.
- This function is similar to a substitute function in excel.
Recommended Articles
This is a guide to the VBA Replace String. Here we discuss How to Replace Text in String in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –