Change color of specific text piece

  • Thread starter Thread starter Faraz A. Qureshi
  • Start date Start date
F

Faraz A. Qureshi

Any idea of a suitable piece of code to convert the fonts' color of ONLY the
WORD "people", in the CURRENT SELECTION?

In other words, instead of hitting F2 a hundred times, placing the cursor to
the specific piece of text everytime, selecting it, and changing it to red
(for example), and then again moving to the other cell, there should be a
single code to carryout the exercise.
 
Hi Faraz

Try the below macro which works on the current selection


Sub FindAndColorCharacterWithinCell()
Dim cell As Range, intPos as Integer
For Each cell In Selection
With cell
Do
intPos = InStr(intPos + 1, .Value, "people", vbTextCompare)
If intPos Then
..Characters(Start:=intPos, Length:=6).Font.FontStyle = "Bold"
..Characters(Start:=intPos, Length:=6).Font.ColorIndex = 3
End If
Loop Until intPos = 0
End With
Next
End Sub
 
Here is a macro that should do what you want...

Sub PeopleColorer()
Dim Cell As Range, StartAt As Long
For Each Cell In Selection
StartAt = InStr(1, Cell.Value, "people", vbTextCompare)
Do While StartAt > 0
Cell.Characters(StartAt, 6).Font.ColorIndex = 3
StartAt = InStr(StartAt + 1, Cell.Value, "people", vbTextCompare)
Loop
Next
End Sub
 
Thanx bro!
--
Best Regards,

Faraz


Rick Rothstein said:
Here is a macro that should do what you want...

Sub PeopleColorer()
Dim Cell As Range, StartAt As Long
For Each Cell In Selection
StartAt = InStr(1, Cell.Value, "people", vbTextCompare)
Do While StartAt > 0
Cell.Characters(StartAt, 6).Font.ColorIndex = 3
StartAt = InStr(StartAt + 1, Cell.Value, "people", vbTextCompare)
Loop
Next
End Sub

--
Rick (MVP - Excel)




.
 
Back
Top