Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

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
        .Find.ClearFormatting
        With .Find
            .Text = strFind
            .MatchWholeWord = True
            .Execute
        End With
        Do
            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
            .Find.Execute
        Loop Until Not (.Find.Found)
    End With

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andreas Hermle

ASKER

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

Regards, Andreas