How to highlight duplicates in column that are not blanks?

前端 未结 1 1505
感情败类
感情败类 2021-01-24 04:06

I want to highlight all the duplicates of a concatenated string in column I and provide an error message if there are any duplicates highlighted. However, there are several blan

相关标签:
1条回答
  • 2021-01-24 05:10

    If you want to use VBA this should work for you.

        Dim mydict As Object
        Dim iter As Long
        Dim lastrow As Long
        Dim errmsg As String
        Dim key As Variant
    
        Set mydict = CreateObject("Scripting.Dictionary")
    
        ' If you want to use early binding add in the Microsoft Scripting Runtime reference then: Set mydict = new dictionary
    
        With ActiveSheet
            lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
            For iter = 2 To lastrow
                If Not mydict.exists(.Cells(iter, "A").Value) Then
                    mydict.Add .Cells(iter, "A").Value, False
                Else
                    .Cells(iter, "A").Interior.ColorIndex = 36
                    mydict(.Cells(iter, "A").Value) = True 'Keep track of which values are repeated
                End If
            Next
        End With
        errmsg = "Duplicate Values: "
        For Each key In mydict
            If mydict(key) = True Then 'Dupes
                If Not errmsg = "Duplicate Values: " Then 'No extra comma
                    errmsg = errmsg & ", " & key
                Else
                    errmsg = errmsg & " " & key
                End If
            End If
        Next
    
        MsgBox errmsg
    
    0 讨论(0)
提交回复
热议问题