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

conditionally_color_text_VBA.docx
Andreas HermleTeam leaderAsked:
Who is Participating?

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

x
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.

GrahamSkanRetiredCommented:
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

1

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
GrahamSkanRetiredCommented:
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.
0
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

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

Thank you
0
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
0
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

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

Regards, Andreas
0
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
VBA

From novice to tech pro — start learning today.