【问题标题】:Find matching duplicates and color each a different color找到匹配的重复项并为每个不同的颜色着色
【发布时间】:2020-08-25 10:49:14
【问题描述】:

我正在尝试在文档中查找重复项,并将每个唯一匹配项的字体设置为不同的颜色。

我修改了我遇到的代码,它适用于前两个唯一的重复匹配然后停止。

我只想找到重复匹配的唯一 5 位整数 (

Sub ColorDuplicates()
 
    Dim xRg As Range
    Dim xTxt As String
    Dim xCIxCellndex As Long
    Dim xCol As Collection
    Dim xCell As Range
    'Dim xChar As String
    Dim xCellPre As Range
    Dim I As Long

    Set xRg = ActiveDocument.Words.Last

    xCIndex = 5
    Set xCol = New Collection

    For Each xCell In ActiveDocument.Words

        On Error Resume Next
        xCol.Add xCell, xCell.Text

        If Err.Number = 457 Then
            xCIndex = xCIndex + 1
            Set xCellPre = xCol(xCell.Text)

            If xCellPre.Font.ColorIndex = 1 Then xCellPre.Font.ColorIndex = xCIndex
                xCell.Font.ColorIndex = xCellPre.Font.ColorIndex

            End If
            On Error GoTo 0


    Next

End Sub

【问题讨论】:

    标签: vba ms-word duplicates


    【解决方案1】:

    您可以尝试以下方式:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim StrFnd As String, i As Long
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<[0-9]{5}>"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        If InStr(StrFnd, .Text) = 0 Then
          StrFnd = StrFnd & "|" & .Text
          i = UBound(Split(StrFnd, "|"))
        Else
          For i = 1 To UBound(Split(StrFnd, "|")) - 1
            If Split(StrFnd, "|")(i) = .Text Then Exit For
          Next
        End If
        i = i Mod 15
        If i < 7 Then
          i = i + 1
        Else
          i = i + 2
        End If
        .HighlightColorIndex = i
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    

    上面的代码最多可以处理14个重复的数字,之后颜色会被回收。按照编码,它会突出显示它们,而不是更改字体颜色。要执行后者,请更改:

    .HighlightColorIndex = i
    

    到:

    .Font.ColorIndex = i
    

    如果您需要超过 14 种字体颜色,则需要提供一些安排,以指定要使用的 1600 万种颜色中的哪一种 - 通过它们的 RGB 值。但是,对于突出显示,只有 16 种颜色存在,包括黑色和白色——我已经排除了这两种颜色。

    【讨论】:

    • 这太棒了!我将了解如何在字体中添加其他颜色。
    【解决方案2】:

    感谢您对此的帮助!

    我能够通过一种稍微不同的方式来解决这个问题。这是每组重复项具有 71 种不同颜色的代码。

    Sub HighFive()
    Const MaxColors = 56
    ' include a reference to Microsoft Scripting Runtime, under Tools References
    Dim Fives As Scripting.Dictionary, F As Variant
    Dim Colors As Collection, C As Integer
        Set Fives = New Scripting.Dictionary
        Set Colors = New Collection
        Colors.Add RGB(255, 0, 0) 'red
        Colors.Add RGB(255, 255, 0) 'yellow
        Colors.Add RGB(0, 240, 255) 'cyan
        Colors.Add RGB(255, 160, 0) 'orange
        Colors.Add RGB(240, 0, 255) 'fuchsia
        Colors.Add RGB(0, 255, 0) 'lime
        Colors.Add RGB(192, 192, 192) 'silver
        Colors.Add RGB(0, 0, 255) 'blue
        Colors.Add RGB(1, 0, 103)
        Colors.Add RGB(213, 255, 0)
        Colors.Add RGB(255, 0, 86)
        Colors.Add RGB(158, 0, 142)
        Colors.Add RGB(14, 76, 161)
        Colors.Add RGB(255, 229, 2)
        Colors.Add RGB(0, 95, 57)
        Colors.Add RGB(0, 255, 0)
        Colors.Add RGB(149, 0, 58)
        Colors.Add RGB(255, 147, 126)
        Colors.Add RGB(164, 36, 0)
        Colors.Add RGB(0, 21, 68)
        Colors.Add RGB(145, 208, 203)
        Colors.Add RGB(98, 14, 0)
        Colors.Add RGB(107, 104, 130)
        Colors.Add RGB(0, 0, 255)
        Colors.Add RGB(0, 125, 181)
        Colors.Add RGB(106, 130, 108)
        Colors.Add RGB(0, 174, 126)
        Colors.Add RGB(194, 140, 159)
        Colors.Add RGB(190, 153, 112)
        Colors.Add RGB(0, 143, 156)
        Colors.Add RGB(95, 173, 78)
        Colors.Add RGB(255, 0, 0)
        Colors.Add RGB(255, 0, 246)
        Colors.Add RGB(255, 2, 157)
        Colors.Add RGB(104, 61, 59)
        Colors.Add RGB(255, 116, 163)
        Colors.Add RGB(150, 138, 232)
        Colors.Add RGB(152, 255, 82)
        Colors.Add RGB(167, 87, 64)
        Colors.Add RGB(1, 255, 254)
        Colors.Add RGB(255, 238, 232)
        Colors.Add RGB(254, 137, 0)
        Colors.Add RGB(189, 198, 255)
        Colors.Add RGB(1, 208, 255)
        Colors.Add RGB(187, 136, 0)
        Colors.Add RGB(117, 68, 177)
        Colors.Add RGB(165, 255, 210)
        Colors.Add RGB(255, 166, 254)
        Colors.Add RGB(119, 77, 0)
        Colors.Add RGB(122, 71, 130)
        Colors.Add RGB(38, 52, 0)
        Colors.Add RGB(0, 71, 84)
        Colors.Add RGB(67, 0, 44)
        Colors.Add RGB(181, 0, 255)
        Colors.Add RGB(255, 177, 103)
        Colors.Add RGB(255, 219, 102)
        Colors.Add RGB(144, 251, 146)
        Colors.Add RGB(126, 45, 210)
        Colors.Add RGB(189, 211, 147)
        Colors.Add RGB(229, 111, 254)
        Colors.Add RGB(222, 255, 116)
        Colors.Add RGB(0, 255, 120)
        Colors.Add RGB(0, 155, 255)
        Colors.Add RGB(0, 100, 1)
        Colors.Add RGB(0, 118, 255)
        Colors.Add RGB(133, 169, 0)
        Colors.Add RGB(0, 185, 23)
        Colors.Add RGB(120, 130, 49)
        Colors.Add RGB(0, 255, 198)
        Colors.Add RGB(255, 110, 65)
        Colors.Add RGB(232, 94, 190)
    
        Selection.HomeKey Unit:=wdStory
        ' take this out of you don't want to reset the colors
        ActiveDocument.Range.Font.Color = 0
        ActiveDocument.Range.Font.Bold = False
    
        With Selection.Find
            .ClearFormatting
            .Text = "<[0-9]{5}>"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = True
    
        End With
    
        While Selection.Find.Execute
            ' believe it or not, if this entry is not already in the dictionary, it won't give an error
            ' it will add the entry and perform the operation, like a true friend
            Fives(Selection.Text) = Fives(Selection.Text) + 1
        Wend
    
        C = 1
        With Selection.Find
            .ClearFormatting
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            With .Replacement
                .Text = ""
                .ClearFormatting
                .Font.Bold = True
                .Font.Color = Colors(C)
            End With
        End With
    
        For Each F In Fives.Keys
            DoEvents
            If Fives(F) > 1 Then  ' not sure if you want only 5-digit numbers with at least one dup, if no, remove this
                Selection.HomeKey Unit:=wdStory
                With Selection.Find
                    .Text = F
                    .Replacement.Font.Color = Colors(C)
                    .Execute Replace:=wdReplaceAll
                End With
                C = C + 1
                If C > MaxColors Then C = 1
            End If
        Next F
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      更灵活的方法是从我之前的答案中获取代码并替换:

      Dim StrFnd As String, i As Long
      

      与:

      Dim StrFnd As String, i As Long, s As Long, r As Long, g As Long, b As Long, c As Long
      s = 56: c = s ^ (1 / 3)
      

      并替换:

      i = i Mod 15
      If i < 7 Then
        i = i + 1
      Else
        i = i + 2
      End If
      .Font.ColorIndex = i
      

      与:

      r = (i Mod c) * 256 / (c - 1)
      g = (Int(i / c) Mod c) * 256 / (c - 1)
      b = (Int(i / c ^ 2) Mod c) * 256 / (c - 1)
      .Font.Color = RGB(r, g, b)
      

      这样,只需更改 's = 56' 中的 '56' 即可预先指定要使用的最大颜色数。然后代码将从可用的 16,777,216 种颜色中自动选择这些颜色。自然,随着您增加/减少 's' 值,自动选择的颜色系列也会有所不同。

      要了解使用这种方法可以获得的颜色,请在新文档中尝试以下操作:

      Sub Demo()
      Dim i As Long, s As Long, r As Long, g As Long, b As Long, c As Long
      With ActiveDocument.Range
        .Text = vbNullString
        s = 56: c = s ^ (1 / 3)
        For i = 1 To s
          r = (i Mod c) * 256 / (c - 1)
          g = (Int(i / c) Mod c) * 256 / (c - 1)
          b = (Int(i / c ^ 2) Mod c) * 256 / (c - 1)
          .InsertAfter vbTab & ChrW(&H2588)
          .Characters.Last.Previous.Font.Color = RGB(r, g, b)
        Next
      End With
      End Sub
      

      【讨论】:

      • 谢谢!我试试看
      猜你喜欢
      • 2022-01-24
      • 2013-05-22
      • 2020-06-04
      • 2017-02-08
      • 2019-01-22
      • 1970-01-01
      • 2018-10-12
      • 2013-09-08
      • 2014-10-01
      相关资源
      最近更新 更多