Conditionally apply blue font color to table rows using VBA

Dear Experts:

The following macro (courtesy of the MS Word 'Sage' Graham Skan from EE) applies a blue font color on all table rows where the text entry in the 8th column of the tables is 'blue'

I got a a new requirement:

It is always the last column where this text entry is found. Hence instead of hard coding the 8th column, Line 6 and Line 7 should be tweaked to accomodate the new requirement.

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


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

Regards, Andreas
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:

rw.Cells.Count

Open in new window


hence:

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(rw.Cells.Count).Range.Text) = 6 Then
                If InStr(1, rw.Cells(rw.Cells.Count).Range.Text, "blue", vbTextCompare) Then
                    rw.Range.Font.Color = wdColorBlue
                End If
            End If
        Next rw
    Next tbl
End Sub

Open in new window

0

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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
not too sure about the condition:

If Len(rw.Cells(8).Range.Text) = 6 Then

Open in new window

which you may simply remove it.

to become:

Sub BlueRow()
    Dim tbl As Table
    Dim rw As Row
    For Each tbl In ActiveDocument.Tables
        For Each rw In tbl.Rows
            If InStr(1, rw.Cells(rw.Cells.Count).Range.Text, "blue", vbTextCompare) Then
                rw.Range.Font.Color = wdColorBlue
            End If
        Next rw
    Next tbl
End Sub

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Great Ryan, this did the trick, thank you very much for your swift 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.