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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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