color cells based in cell string

15 views
Skip to first unread message

Daniel Spitale

unread,
Jan 23, 2021, 1:08:17 PM1/23/21
to
Hello forum,
I have many tables in word documents and I want to color (background color) the cells according to specific string. Below my code but nothing happen when I run it.

Sub colora()
Dim MyTable As Table
Dim MyRow As Row
Dim MyCell As Cell
For Each MyTable In ActiveDocument.Tables
For Each MyRow In MyTable.Rows
For Each MyCell In MyRow.Cells
If MyCell.Range.Text = "ottimo" Then
MyCell.Shading.BackgroundPatternColor = wdColorBlue
ElseIf MyCell.Range.Text = "ottimo-buono" Then
MyCell.Shading.BackgroundPatternColor = wdColorGreen
End If
Next MyCell
Next MyRow
Next MyTable
Set MyCell = Nothing
Set MyRow = Nothing
Set MyTable = Nothing
End Sub

However, if I use the code below it works, but not exactly as I want because both the conditions are colored in blue.
Where I'm wrong??
Thank you for any help!
Daniel

Sub colora()
Dim MyTable As Table
Dim MyRow As Row
Dim MyCell As Cell
For Each MyTable In ActiveDocument.Tables
For Each MyRow In MyTable.Rows
For Each MyCell In MyRow.Cells
If MyCell.Range.Text Like "ottimo*" Then
MyCell.Shading.BackgroundPatternColor = wdColorBlue
ElseIf MyCell.Range.Text Like "ottimo-buono" Then
MyCell.Shading.BackgroundPatternColor = wdColorGreen
End If
Next MyCell
Next MyRow
Next MyTable
Set MyCell = Nothing
Set MyRow = Nothing
Set MyTable = Nothing
End Sub
Reply all
Reply to author
Forward
0 new messages