Change font color based on partial text

mickyh

New Member
Joined
Jul 10, 2012
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Good morning
I'm looking for suggestions on how I might change the font color based on the first word (or two) so that I can highlight cells that start "Optional" or "Provisional Sum" in red. The first problem I'm having is searching with partial text. (not sure if my code beyond that works or not)?
VBA Code:
Sub FindStringSOW()

Dim c As Range
    Dim firstAddress As String

    With ActiveSheet.Range("C1:C100")
        Set c = .Find("Optional:** ", LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.font = vbRed
                Set c = .FindNext(c)
            Loop While Not c Is Nothing
        End If
    End With

End Sub
 
Last edited by a moderator:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this instead

VBA Code:
Sub FindStringSOW_v2()
  Dim c As Range
  Dim firstAddress As String
  
  With ActiveSheet.Range("C1:C100")
    Set c = .Find("Optional*", LookIn:=xlValues, LookAt:=xlWhole)
    If Not c Is Nothing Then
      firstAddress = c.Address
      Do
        c.Font.Color = vbRed
        Set c = .FindNext(c)
      Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
    Set c = .Find("Provisional Sum*", LookIn:=xlValues, LookAt:=xlWhole)
    If Not c Is Nothing Then
      firstAddress = c.Address
      Do
        c.Font.Color = vbRed
        Set c = .FindNext(c)
      Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
  End With
End Sub
 
Upvote 0
.. or another approach that you could try. (Assumes a heading row in row 1)

VBA Code:
Sub FindStringSOW_v3()
  With Range("C1:C100")
    .AutoFilter Field:=1, Criteria1:="Optional*", Operator:=xlOr, Criteria2:="Provisional Sum*"
    If .SpecialCells(xlVisible).Count > 1 Then .Offset(1).Resize(.Rows.Count - 1).Font.Color = vbRed
    .AutoFilter
  End With
End Sub
 
Upvote 0
Solution
You're welcome. Thanks for the follow-up. :)

Did you try the second, more compact, form, too?
 
Upvote 0
Thanks again Peter
That also works perfectly and is the solution that I'm using. I also find it easier to follow. ? I've now marked that as the solution but both worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,953
Members
449,198
Latest member
MhammadishaqKhan

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