Code to color dupes in row 1 colors only one cell of dupe

后端 未结 1 1038
离开以前
离开以前 2021-01-26 16:13

I want to check for dupes in Row One

The Code below works fine for column ranges like:

 myrng = Range(\"C2:C\" & Range(\"C65536\").End(x         


        
1条回答
  •  终归单人心
    2021-01-26 17:01

    Use Collections to get the unique values, then loop through the collections to highlight the duplicates.

    Sub UsingCollection()
        Dim cUnique As Collection
        Dim Rng As Range
        Dim Cell As Range
        Dim sh As Worksheet
        Dim vNum As Variant
        Dim LstCol As Long
        Dim c As Long, clr As Long, x, r As Range
    
        Set sh = ThisWorkbook.Sheets("Nodes")
        With sh
    
            LstCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            Set Rng = .Range(.Cells(1, 1), .Cells(1, LstCol))
            Set cUnique = New Collection
            Rng.Interior.ColorIndex = xlNone
            clr = 3
    
            On Error Resume Next
            For Each Cell In Rng.Cells
                cUnique.Add Cell.Value, CStr(Cell.Value)
            Next Cell
            On Error GoTo 0
    
            For Each vNum In cUnique
                For c = 1 To LstCol
                    Set r = .Cells(1, c)
                    x = Application.WorksheetFunction.CountIf(.Range(.Cells(1, 1), .Cells(1, c)), r)
                    If r = vNum Then
                        If x > 1 Then
                            r.Interior.ColorIndex = clr
                        End If
                    End If
                Next c
                clr = clr + 1
            Next vNum
    
        End With
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题