区分excel中的重复值

时间:2012-12-01 05:23:27

标签: excel duplicates

我需要具有相同列颜色的特定值的所有副本。这将帮助我找到特定值,相应列值等的重复数量。 请找Sample Excel。如果我进行条件格式化,突出显示重复项,这将为所有重复项提供相同的颜色,而不考虑值。这不会解决目的。 还建议在excel中区分重复值的其他方法(如果有的话)。

1 个答案:

答案 0 :(得分:2)

没有办法用条件格式做你想要的,但我认为下面的宏会做你想要的。它构建了一个唯一值的字典,并为每个值分配一个随机颜色,然后匹配并重复使用任何重复值。

' Base function that can be used for different columns.
Sub colorDistinctInColumn(ByVal columnLetter As String)
    Dim lastRow As Integer
    Dim i As Integer
    Dim dict
    Dim currentCell As Range
    Dim columnNumber As Integer

    ' Create a dictionary to hold the value/colour pairs
    Set dict = CreateObject("Scripting.Dictionary")

    ' Find the last-used cell in the column of interest
    lastRow = ActiveSheet.Columns(columnLetter).Cells.Find( _
        "*", _
        SearchOrder:=xlByRows, _
        LookIn:=xlValues, _
        SearchDirection:=xlPrevious).Row

    ' Pick columnNumber using the given column letter
    columnNumber = ActiveSheet.Columns(columnLetter).Column

    ' Loop through all of the cells
    For i = 1 To lastRow
        Set currentCell = ActiveSheet.Cells(i, columnNumber)

        ' See if we've already come across the current value
        If Not dict.exists(currentCell.Value) Then

            ' This value has not been encountered yet,
            ' so store it and assign a random colour
            dict.Add currentCell.Value, RGB(Rnd * 255, Rnd * 255, Rnd * 255)
        End If

        ' Set the colour of the current cell to whichever colour
        ' has been stored for the current cell's value
        currentCell.Interior.Color = dict(currentCell.Value)
    Next i

    Set dict = Nothing
End Sub

' Actual Macro that will run in Excel
Sub colorDistinctInColumnA()
    Call colorDistinctInColumn("A")
End Sub