感谢您对此的帮助!
我能够通过一种稍微不同的方式来解决这个问题。这是每组重复项具有 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