逐列突出显示重复项

时间:2013-07-09 14:50:59

标签: excel vba excel-vba

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

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

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

这是我的代码,它遍历我B2: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行之间的每一列。enter image description here

2 个答案:

答案 0 :(得分: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

参考调整到适合。

答案 1 :(得分: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