Conditional formatting for table cells

L

Lenny

In a protected Word 2003 form template I need to set up the following
conditional formatting of two rows of cells.

Row 1 Number entered into cell A12 (4)
Row 2 Number entered into cell A13 (7)

Result: if the number entered into A13 is => A12 then cell A13 fills with
Green
if the number entered into A13 is =< A12 then cell A13 fills with
Red

I have to replicate this in each of 20 columns. Would the macro run as an
exit macro for each cell in each column? Do I need a separate macro for each
column cell?

This is easy to set up in Excel, but can it be set up in Word with form
fields. Will the protection impede the code running?

Assistance with the concept and coding would be greatly appreciated.
 
J

Jay Freedman

In a protected Word 2003 form template I need to set up the following
conditional formatting of two rows of cells.

Row 1 Number entered into cell A12 (4)
Row 2 Number entered into cell A13 (7)

Result: if the number entered into A13 is => A12 then cell A13 fills with
Green
if the number entered into A13 is =< A12 then cell A13 fills with
Red

I have to replicate this in each of 20 columns. Would the macro run as an
exit macro for each cell in each column? Do I need a separate macro for each
column cell?

This is easy to set up in Excel, but can it be set up in Word with form
fields. Will the protection impede the code running?

Assistance with the concept and coding would be greatly appreciated.

The same macro can be used as the exit macro for all the form fields in rows 12
and 13, because the Selection.Information function can determine which column
the cell is in. Also, the macro unprotects the document before changing the cell
color and reprotects it afterward.

Sub ExitCell()
Dim Cell12 As Cell
Dim Cell13 As Cell
Dim colNum As Long

' Assumes that every form field that has this macro
' as its exit macro is in a table cell. If that may
' not be the case, you need code to check for it.

On Error GoTo ErrHdl

colNum = Selection.Information(wdEndOfRangeColumnNumber)
Set Cell12 = Selection.Tables(1).Cell(12, colNum)
Set Cell13 = Selection.Tables(1).Cell(13, colNum)

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

If Val(Cell13.Range.Text) >= Val(Cell12.Range.Text) Then
Cell13.Shading.BackgroundPatternColor = wdColorGreen
Else
Cell13.Shading.BackgroundPatternColor = wdColorRed
End If

Set Cell12 = Nothing
Set Cell13 = Nothing

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True

Exit Sub

ErrHdl:
MsgBox Err.Description
End Sub
 
L

Lenny

:
Jay - many, many thanks! I read several posts indicating that conditional
formatting was not possible in Word.... I thought that setting up an "if'
statement might get me part of the way there, but your solution hit the
mark...
 

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