PDA

View Full Version : VBA CONDITIONAL FORMATIING IN WORD



stevembe
02-22-2016, 06:45 AM
Not sure if this can be done in MS Word but I have a cell in a table with a drop down menu that users can select Low, Medium or High. Is there anyway of changing the cell colour to Green for Low, Yellow for Medium and Red for High?

Thank you for taking the time to read.

gmaxey
02-22-2016, 05:52 PM
Assumes it is a content control dropdown titled "DD"

Put this code in the ThisDocument module:

Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean)
Dim oCell As Cell
Select Case oCC.Title
Case "DD"
Set oCell = oCC.Range.Cells(1)
Select Case oCC.Range.Text
Case "High": oCell.Shading.BackgroundPatternColor = wdColorRed
Case "Medium": oCell.Shading.BackgroundPatternColor = wdColorYellow
Case "Low": oCell.Shading.BackgroundPatternColor = wdColorGreen
Case Else: oCell.Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End Select
lbl_Exit:
Exit Sub
End Sub

stevembe
12-09-2016, 03:44 AM
Assumes it is a content control dropdown titled "DD"

Put this code in the ThisDocument module:

Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean)
Dim oCell As Cell
Select Case oCC.Title
Case "DD"
Set oCell = oCC.Range.Cells(1)
Select Case oCC.Range.Text
Case "High": oCell.Shading.BackgroundPatternColor = wdColorRed
Case "Medium": oCell.Shading.BackgroundPatternColor = wdColorYellow
Case "Low": oCell.Shading.BackgroundPatternColor = wdColorGreen
Case Else: oCell.Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End Select
lbl_Exit:
Exit Sub
End Sub

Just looking back through old posts and noticed I had not thanked you for this one but I thought I had. Please accept my apologies and thank you for the solution, it works perfectly, I appreciate your help and time.

gmaxey
12-09-2016, 04:49 AM
Perfectly fine. I'm pleased that I was able to help.