Modify code for Highlighting text

bradjedis
4StarLounger
Posts: 550
Joined: 30 Mar 2010, 18:49
Location: United States

Modify code for Highlighting text

Post by bradjedis »

Greetings,

Hans built the below code: Instead of just picking out specific words and changing the color/bold, I would like to change all the text in the the entire row to be the identified style/color.

Code: Select all

Sub FindAndHighlight2()
    Const CaseSensitive = False '<-- adjust
    Dim wsh As Worksheet
    Dim strFind As Variant
    Dim rng As Range
    Dim strAddress As String
    Dim CompareType As VbCompareMethod
    Dim myLen As Long
    Dim Posn As Long
    Application.ScreenUpdating = False
    If CaseSensitive Then CompareType = vbBinaryCompare Else CompareType = vbTextCompare
    For Each wsh In Worksheets
        
        With wsh.Range("D:R")
            
            For Each strFind In Array("brad", "sam") '<-- adjust
                
                
                myLen = Len(strFind)
                Set rng = .Find(What:=strFind, LookAt:=xlPart, MatchCase:=CaseSensitive)
                If Not rng Is Nothing Then
                    strAddress = rng.Address
                    Do
                        Posn = InStr(1, rng.Value, strFind, CompareType)
                        Do
                            With rng.Characters(Start:=Posn, Length:=myLen).Font
                                .FontStyle = "Bold" '<-- adjust
                                .ColorIndex = 5
                            End With
                            Posn = InStr(Posn + 1, rng.Value, strFind, CompareType)
                        Loop Until Posn = 0
                        Set rng = .FindNext(After:=rng)
                    Loop Until rng.Address = strAddress
                End If
            Next strFind
        End With
    Next wsh
    Application.ScreenUpdating = True
End Sub
Thanks,
Brad

User avatar
HansV
Administrator
Posts: 78794
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Modify code for Highlighting text

Post by HansV »

You can use conditional formatting for that instead of code if you like. That way, rows will be colored automatically without having to run a macro.

Select the range you want to apply this to, for example rows 2 to 100.
The active cell in the selection should be in the top row, in this example in row 2.
On the Home tab of the ribbon, click Conditional Formatting > New Rule...
Select 'Use a formula to determine which cells to format'.
Enter the formula

=COUNTIF($D2:$R2, "Brad")+COUNTIF($D2:$R2, "Sam")

Click Format...
Activate the Font tab.
Select a highlight color.
Click OK, then click OK again.
Best wishes,
Hans

bradjedis
4StarLounger
Posts: 550
Joined: 30 Mar 2010, 18:49
Location: United States

Re: Modify code for Highlighting text

Post by bradjedis »

Can I stack several within the formula?

IE: =COUNTIF($D2:$R2, "Brad")+COUNTIF($D2:$R2, "Sam")+COUNTIF($D2:$R2, "fred")+COUNTIF($D2:$R2, "Mary")

User avatar
HansV
Administrator
Posts: 78794
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Modify code for Highlighting text

Post by HansV »

Yes, you can!
Best wishes,
Hans

bradjedis
4StarLounger
Posts: 550
Joined: 30 Mar 2010, 18:49
Location: United States

Re: Modify code for Highlighting text

Post by bradjedis »

hmmm, so I need it to do the formatting if the cells contain the info as part of the cell. cell can contain MORE than the target value, so it cannot be an exact match.

User avatar
HansV
Administrator
Posts: 78794
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Modify code for Highlighting text

Post by HansV »

That is possible too, using wildcards:

=COUNTIF($D2:$R2, "*Brad*")+COUNTIF($D2:$R2, "*Sam*")+COUNTIF($D2:$R2, "*fred*")+COUNTIF($D2:$R2, "*Mary*")
Best wishes,
Hans