Shade whole row on specific term

Dear Experts:

below code searches for a specific term in a table (input by means of an inputbox) and shades the whole row grey where this term is located.

The code works fine but ...

... although I have indicated '.MatchWholeWord = True', more rows get shaded as required.

Example: Inputbox Input = "Set"

If a cell contains the term "My Set" or "Configuration-Set" or "Set Configuration", these rows also get shaded, but I only would like to get those rows shaded where the cell contents is just "Set" and nothing else.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub Term_Specific_Shading()

Dim ht As Single
Dim strLen As Integer
Dim rng As Range
Dim strFind As String
Dim Index As Variant
    Dim rngStory As Range

 If MsgBox("Would you like to grey shade rows (20% grey) containing a specific term?" & vbCrLf & vbCrLf & _
 "Shading for rows containing a specific text!", vbYesNo + vbQuestion, "Shade Rows featuring specific term: 20% Grey)") = vbNo Then
        Exit Sub
        End If

    strFind = InputBox("Which term is to be searched?", "Indicate Term")
Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
    With rngStory
        With .Find
            .Text = strFind
            .MatchWholeWord = True
        End With
            If .Find.Found Then
                If rngStory.Information(wdWithInTable) Then
                       rngStory.rows(1).Shading.BackgroundPatternColor = RGB(235, 235, 235)
                        rngStory.ParagraphFormat.KeepWithNext = True
                End If
            End If
            rngStory.MoveStart 3
        Loop Until Not (.Find.Found)
    End With

End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?

to test if your cell has only the strFind then try

            If .Find.Found Then
                If rngStory.Information(wdWithInTable) Then
                    If Left(rngStory.Cells(1).Range.Text, Len(rngStory.Cells(1).Range.Text) - 2) = strFind Then
                       rngStory.Rows(1).Shading.BackgroundPatternColor = RGB(235, 235, 235)
                        rngStory.ParagraphFormat.KeepWithNext = True
                    End If
                End If
            End If

Open in new window

.MatchWholeWord = False would find Setting as well as Set

Andreas HermleTeam leaderAuthor Commented:
RGonzo, again, I am very impressed with your VBA expertise. Great job, thank you very much for your professional support.

Regards, Andreas
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.