I have found this code on the net:
Sub Multi_FindReplace()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
fndList = Array("Canada", "United States", "Mexico")
rplcList = Array("CAN", "USA", "MEX")
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
End Sub
And I have adapted to my situation (I should mention that I have already a filter on that sheet with a selection on the column J, so I had also to remove the filter in my code):
Sub Z_Find_mywords()
'Find/highlight(optional replace) a list of words (case sensitive) in a specific column
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
Worksheets("Sheet1").Activate
Selection.AutoFilter 'Removes the filter from the entire sheet
Columns("J:J").Select
With Application.ReplaceFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
fndList = Array("ALBA", "ARAD", "BACAU")
rplcList = Array("ALBA", "ARAD", "BACAU")
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each cell of the column
Selection.Replace What:=fndList(x), Replacement:=rplcList(x), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=True
Next x
End Sub
Of course I made it for 3 words and I can add now all my list inside the code.
I welcome anyone that wants to improve it (should be... maybe specify the range instead the selection... I am a totally newbie in VBA, so I believe there is place for improvement).
Anyway... it works already for me this code... so the problem is SOLVED. Thank you Keebellah for suggesting to do recording macro on find/replace... I used multiple times the tool to find my way.
How can be modified the code to point at the list of the words located in Sheet2 instead of including all the searched words inside the VBA code? You know... when you have 50 words like me, the code doesn't look so pretty.
I wait 24 hours and if nothing new I will mark the thread as "Solved".
Bookmarks