1

我正在尝试浏览列并突出显示列中的重复项。我使用记录宏来了解我需要什么,但我不确定如何在许多列中应用它。突出显示所有列将不起作用,因为许多名称重复。我需要找出一个名称是否在列表中重复多次。

这是我到目前为止的代码:

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 范围内的每一列,并按颜色和字母排序。我的想法是,因为此代码逐列并排序,我可以简单地添加到它以突出显示它已经排序的列中的重复项。但我不确定我会怎么做。

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 行之间的每一列的格式。在此处输入图像描述

4

2 回答 2

0

如果我正确理解您的问题,您希望能够突出显示单个列中的重复项,并且您希望能够自动将此格式应用于给定工作表中的所有列。因此,如果 Cleopatra 在几列中出现一次,她将不会被突出显示,但如果她在单个列中出现多次,她会。

下面的代码就是这样做的。我通过在第 3 行中查找值来找到最后一列。

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
于 2013-07-09T18:37:52.667 回答
0

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

参考调整以适应。

于 2013-07-09T18:13:55.157 回答