How to Use the Substitute Function in Excel VBA: 3 Methods

Method 1 – Removing the First Character(s) from a String with the Substitute Function in Excel VBA

Below is a dataset with the names and IDs of employees.

Data Set to Use the Substitute Function in Excel VBA

Steps:

  • Develop a VBA code using the Substitute function to remove the first two characters (SR) from all the IDs.
  • Find out the first 2 characters from the original string. We’ll execute this using the Left function of VBA.
First_Two_Characters = Left(Original_String, 2)
  • Use the Substitute function to replace the first 2 characters with an empty string (“”).
Output_String = Application.WorksheetFunction.Substitute(Original_String, First_Two_Characters, "", 1)
  • The complete VBA code will be:

⧭ VBA Code:

Sub Remove_First_Characters_with_Substitute()

For i = 1 To Selection.Rows.Count

    For j = 1 To Selection.Columns.Count

        Original_String = Selection.Cells(i, j)

        First_Two_Characters = Left(Original_String, 2)

        Output_String = Application.WorksheetFunction.Substitute(Original_String, First_Two_Characters, "", 1)

        Selection.Cells(i, j) = Output_String

    Next j

Next i

End Sub

  • Select the employee IDs and run this Macro (Remove_First_Characters_with_Substitute).

Running Macro with the Substitute Function in Excel VBA

  • Remove the first 2 characters from all the IDs.

First Two Characters Removed Using the Substitute Function in Excel VBA

 


Method 2 – Replacing the First Character of a String with the Uppercase Letter with the Substitute Function in Excel VBA

There’s another column added to the data set containing the employees’ home country, but all the letters of the country names have been made with lower cases.

Steps:

  • Develop a VBA code with the Substitute function to replace the first letters of the country names with upper cases.
  • Extract the first letter of the country names using the Left function of VBA.
First_Letter = Left(Country_Name, 1)
  • Bring out the upper case version of the first letter using the UCase function of VBA.
UpperCase_Letter = UCase(First_Letter)
  • Use the Substitute function to replace the first letter of the country name with the uppercase letter.
Output = Application.WorksheetFunction.Substitute(Country_Name, First_Letter, UpperCase_Letter, 1)
  • The complete VBA code will be:

⧭ VBA Code:

Sub Replace_First_Letter_with_Uppercase()

For i = 1 To Selection.Rows.Count

    For j = 1 To Selection.Columns.Count

        Country_Name = Selection.Cells(i, j)

        First_Letter = Left(Country_Name, 1)

        UpperCase_Letter = UCase(First_Letter)

        Output = Application.WorksheetFunction.Substitute(Country_Name, First_Letter, UpperCase_Letter, 1)

        Selection.Cells(i, j) = Output

    Next j

Next i

End Sub

Code with the Substitute Function in Excel VBA

  • Select the home countries and run this Macro (Replace_First_Letter_with_Uppercase).

Running Macro with the Substitute Function in Excel VBA

  • Get the first letters of the country names converted to upper case letters.


Method 3 – Replacing a Specific Text with Another Text within a String with the Substitute Function in Excel VBA

The below dataset has the Email Addresses of the employees in a new column.

Data Set to Use the Substitute Function in Excel VBA

Steps:

  • Develop a VBA code using the Substitute function to convert the Gmail addresses into Outlook addresses.
  • Use the Substitute function to replace the string “gmail” of each Email address with the string “outlook”.
New_Email = Application.WorksheetFunction.Substitute(Old_Email, "gmail", "outlook")
  • The complete VBA code will be:

⧭ VBA Code:

Sub Replace_Gmail_with_Outlook()

For i = 1 To Selection.Rows.Count

    For j = 1 To Selection.Columns.Count

        Old_Email = Selection.Cells(i, j)

        New_Email = Application.WorksheetFunction.Substitute(Old_Email, "gmail", "outlook")

        Selection.Cells(i, j) = New_Email

    Next j

Next i

End Sub

Code with the Substitute Function in Excel VBA

  • Select the Email Addresses and run this Macro (Replace_Gmail_with_Outlook).

  • Convert the Gmail addresses into Outlook addresses.

 


Download the Practice Workbook

Download this workbook to practice.


Further Readings

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

2 Comments
  1. Hi, is there a formula where I can use the substitute function like in the 3rd option but assuming that more people are going to type their email with @gmail.com so whenever they type that in the column it automatically changes only the gmail.com to outlook.com maintaining their first string which we don’t know yet

    • Hi Raymond,
      Thanks for your question. I think you can do your task easily by following the code below.
      • Right-click on the sheet name containing your dataset and then select the View Code option.
      1
      • Type the following code in the opened window and make sure to adjust the number of Target.Column = 5 according to the column number of the emails.

      Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Column = 5 Then
      New_Email = Application.WorksheetFunction.Substitute(Target.Value, "gmail", "outlook")
      Target.Value = New_Email
      End If
      End Sub

      2
      • After saving the code, return to your worksheet.
      • Type a random email with @gmail.com
      3
      • Press ENTER.
      In this way, the email will be automatically changed from gmail to outlook.
      4

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo