VBA code to do find and replace on multiple values

benjie1982

New Member
Joined
Apr 12, 2017
Messages
9
Hi guys, need some help here. I have a list of values to find (column A) and replace withe new values Column B - over 400 rows. I need to run the find and replace over the whole workbook (or at least a whole sheet at a time)

I found alot of macros on the internet that seem to be very old and don't work.

This is one of the ones I found that looks like it will do but I keep getting a variable not defined error at 'Set TempArray'. My knowledge of code is very limited so I have no idea how to fix this. If someone could help me fix it or show me one of theirs that works, that'll be much appreciated


Code:
Sub Multi_FindReplace()

Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant


'Create variable to point to your table
  Set tbl = Worksheets("List").ListObjects("Table1")


'Create an Array out of the Table's Data
  Set TempArray = tbl.DataBodyRange
  myArray = Application.Transpose(TempArray)
  
'Designate Columns for Find/Replace data
  fndList = 1
  rplcList = 2


'Loop through each item in Array lists
  For X = LBound(myArray, 1) To UBound(myArray, 2)
    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
      For Each sht In ActiveWorkbook.Worksheets
        If sht.Name <> tbl.Parent.Name Then
          
          sht.Cells.Replace What:=myArray(fndList, X), Replacement:=myArray(rplcList, X), _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
        
        End If
      Next sht
  Next X


End Sub

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I haven't checked the code to see if it will do what you want, but to fix that message, add 2 more Dim lines with the others at the start of the code.
Code:
Dim TempArray As Range
Dim X As Long
 
Upvote 0
I haven't checked the code to see if it will do what you want, but to fix that message, add 2 more Dim lines with the others at the start of the code.
Code:
Dim TempArray As Range
Dim X As Long


Thanks. It got past that

I now get a subscript out of range error at this line:

Code:
Set tbl = Worksheets("List").ListObjects("Table1")

I assumed "List" is the sheet name where the find values are and "Table1" i have no idea what that refers to
 
Upvote 0

Forum statistics

Threads
1,215,829
Messages
6,127,130
Members
449,361
Latest member
VBquery757

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