逐列突出显示重复项

我试图去通过列和突出显示列中的重复项。 我使用loggingmacros来了解我需要什么,但是我不知道如何跨越多列应用这一点。 突出显示所有列将不起作用,因为许多名称重复。 我需要找出一个名字是否在列表中重复多次。

这是迄今为止的代码:

Sub findDuplicates() Application.Goto Reference:="R3C18:R89C18" Application.Goto Reference:="R3C18:R88C18" Selection.FormatConditions.AddUniqueValues Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority Selection.FormatConditions(1).DupeUnique = xlDuplicate With Selection.FormatConditions(1).Font .Color = -16751204 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 10284031 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Range("R21").Select End Sub 

这是代码我已经通过B3:OA3我的范围内的每一列,并按颜色和字母sorting。 我的想法是,因为这个代码逐列sorting,我可以简单地添加它来突出显示它已经sorting的列中的重复项。 但是我不确定我会怎么做。

 Sub sortColorThenAlpha() 'sort by color then by alphabet Dim rngFirstRow As Range Dim rng As Range, rngSort As Range Dim ws As Worksheet Application.ScreenUpdating = False Set ws = ActiveSheet Set rngFirstRow = ws.Range("B3:OA3") For Each rng In rngFirstRow.Cells With ws.Sort Set rngSort = rng.Resize(86, 1) 'to row 88 .SortFields.Clear .SortFields.Add(rng, xlSortOnCellColor, xlAscending, , xlSortNormal). _ SortOnValue.Color = RGB(198, 239, 206) .SortFields.Add Key:=rng, SortOn:=xlSortOnValues, _ Order:=xlAscending, DataOption:=xlSortNormal .SetRange rngSort .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Next rng Application.ScreenUpdating = True End Sub 

这就是我正在看的。 黄色的条件格式是我想要应用到第3行和第88行之间的每一列。 在这里输入图像说明

VBA似乎不是必要的,因为下面的规则似乎有条件格式化:

 =A1=VLOOKUP(A1,A2:A$99,1,FALSE) Applies to: =$A$1:$J$99 =A2=VLOOKUP(A2,A$1:A1,1,FALSE) Applies to: =$A$2:$J$99 

参考调整适合。

如果我正确理解您的问题,您希望能够突出显示单个列中的重复项,并且希望能够将此格式自动应用于给定工作表中的所有列。 所以,如果埃及艳后出现一次,她就不会被突出显示,但是如果她出现在一个专栏里不止一次,她会的。

下面的代码就是这样做的。 我在第3行find最后一列。

 Sub HighlightDupesOneColumnAtATime() Dim ws As Worksheet Dim myColumn As Long Dim i As Integer Dim columnCount As Long Dim lastRow As Long Dim dupeColor As Long Set ws = ThisWorkbook.Sheets("Sheet1") columnCount = ws.Cells(3, ws.Columns.Count).End(xlToLeft).Column dupeColor = 9944516 For i = 1 To columnCount lastRow = ws.Cells(ws.Rows.Count, i).End(xlUp).Row Call HighlightDupesInRange(dupeColor, Cells(1, i).Resize(lastRow, 1)) ' it is easy to change the color of the ' highlighted duplicates if you want dupeColor = dupeColor + 15 Next i End Sub Sub HighlightDupesInRange(cellColor As Long, rng As Range) With rng .FormatConditions.Delete .FormatConditions.AddUniqueValues .FormatConditions(1).DupeUnique = xlDuplicate .FormatConditions(1).Interior.Color = cellColor .FormatConditions(1).StopIfTrue = False End With End Sub