Change the Row shading in a table

S

Strobo

Hi,

I'm making a table that contains 3 check boxes in each row (Pass, Fail and
NT). I want to make any row with a Fail ticked turn red.

Im trying to do is by setting an exit macro as

Sub onFailGoRed()
Selection.Rows(1).Shading.BackgroundPatternColor = wdColorRed
End Sub

but this ends up changing the shading of the first line in the document
rather than the current row in the table.

What am I doing wrong?

Thanks
 
G

Greg Maxey

Protected forms often don' play nice. You would need to first unprotect the
form:

Sub onFailGoRed()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
If Selection.FormFields(1).CheckBox.Value = True Then
Selection.Rows(1).Shading.BackgroundPatternColor = wdColorRed
Else
Selection.Rows(1).Shading.BackgroundPatternColor = wdColorAutomatic
End If
ActiveDocument.Protect wdAllowOnlyFormFields, True
End If
End Sub
 
S

Strobo

Thanks Greg...

That seems to work...although I have other issues with the code.
Neverending troubles with VBA in word :)

I do have another question.
It seems that VBA does not like tables with merged rows/columns.
In most parts I get around this by traversing individual cells rather than
rows.

But how would I go about it in this case where I want to change the row's
shading?

Thanks
 
G

Greg Maxey

Yep. That can be a problem. As long a the columns are not merged you might
be able to use some adaptation of this:

Sub Test()
Dim i As Long
Dim j As Long
i = Selection.Information(wdEndOfRangeRowNumber)
On Error Resume Next
For j = 1 To Selection.Tables(1).Columns.Count
Selection.Tables(1).Cell(i, j).Shading.BackgroundPatternColor = wdColorRed
Next
End Sub
 
S

Strobo

Uh... I have merged rows as well as columns so it might not work for me.
Doesn't matter...I might even leave the feature out.

The idea was to highlight any Failed tests...but I might just leave it out.
I better move onto other questions :)

Thanks anyway...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top