Conditionally apply a font color to rows using VBA

Andreas Hermle
Andreas Hermle used Ask the Experts™
on
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

conditionally_color_text_VBA.docx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Top Expert 2012
Commented:
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

GrahamSkanRetired
Top Expert 2012

Commented:
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 leader

Author

Commented:
Hi Graham,

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

Thank you
Andreas HermleTeam leader

Author

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 leader

Author

Commented:
Hi Graham,

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

Regards, Andreas

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial