Highlight the word overtime in cell

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a workbook with many comments which I have used some code from contextures.com to pull all the comments into a seperate sheet.

What I would like to do is highlight all the instances of overtime.

<TABLE style="WIDTH: 85pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=113><COLGROUP><COL style="WIDTH: 85pt; mso-width-source: userset; mso-width-alt: 4132" width=113><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 85pt; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20 width=113>special overtime</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20>Overtime taken</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20>Double overtime</TD></TR></TBODY></TABLE>

Here is what I am trying, but can't figure out how to highlight overtime when it is in a cell with other words.

Code:
Sub FindWord()
    Dim cell As Range
    Dim r As Range
    For Each cell In Selection
        Set r = cell.Find(what:=" overtime ", Lookin:=xlValues, lookat:=xlWhole)
        If Not r Is Nothing Then
            With r
                .Interior.ColorIndex = 6
            End With
        End If
    Next cell
End Sub
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Perhaps use wildcards?

Code:
Sub FindWord()
    Dim cell As Range
    Dim r As Range
    For Each cell In Selection
        Set r = cell.Find(what:="*overtime*", LookIn:=xlValues, lookat:=xlWhole)
        If Not r Is Nothing Then
            With r
                .Interior.ColorIndex = 6
            End With
        End If
    Next cell
End Sub
 
Upvote 0
Thanks to both for the responses

Is there anyway to only highlight the word overtime? These suggestions hightlight the entire cell.
 
Upvote 0
Try

Code:
Sub FindWord()
    Dim cell As Range
    Dim r As Long
    For Each cell In Selection
        r = InStr(1, cell.Value, "overtime", vbTextCompare)
        If r > 0 Then
            With cell
                .Characters(r, Len("overtime")).Font.ColorIndex = 6
            End With
        End If
    Next cell
End Sub

This will not work (and cannot be done) if the cell values are Formulas
 
Upvote 0
Thanks Jonmo works great. I can see now I was even barking up the wrong tree with .Interior.ColorIndex. Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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