Duplicates in different colors - VBA

illusionek

Board Regular
Joined
Jun 21, 2014
Messages
104
Hello

I have found below code online, which pretty much does what I need with one exception, I would like to exclude blank cells or cells with errors ie #N/A. The end goal is to highlight all duplicated values in different colors.

I am pretty sure I need to use IF statement but I tried couple different variations and nothing works.


VBA Code:
Sub Duplicates_Dif_Colors()
    Dim RG As Range
    Dim TT As String
    Dim CL As Range
    Dim CR As String
    Dim CP As Range
    Dim CD As Long
    Dim Cltn As Collection
    Dim J As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      TT = ActiveWindow.RangeSelection.AddressLocal
    Else
      TT = ActiveSheet.UsedRange.AddressLocal
    End If
    Set RG = Application.InputBox("Select the range of data:", "Duplicates with Colors", TT, , , , , 8)
    If RG Is Nothing Then Exit Sub
    CD = 2
    Set Cltn = New Collection
    For Each CL In RG
      On Error Resume Next
      Cltn.Add CL, CL.Text
      If Err.Number = 457 Then
        CD = CD + 1
        Set CP = Cltn(CL.Text)
        If CP.Interior.ColorIndex = xlNone Then CP.Interior.ColorIndex = CD
        CL.Interior.ColorIndex = CP.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
        MsgBox "Found excessive duplicates", vbCritical, "Duplicates with Colors"
        Exit Sub
      End If
      On Error GoTo 0
    Next
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I am sure someone can come up with a better way but in the meantime you can try this:

VBA Code:
    Dim RG As Range
    Dim TT As String
    Dim CL As Range
    Dim CR As String
    Dim CP As Range
    Dim CD As Long
    Dim Cltn As Collection
    Dim J As Long
    Dim bNoError As Boolean
    Dim bOKtoProcess As Boolean
    
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      TT = ActiveWindow.RangeSelection.AddressLocal
    Else
      TT = ActiveSheet.UsedRange.AddressLocal
    End If
    Set RG = Application.InputBox("Select the range of data:", "Duplicates with Colors", TT, , , , , 8)
    If RG Is Nothing Then Exit Sub
    CD = 2
    Set Cltn = New Collection
    
    For Each CL In RG
        bNoError = Not IsError(CL)
        bOKtoProcess = False
        If bNoError Then
            If CL <> "" Then
                bOKtoProcess = True
            End If
        End If
        
        If bOKtoProcess Then
            On Error Resume Next
            Cltn.Add CL, CL.Text
            If Err.Number = 457 Then
              CD = CD + 1
              Set CP = Cltn(CL.Text)
              If CP.Interior.ColorIndex = xlNone Then CP.Interior.ColorIndex = CD
              CL.Interior.ColorIndex = CP.Interior.ColorIndex
            ElseIf Err.Number = 9 Then
              MsgBox "Found excessive duplicates", vbCritical, "Duplicates with Colors"
              Exit Sub
            End If
            On Error GoTo 0
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top