VBA to replace with multiple conditions

nicolas877

New Member
Joined
Jan 15, 2022
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi forum, i need a vba code to replace text following multuple criteria in a sheet like this
Before
Date Name
Week 1 Tom
Week 1 Tom
Week 2 kate
Week 4 Tom

To
Date Name
Week 1 Tom Hanks
Week 1 Tom Hanks
Week 2 kate Smith
Week 4 Tom Miller

Using this key
Week 1 Tom Hanks
Week 2 Kate Smith
Week 4 Tom Miller

The real sheet has like 15000 diferents rows and multiple names/dates, i tried using this code but only works with one condition (like replace Tom with Tom Hanks, but i also the week criteria)

VBA Code:
Sub MultiFindNReplace() 
Dim Rng As Range 
Dim InputRng As Range, ReplaceRng As Range 
xTitleId = "KutoolsforExcel" 
Set InputRng = Application.Selection
 Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8) 
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8) 
Application.ScreenUpdating = False 
For Each Rng In ReplaceRng.Columns(1).Cells    
InputRng.Replace what:=Rng.Value, replacement:=Rng.Offset(0, 1).Value, Lookat:=xlWhole 
Next
Application.ScreenUpdating = True 
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
One way.

VBA Code:
Sub MultiFindNReplace()
    Dim SearchArray As Variant, ReplaceArray As Variant, SearchTerm As String, ReplaceTerm As String, I As Long

    SearchArray = Array("Week 1 Tom", "Week 2 kate", "Week 4 Tom")
    ReplaceArray = Array("Week 1 Tom Hanks", "Week 2 Kate Smith", "Week 4 Tom Miller")

    Application.ScreenUpdating = False
    With ActiveSheet.UsedRange
        For I = LBound(SearchArray) To UBound(SearchArray)
            SearchTerm = SearchArray(I)
            ReplaceTerm = ReplaceArray(I)
            .Replace What:=SearchTerm, Replacement:=ReplaceTerm, Lookat:=xlWhole
        Next I
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Assuming that what you posted above about what the data looks like, and where it is stored means this:
Book1
AB
1DateName
2Week 1Tom
3Week 1Tom
4Week 2kate
5Week 4Tom
Sheet3

Then here is one way:
VBA Code:
Sub SearchReplace()
    Dim WS As Worksheet
    Dim CellRange As Range
    Dim R As Range
    Dim SearchTerm As String

    Set WS = ActiveSheet

    With WS
        Set CellRange = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)    'last cell in column w/data
    End With

    For Each R In CellRange
        SearchTerm = Trim(UCase(R.Value)) & " " & Trim(UCase(R.Offset(0, 1).Value))
        Select Case SearchTerm
        Case "WEEK 1 TOM"
            R.Offset(0, 1).Value = "Tom Hanks"
        Case "WEEK 2 KATE"
            R.Offset(0, 1).Value = "Kate Smith"
        Case "WEEK 4 TOM"
            R.Offset(0, 1).Value = "Tom Miller"
        End Select
    Next R
End Sub

It will produce this result:
Book1
AB
1DateName
2Week 1Tom Hanks
3Week 1Tom Hanks
4Week 2Kate Smith
5Week 4Tom Miller
Sheet3
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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