How to Find and Replace from List with Macro in Excel (5 Examples)

Example 1 – Find and Replace a Word from a List with Excel Macro

In the following dataset, we want to find the product Cable and replace it with TV.

Find and Replace a Word from List with Excel Macro

STEPS:

  • Right-click on the sheet name at the bottom.
  • Select View Code.

Find and Replace a Word from List with Excel Macro

  • The VBA window will pop out with an empty code box.
  • Copy the following code and paste it into the box.
Sub Replace_Word()
Range("B3:D8").Replace What:="Cable", Replacement:="TV"
End Sub

Find and Replace a Word from List with Excel Macro

  • Press F5. The Macros dialog box will pop out.
  • Select Replace_Word and press Run.

  • Close the VBA window and you’ll see the product TV in the places of Cable.

Read More: How to Find and Replace Using VBA


Example 2 – Apply Macro for Case-Sensitive Replacement from a List in Excel

The following dataset contains product Tv and we want it to replace it with TV.

Apply Macro for Case Sensitive Replacement from List in Excel

STEPS:

  • Right-click on the sheet name.
  • Select View Code.
  • Copy the given code and paste it into the code box that opened.
Sub Replace_CaseSensitive()
Range("B3:D8").Replace What:="Tv", Replacement:="TV", MatchCase:=True
End Sub

Apply Macro for Case Sensitive Replacement from List in Excel

  • Press F5. The Macros dialog box will pop out.
  • Select Replace_CaseSensitive in the Macro Name and press Run.

  • Close the VBA window and you’ll see the desired changes.


Example 3 – Use the Macro Replace Function for Finding and Replacing Text within a Text String

We want to replace Text present in a Text String. There are 3 text strings. We want to insert a space between the text MyMicrosoft.

Use Macro Replace Function for Finding and Replacing Text within a Text String

STEPS:

  • Right-click on the sheet name.
  • Select View Code.
  • Copy and paste the code given below in the code box.
Sub TextString_Replace()
UsedRange.Replace What:="MyMicrosoft", Replacement:="My Microsoft"
End Sub

Use Macro Replace Function for Finding and Replacing Text within a Text String

  • Press F5.
  • Close the VBA window.


Example 4 – Find and Replace Multiple Values from a List with a Macro in Excel

The below dataset has a column Product and we want to replace the values in it with the column Replace with.

Find and Replace Multiple Values from List with Macro in Excel

STEPS:

  • Go to View Code.
  • The VBA window will pop out and a code box will appear.
  • Copy the below code and paste it into the box.
Sub Replace_MultiValues()
Dim R As Range
Dim InputR As Range, ReplaceR As Range
xTitleId = "Choose Range"
Set InputR = Application.Selection
Set InputR = Application.InputBox("Old ", xTitleId, InputR.Address, Type:=8)
Set ReplaceR = Application.InputBox("New :", xTitleId, Type:=8)
Application.ScreenUpdating = False
For Each R In ReplaceR.Columns(1).Cells
    InputR.Replace What:=R.Value, Replacement:=R.Offset(0, 1).Value
Next
Application.ScreenUpdating = True
End Sub

Find and Replace Multiple Values from List with Macro in Excel

  • After that, close the VBA window.
  • Now, select Macros from the Developer tab.

Find and Replace Multiple Values from List with Macro in Excel

  • The Macros dialog box will appear.
  • Select Replace_MultiValues in the Macro name and press Run.

Find and Replace Multiple Values from List with Macro in Excel

  • The Choose Range dialog box will appear.
  • Select $A$2:$A$6 in the Old box and press OK.

  • Another dialog box will appear. Select $D$2:$E$6 in the New field and press OK.

  • We’ll get the Product column according to our requirements.


Example 5 – Find and Replace Throughout the Entire Workbook in Excel with Macro

We have 2 sheets and we want to replace the product Cable present in both the sheets with product TV.

Find and Replace All Throughout Entire Workbook in Excel with Macro

STEPS:

  • Right-click on a sheet name.
  • Select View Code.
  • Copy the following code and paste it in the code box.
Sub FnD_All()
Dim sheet As Worksheet
Dim f As Variant
Dim r As Variant
f = "Cable"
r = "TV"
For Each sheet In ActiveWorkbook.Worksheets
  sheet.Cells.Replace what:=f, Replacement:=r, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
Next sheet
End Sub

  • Press F5 and close the VBA window.
  • The product Cable has been replaced by the product TV in all the worksheets.


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Aung Shine
Aung Shine

Aung Shine completed his bachelor’s in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. It has been almost 2 years since he joined SOFTEKO and actively working on the ExcelDemy project. Currently he works as a Team Leader where he guides his team members to create technical content. He has published 150+ articles and reviewed 50+ articles. He has also solved various user problems before. He has interests in Data Analysis, Power Query, Advanced Excel,... Read Full Bio

2 Comments
  1. This is helpful, but missing the last example, which would naturally be using VBA to Find and Replace Multiple Values from List All Throughout Entire Workbook in Excel.

    • Thank you Michael for your comment. We made adjustments in the Excel file. Please find the added VBA code for the last example you mentioned.
      Regards,
      Aung

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo