使用宏条件格式化Word表中的特定列

问题描述 投票:0回答:2

我有一个宏,可以根据同一单元格中的文本更改Word中表格的背景颜色 - 类似于Excel的条件格式规则。

但是,我想将此限制为特定列 - 在包含多行但有两列的表中的第2列:第1列是问题的位置,第2列是用户从下拉列表中输入答案的位置 - 并且取决于答案细胞改变颜色。

我的代码如下;但这适用于两个列。

任何人都知道如何重新编码,因此它只适用于表格第2列。我使用的是MS Word 2016。

谢谢

Dim r As Range

Sub UBC ()
    color "No", wdRed
    color "Yes", wdGreen
    color "Unknown", wdYellow
    color "Not Applicable", wdGray50
End Sub

Function color(text As String, backgroundColor As WdColorIndex)
    Set r = ActiveDocument.Range

    With r.Find
       Do While .Execute(FindText:=text, MatchWholeWord:=True, Forward:=True) = True
    r.Cells(1).Shading.BackgroundPatternColorIndex = backgroundColor
       Loop
    End With
End Function
vba ms-word word-vba
2个回答
1
投票

建立在Answer that was given to you yesterday ...

一旦If检查了找到的Range是否在表中,就可以有条件地检查Range的单元格所在的列:

Function color(text As String, backgroundColor As WdColorIndex)
    Dim r As Word.Range

    Set r = ActiveDocument.content

    With r.Find
       Do While .Execute(findText:=text, MatchWholeWord:=True, Forward:=True) = True
          If r.Tables.Count > 0 Then
            If r.Cells(1).ColumnIndex = 2 Then
                r.Cells(1).Shading.BackgroundPatternColorIndex = backgroundColor
            End If
          End If
       Loop
    End With
End Function

0
投票

您可以使用ContentControl的Exit事件。当用户离开单元格时,它将根据所选内容进行格式化。此代码位于ThisDocument模块中。

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

    Select Case ContentControl.Range.Text
        Case "Yes"
            ContentControl.Range.Cells(1).Shading.BackgroundPatternColorIndex = wdGreen
        Case "No"
            ContentControl.Range.Cells(1).Shading.BackgroundPatternColorIndex = wdRed
        Case "Unknown"
            ContentControl.Range.Cells(1).Shading.BackgroundPatternColorIndex = wdYellow
        Case "Not Applicable"
            ContentControl.Range.Cells(1).Shading.BackgroundPatternColorIndex = wdGray50
    End Select

End Sub

如果您使用旧版下拉列表作为表单字段,则可以将此子项作为“退出宏”。您必须完成所有选项的宏。

Public Sub LegacyDropDownExit()

    ThisDocument.Unprotect

    Select Case Selection.FormFields(1).Result
        Case "Yes"
            Selection.Cells(1).Range.Shading.BackgroundPatternColorIndex = wdGreen
        Case "No"
            Selection.Cells(1).Range.Shading.BackgroundPatternColorIndex = wdRed
    End Select

    ThisDocument.Protect wdAllowOnlyFormFields, True

End Sub

如果您使用的是ActiveX控件,则可以执行以下操作

Private Sub ComboBox1_Change()

    ChangeCellBg Me.ComboBox1.Value, 1

End Sub

Private Sub ComboBox2_Change()

    ChangeCellBg Me.ComboBox2.Value, 2

End Sub

Private Sub ComboBox3_Change()

    ChangeCellBg Me.ComboBox3.Value, 3

End Sub

Private Sub ChangeCellBg(ByVal sValue As String, ByVal lRow As Long)

    Select Case sValue
        Case "Yes"
            Me.Tables(1).Cell(lRow, 2).Range.Shading.BackgroundPatternColorIndex = wdGreen
        Case "No"
            Me.Tables(1).Cell(lRow, 2).Range.Shading.BackgroundPatternColorIndex = wdRed
    End Select

End Sub

您还可以创建一个类模块,这样您就不必创建所有这些Change事件,但这超出了本答案的范围。

© www.soinside.com 2019 - 2024. All rights reserved.