Conditionally apply a font color to rows using VBA

Dear Experts:

I got a big word document with hundreds of tables.

Dozens of the rows of these 8 column tables have an entry in the 8th column, saying 'blue'.

I now would like to run a macro that performs the following action.

Whenever a row in any of these tables has the entry 'blue' in the 8th column, the text entries of the whole row have to have a blue font color (RGB 0,0,255).

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

I have attached a sample file for your convenience.

Regards, Andreas

Andreas HermleTeam leaderAsked:
Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
This macro is not case-sensitive, so will accept "Blue". The text length is checked so that it will not respond to, say, 'blue-green'
Sub BlueRow()
    Dim tbl As Table
    Dim rw As row
    For Each tbl In ActiveDocument.Tables
        For Each rw In tbl.Rows
            If Len(rw.Cells(8).Range.Text) = 6 Then
                If InStr(1, rw.Cells(8).Range.Text, "blue", vbTextCompare) Then
                    rw.Range.Font.Color = wdColorBlue
                End If
            End If
        Next rw
    Next tbl
End Sub

Open in new window

Note that, if  the number of instances of the target word is relatively low, it could be be more performant to use Find instead of examining every cell number eight.
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

great, works as desired, will do some more testing and then let you know :-)

Thank you
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

great, works like a charm. I wonder if you could tweak the code so that 'line 6' and 'line 7' of your code accomodates the following new requirement:

It is always the very last column of the tables where this termn 'blue' has been entered.

Help is much appreciated.

Thank  you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

works like a charm, thank you very much for your great help.

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.