Macro for multiple find and replace criteria

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
Hi All,

I have data that is one of six values in column H (ABC, AAA, CAB, BAC, BBB, CCC).

I would like to replace each of those values with a text value from another list. So, if a cell says ABC, I would like to replace it with the text ONE; if it says AAA, then replace it with TWO, and so on.

Is there a fast and easy way to set that up in a macro?

I had an idea of how to do it, but I think it's getting really complicated and I know there has to be an easy way of doing this.

Thanks in advance for your help!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can use a filter. In Excel 2002/2003, go to "Data" → "Filter" →"AutoFilter". For each value that you filter in column H, replace the data with the value you want
 
Upvote 0
If you want a macro to do this for you, you can use this. I'm still learning to use arrays but this should work nicely.

Code:
Sub Replace_Values()

Dim myVar() As String, myRepl() As String, currBook As String
Dim myCol As Range
Dim LastRow As Long

'Replace C:\Documents and Settings\bb185064\Desktop\Test1.xlsx with the pathname
'of the file that contains the list
currBook = ActiveWorkbook.Name
Workbooks.Open "C:\Documents and Settings\bb185064\Desktop\Test1.xlsx"
Max = Range("A" & Rows.Count).End(xlUp).Row - 1

ReDim myVar(1 To Max)
ReDim myRepl(1 To Max)

With Range("A1")
    For i = 1 To Max
        myVar(i) = .Offset(i, 0)
        myRepl(i) = .Offset(i, 1)
    Next i
End With

ActiveWorkbook.Close

Windows(currBook).Activate
LastRow = Range("H" & Rows.Count).End(xlUp).Row
Set myCol = Range("H2:H" & LastRow)

For i = 1 To Max
        myCol.Replace What:=myVar(i), Replacement:=myRepl(i), LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
Next i

End Sub

The only thing you would have to change it the Workbooks.Open line that has the comment above it. Also, this macro should be run when you have the book that the replacements will be made on as your activeworkbook.

All that you have to do to maintain this is keep that list up to date. If you choose to use this then you should have a workbook set up with what you want to replace in column A starting at A2 and what it should be replaced with in column B.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,510
Messages
6,125,222
Members
449,216
Latest member
biglake87

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