Hello prayami,
Here is the updated macro. It has been added to the attached workbook.
' Thread: http://www.excelforum.com/excel-programming/789158-match-and-replace-with-index.html
' Poster: prayami
' Written: August 24, 2011
' Updated: August 26, 2011 - Substitute alternate text for city and state
' Author: Leith Ross
Sub ReplaceCityState()
Dim Cell As Range
Dim CityRng As Range
Dim CityWks As Worksheet
Dim FindWhat As Variant
Dim Match As Range
Dim Rng As Range
Dim StateRng As Range
Dim StateWks As Worksheet
Dim Wks As Worksheet
Set CityWks = Worksheets("City")
Set StateWks = Worksheets("State")
Set CityRng = CityWks.Range("A1").CurrentRegion
Set StateRng = StateWks.Range("A1").CurrentRegion
Set Wks = Worksheets("Suburb_City_State")
Set Rng = Wks.Range("A1").CurrentRegion
Set Rng = Intersect(Rng, Rng.Offset(1, 0)).Resize(RowSize:=Rng.Rows.Count - 1)
For Each Cell In Rng.Columns(2).Cells
FindWhat = Cell.Item(1, 1).Value
Set Match = CityRng.Find(FindWhat, , xlValues, xlWhole, xlByRows, xlNext, False)
If Not Match Is Nothing Then Cell.Item(1, 1).Value = Match.Offset(0, 1)
FindWhat = Cell.Item(1, 2).Value
Set Match = StateRng.Find(FindWhat, , xlValues, xlWhole, xlByRows, xlNext, False)
If Not Match Is Nothing Then Cell.Item(1, 2).Value = Match.Offset(0, 1)
Next Cell
End Sub
Bookmarks