Amend an Find Replace macro to offset the results column

Levitation

New Member
Joined
May 18, 2017
Messages
9
Hi. So, below Ialready have a working macro which when I highlight a cell or column ofdata in one sheet (not in the ClientNames sheet as that’s the sheet beingreferenced), it will reference Table4 (which is Col A to B in theClientNames sheet) and change the name based on what is in Col A towhat is in Col B from Table4.
e.g. say I select ‘Mike’ somewhere a sheet in the workbook, and I then runthe macro. Because in Table4, cell A2 contains 'Mike' and B2 contains'Michael', the macro changes Mike to Michael.

Sub A_MrExcelReplaceRangeSelectedClientNames()
'PURPOSE: Find & Replace a list of text/values in a range
Dim X As Long
' code from Joe at MrExcel
Dim Table4 As ListObject, vFindReplc As Variant
Set Table4 = Sheets("ClientNames").ListObjects("Table4")
vFindReplc = Table4.DataBodyRange.Columns("A:B").Value 'DesignateColumns for Find/Replace data
'Loop through each item in Array lists
For X = LBound(vFindReplc, 1) To UBound(vFindReplc, 1)
Selection.Replace What:=vFindReplc(X, 1), Replacement:=vFindReplc(X, 2), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next X
End Sub


However, what Iwant the macro to do is when I when I select a cell or a column (for exampleCol D), I want it to check for all the entries in Table4 Col A and return thevalue from Col B (as above, Table4, cell A2 contains 'Mike' and B2 contains'Michael'), however the result needs to be offset by one column to the rightfrom the column which was originally selected.
e.g. if I select‘Mike’ in Column D (let’s say ‘Mike’ is in cell D5) of any sheet (apart from theClientNames sheet), I want the macro to interrogate Table4, and paste ‘Michael’in Column E (so in this scenario it’d paste ‘Michael’ in cell E5) etc.

Thanks

Mike

 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
All's quiet on the Western Front - perhaps I should ad something more pictorial to explain how I want to change the macro!?
 
Upvote 0
Try this

Select a cell, a range of cells (in a single column) or a column and run the following macro.

Code:
Sub Search_Name()
  Dim c As Range, f As Range, rng As Range, sh As Worksheet, table4 As ListObject
  Set sh = Sheets("[COLOR=#0000ff]ClientNames[/COLOR]")
  Set table4 = sh.ListObjects("[COLOR=#0000ff]Table4[/COLOR]")
  If ActiveSheet.Name = sh.Name Then Exit Sub
  Set rng = Selection
  For Each c In rng.SpecialCells(xlCellTypeConstants)
    Set f = table4.DataBodyRange.Columns(1).Find(c, , xlValues, xlWhole)
    If Not f Is Nothing Then
      c.Offset(, 1).Value = f.Offset(, 1).Value
    End If
  Next
End Sub
 
Upvote 0
Im glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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