【问题标题】:Using a macro to conditional format a specific column in Word Table使用宏对 Word 表中的特定列进行条件格式设置
【发布时间】:2019-02-12 18:29:55
【问题描述】:

我有一个宏,可以根据同一单元格中的文本更改 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


【解决方案1】:

您可以使用 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

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

【讨论】:

  • 我在这个问题中没有看到对内容控件的引用...?
  • 这就是为什么我问正在使用什么种类的下拉菜单。 OP 指的是下拉菜单,但没有说明是什么类型。
  • 在第 2 段结尾处显示“...用户从下拉列表中输入答案...”
【解决方案2】:

基于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

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多