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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.