微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

搜索特定列后,如何使用 MS Word VBA 代码为具有特定文本的单元格着色?

如何解决搜索特定列后,如何使用 MS Word VBA 代码为具有特定文本的单元格着色?

我正在用 word 处理一个表格,并使用以下代码查找带有“是”和“否”字样的单元格并为其着色。在第 3 列和第 4 列中,我使用什么代码专门执行此操作?

Dim r As Range

Sub UBC()
    color "No",wdRed
    color "Yes",wdGreen
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

解决方法

您需要使用 Range.Information 属性来确定匹配项是否在表中。然后,您将使用单元格的 ColumnIndex 属性来确定它是否在所需的列中。

Function color(text As String,backgroundColor As WdColorIndex)
   Dim r As Range
   Set r = ActiveDocument.Content
   With r.Find
      Do While .Execute(FindText:=text,MatchWholeWord:=True,Forward:=True) = True
         If r.Information(wdWithInTable) Then
            With r.Cells(1)
               If .ColumnIndex = 3 Or .ColumnIndex = 4 Then .Shading.BackgroundPatternColorIndex = backgroundColor
            End With
         End If
      Loop
   End With
End Function

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。