There are a couple of ways of doing calculations in Word tables, but there's nothing built in to do conditional formatting the way Excel does.
For calculations, there are two non-VBA methods, each of which has its drawbacks.
- Put legacy text form fields in the "incoming data" cells such as a1 and b1 in your example. In their Properties dialogs, check both "Fill-in enabled" and "Calculate on exit". For your example, you could name the first field F and the second field S (in the Bookmark box). Then in cell c1, insert an ordinary formula field with the field code { = F * S }. Each time the cursor exits either of the form fields, the formula field will automatically update. The drawback of this is that the legacy fields require restricting editing to "Filling in fields", which isn't always compatible with content controls elsewhere in the form.
- Leave the data entry fields as ordinary table cells. Insert an ordinary formula field in the calculated cell, using the functions available in the Help topic "Use a formula in a Word or Outlook table". In this case, the field code could be either { = PRODUCT(a1,b1) } or { = PRODUCT(LEFT) }. [Note: the a1 and b1 would have to be replaced with the actual column letters and row number in the table, which are not a, b, and 1 in your example.] The drawback is that the calculation isn't automatic; you must manually update the formula field, either by selecting it and pressing F9 or by going to Print Preview and back.
Neither of these would do anything about coloring the cell backgrounds or changing the content of cell d1.
Since the results of our previous exploration involve a macro, it would probably be best to continue on that path. In that case, the necessary code can be added to the ContentControlOnExit macro you already have.
- Insert plain text content controls in all four cells (and in the corresponding cells of other rows). Give each control a unique Tag value that includes the row number. For the example, I would use tags like F1, S1, R1, and Risk1, and just change the number for each additional row. (These don't have to be the actual row numbers.)
- Replace the code in your previous post with this...
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim ccCH1 As ContentControl
Dim ccCH2 As ContentControl
Dim ccATnum As ContentControl
Dim ccF As ContentControl
Dim ccS As ContentControl
Dim ccR As ContentControl
Dim ccRisk As ContentControl
Dim intRow As Integer
Dim intVal As Integer
Dim rg As Range
Set ccCH1 = ActiveDocument.SelectContentControlsByTag("CH1")(1)
Set ccCH2 = ActiveDocument.SelectContentControlsByTag("CH2")(1)
Set ccATnum = ActiveDocument.SelectContentControlsByTag("TITLE")(1)
Select Case True ' tag of control being exited
Case CC.Tag = "CH1"
If CC.Checked Then
ccCH2.Checked = False
ccATnum.Range.Text = "AT1"
ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = False
Else
ccCH2.Checked = True
ccATnum.Range.Text = "AT2"
ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = True
End If
Case CC.Tag = "CH2"
If CC.Checked Then
ccCH1.Checked = False
ccATnum.Range.Text = "AT2"
ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = True
Else
ccCH1.Checked = True
ccATnum.Range.Text = "AT1"
ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = False
End If
Case (CC.Tag Like "F*") Or (CC.Tag Like "S*")
intRow = Right(CC.Tag, Len(CC.Tag) - 1)
' get the "R" control in the same row
Set ccF = ActiveDocument.SelectContentControlsByTag("F" & intRow)(1)
Set ccS = ActiveDocument.SelectContentControlsByTag("S" & intRow)(1)
Set ccR = ActiveDocument.SelectContentControlsByTag("R" & intRow)(1)
Set ccRisk = ActiveDocument.SelectContentControlsByTag("Risk" & intRow)(1)
intVal = Val(ccF.Range.Text) * Val(ccS.Range.Text)
ccR.Range.Text = intVal
Set rg = ccR.Range.Duplicate
'include cell marker, thus whole cell
rg.MoveEnd wdCharacter, 2
If intVal < 5 Then
rg.Shading.BackgroundPatternColorIndex = wdBrightGreen
ccRisk.Range.Text = "Low"
ElseIf intVal <= 9 Then
rg.Shading.BackgroundPatternColorIndex = wdYellow
ccRisk.Range.Text = "Medium, Tolerable"
Else
rg.Shading.BackgroundPatternColorIndex = wdRed
ccRisk.Range.Text = "High, Danger"
End If
Case Else
Exit Sub
End Select
End Sub