VBA - Colour code a string of text

kialexander

New Member
Joined
Jan 11, 2018
Messages
2
Hi,

I am currently using the vba code below to turn the sentence "Deal capacity over 95%" to red, however, the issue I am facing is that it turns all text in that cell red, not just the statement "Deal capacity over 95%". Is there a way I can stop the whole cell text turning red and just turn the statement "Deal capacity over 95%" red instead?

Columns("H:H").Select
With Application.ReplaceFormat.Font
.Superscript = False
.Subscript = False
.Color = 255
.TintAndShade = 0
End With
Selection.Replace What:="Deal capacity over 95%", _
Replacement:="Deal capacity over 95%", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True
Range("H6").Select


Many Thanks,

Kirstie
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You need to apply the formatting to the actual characters in the string. This would achieve what you want I think:

Code:
Public Sub HighlightSearchString()

Const SearchString = "Deal capacity over 95%"

Dim firstCell As Range
Dim nextCell As Range

Set firstCell = Range("H:H").Find(SearchString, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False)
If firstCell Is Nothing Then Exit Sub
Set nextCell = firstCell
Do
    nextCell.Characters(Start:=InStr(1, nextCell.Value, SearchString, vbTextCompare), Length:=Len(SearchString)).Font.Color = 255
    Set nextCell = Range("H:H").FindNext
Loop Until nextCell.Address = firstCell.Address

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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