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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,514
Messages
6,125,272
Members
449,219
Latest member
daynle

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