Find and Replace VBA from a list in a 2nd sheet

Nineball

New Member
Joined
Jan 1, 2022
Messages
36
Office Version
  1. 2010
Platform
  1. Windows
I have looked through a lot of threads but can't find (or understand) what I'm looking for. I have a sheet that has a column of text sentences. I want to find specific text in each row and replace the text with another value from a 2nd worksheet. In the 2nd worksheet, column a has the text to find and column b has the replacement text which in most if not all cases a null value. I have hundreds of replacement rows so an array isn't practical.

I have uploaded an example of what I'm trying to do. The example1 sheet has a column of text I want to search through. When it finds a value from the lookup sheet in column a, I want it to replace the text with the value from the lookup sheet in column b. I hope this is clear.
thanks
 

Attachments

  • Example1.png
    Example1.png
    17.7 KB · Views: 6
  • Example2.png
    Example2.png
    12.7 KB · Views: 6

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
The following macro assume that the workbook running the code contains both worksheets...

VBA Code:
Sub FindAndReplace()

    Dim sentencesWorksheet As Worksheet
    Dim searchRange As Range
    Dim replaceTable As Variant
    Dim findWhat As String
    Dim replaceWith As String
    Dim i As Long
    
    Set sentencesWorksheet = ThisWorkbook.Worksheets("Sentences")
    
    With sentencesWorksheet
        Set searchRange = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
    End With
        
    With ThisWorkbook.Worksheets("F and R")
        replaceTable = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
    
    For i = 1 To UBound(replaceTable)
        findWhat = replaceTable(i, 1)
        replaceWith = replaceTable(i, 2)
        searchRange.Replace _
            what:=findWhat, _
            replacement:=replaceWith, _
            lookat:=xlPart, _
            MatchCase:=False
    Next i
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Thank you for the quick response. You've saved me a lot of work. Much appreciated
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,915
Members
449,478
Latest member
Davenil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top