Find and Replace Multiple values in all worksheets

Average Joe

New Member
Joined
Jul 20, 2012
Messages
15
Hi,
Does anybody have any suggestions / alternatives to modify the below code to allow find and replace multiple values on all worksheets?
Also, if there is a way to fix replacing range, rather than having to select it each time?

Thanks

Code:
Sub MultiFindNReplaceNew()
Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range
xTitleId = "Test"
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
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What is the range of your replacement values?
Will your InputRng vary?, or can that be set?
To replace over all worksheets, will the input & replace ranges be the same on every sheet?
 
Upvote 0
I would like to have a separate worksheet with the InputRng fixed in columns A & B.
The range of the replacement values would then be the full range of all the remaining worksheets, if possible.

If I didn't do it this way, I think the macro would overwrite the values in the InputRng aswell?

Thanks for your help.
 
Upvote 0
Untested but try
Code:
Sub ReplaceInAllShts()
'Average Joe

    Dim Cnt As Long
    Dim Replc As Variant
    Dim Sht As Long
  
    With Sheets(1)
        Replc = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
    End With
    
    For Sht = 2 To Worksheets.Count
        For Cnt = 1 To UBound(Replc)
            Sheets(Sht).UsedRange.Replace what:=Replc(Cnt, 1), Replacement:=Replc(Cnt, 2), _
                LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
                SearchFormat:=False, ReplaceFormat:=False
        Next Cnt
    Next Sht
    
End Sub
This is based on your first sheet (ie Sheets(1)) containing your search & replace data.
 
Upvote 0
What line of code did it fail on?
 
Upvote 0
Looks like it failed on Line 14, but the whole section below was highlighted:
Code:
            Sheets(Sht).UsedRange.Replace what:=Replc(Cnt, 1), Replacement:=Replc(Cnt, 2), _
                LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
                SearchFormat:=False, ReplaceFormat:=False
 
Upvote 0
Try this
Code:
Sub ReplaceInAllShts()
'Average Joe

    Dim Cnt As Long
    Dim Replc As Variant
    Dim Sht As Long
  
    With Sheets(1)
        Replc = .Range("A1", .Range("B" & Rows.Count).End(xlUp)).Value
    End With
    
    For Sht = 2 To Worksheets.Count
        For Cnt = 1 To UBound(Replc)
            Sheets(Sht).UsedRange.Replace what:=Replc(Cnt, 1), Replacement:=Replc(Cnt, 2), _
                LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
                SearchFormat:=False, ReplaceFormat:=False
        Next Cnt
    Next Sht
    
End Sub
 
Upvote 0
This is working, but only when the replacing value is the exactly the same as the cell contents - it will not change part of a cell or file path.
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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