Forum

VBA replace and hig...
 
Notifications
Clear all

VBA replace and highlight

2 Posts
2 Users
0 Reactions
67 Views
(@lasher18)
Posts: 1
New Member
Topic starter
 

Hi guys, 

I would like to highlight the cells I am replacing but the code does not work, can you see the fail?

 

Sub find_replace1()

Range("C2:AG126").replace what:="something", replacement:="Hey", MatchCase:=True

with replacement

.replacement.highlight=true

end with

 

End Sub

 
Posted : 16/05/2020 11:46 am
(@purfleet)
Posts: 412
Reputable Member
 

I dont think you can use the find/replace in that way as it only finds and replaces.

Pretty sure you will need to test each cell and then colour it in

This should would work

Sub find_replace1()

Dim r As Range
Dim C As Range

Set r = Range("C2:AG126")

For Each C In r
Debug.Print C.Address

If C.Value = "s" Then
C.Value = "Hey"
C.Interior.Color = vbYellow
End If
Next C

End Sub

 
Posted : 16/05/2020 1:59 pm
Share: