Replace all based on Cell content

Corleone

Well-known Member
Joined
Feb 2, 2003
Messages
841
Office Version
  1. 365
1677179651408.png

Hi
I would like to adjust the code below so that instead of having to type out the name in the code ie Replacement:="BOB"
it simply looks through any of the cell entries in column D (the ones highlighted in yellow) and where there are any instances that the text occurs in any of the cells in column A it strips out any other text to the left or right of it.

thanks

Sub Macro2()
'
' Macro2 Macro
'

'
Columns("A:A").Select
Selection.Replace What:="*BOB*", Replacement:="BOB", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="*FRED*", Replacement:="FRED", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="*COOPER*", Replacement:="COOPER", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this:

VBA Code:
Sub Macro1()
  Dim c As Range
  For Each c In Range("D1", Range("D" & Rows.Count).End(3))
    ActiveSheet.Range("A:A").Replace What:="*" & c.Value & "*", Replacement:=c.Value, LookAt:=xlPart, _
      SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
  Next
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub Macro1()
  Dim c As Range
  For Each c In Range("D1", Range("D" & Rows.Count).End(3))
    ActiveSheet.Range("A:A").Replace What:="*" & c.Value & "*", Replacement:=c.Value, LookAt:=xlPart, _
      SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
  Next
End Sub
Thats works! - thanks
 
Upvote 1

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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