+ Reply to Thread
Results 1 to 8 of 8

Highlight duplicate cell values in a row with different color code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    9

    Highlight duplicate cell values in a row with different color code

    Not even sure if it is possible:

    There is a dynamic table (the range may increase or decrease by row or column.
    Under each header, there are values.
    Per row, if there are duplicate values, then the cells should be highlighted by different colors.

    Example: Header 1 and 2 has A and A duplicate for row 2 (should be highlighted with one random color). Again, Header 4 and 5 have C as duplicate so the cells should be colored with a different color.
    Image attached for suggestion.

    Any color is fine as long as all the duplicates/triplicates are highlighted with similar sets of colors by row (NOT BY COLUMN).


    Header1 Header2 Header3 Header4 Header5 Header6 Header7
    A A B C C 1 1
    V B C C 1 2 1
    1 3 C D V 1 Q
    123.jpg


    Thanks a LOT in advance.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Highlight duplicate cell values in a row with different color code

    See how the macro can do the job
    There is some limits: No more than 10 different duplicate
    No more than 190 cells per row
    Option Explicit
    
    Sub CotlorTr()
    Dim I  As Integer, J As Integer, K As Integer
    Dim ColorLst
    Dim ObjDic   As Object
    Set ObjDic = CreateObject("Scripting.Dictionary")
    
        ColorLst = Array(192, 255, 49407, 65535, 5296274, 5287936, 15773696, 12611584, 6299648, 10498160)
        With ObjDic
            For I = 2 To Cells(Rows.Count, 1).End(3).Row
                .RemoveAll
                .Item(Cells(I, 1).Value) = 1
                K = 0
                For J = 2 To Cells(I, Columns.Count).End(xlToLeft).Column
                    If (.exists(Cells(I, J).Value)) Then
                        If (.Item(Cells(I, J).Value) < 192) Then
                            K = K + 1
                            Cells(I, J).Interior.Color = ColorLst(K)
                            Cells(I, .Item(Cells(I, J).Value)).Interior.Color = ColorLst(K)
                        Else
                            Cells(I, J).Interior.Color = .Item(Cells(I, J).Value)
                        End If
                    Else
                        .Item(Cells(I, J).Value) = J
                    End If
                Next J
            Next I
        End With
    End Sub
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    02-21-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    9

    Re: Highlight duplicate cell values in a row with different color code

    Dear PCI, you are a genius.
    But there is a small problem.
    For example, in row 4 (in attached image), for the "C"s, the call colorsDF.JPG should be same.

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Highlight duplicate cell values in a row with different color code

    Are the values exactly the same ...
    Can you copy and paste the value from B4 to others and see the result
    Can you attach a short excel sample ?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Highlight duplicate cell values in a row with different color code

    Try
    Sub test()
        Dim i As Long, r As Range, e, n As Long, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        With Cells(1).CurrentRegion
            .Interior.ColorIndex = xlNone
            For i = 2 To .Rows.Count
                For Each r In .Rows(i).Cells
                    If Not dic.exists(r.Value) Then
                        Set dic(r.Value) = r
                    Else
                        Set dic(r.Value) = Union(dic(r.Value), r)
                    End If
                Next
                n = 2
                For Each e In dic
                    If dic(e).Count > 1 Then
                        n = n + 1: If n > 49 Then n = 3
                        dic(e).Interior.ColorIndex = n
                    End If
                Next
                dic.RemoveAll
            Next
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    02-21-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    9

    Re: Highlight duplicate cell values in a row with different color code

    Thank you jindon.

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Highlight duplicate cell values in a row with different color code

    Yes there was a "bug" .. try next code
    Option Explicit
    
    Sub CotlorTr()
    Dim i  As Integer, J As Integer, K As Integer
    Dim ColorLst
    Dim ObjDic   As Object
    Set ObjDic = CreateObject("Scripting.Dictionary")
        ObjDic.CompareMode = 1
        ColorLst = Array(192, 255, 49407, 65535, 5296274, 5287936, 15773696, 12611584, 6299648, 10498160)
        With ObjDic
            For i = 2 To Cells(Rows.Count, 1).End(3).Row
                .RemoveAll
                .Item(Cells(i, 1).Value) = 1
                K = 0
                For J = 2 To Cells(i, Columns.Count).End(xlToLeft).Column
                    If (.exists(Cells(i, J).Value)) Then
                        If (.Item(Cells(i, J).Value) < 192) Then
                            K = K + 1
                            Cells(i, J).Interior.Color = ColorLst(K)
                            Cells(i, .Item(Cells(i, J).Value)).Interior.Color = ColorLst(K)
                            .Item(Cells(i, J).Value) = ColorLst(K)          '  NEW  STATEMENT
                        Else
                            Cells(i, J).Interior.Color = .Item(Cells(i, J).Value)
                        End If
                    Else
                        .Item(Cells(i, J).Value) = J
                    End If
                Next J
            Next i
        End With
    End Sub
    Last edited by PCI; 10-08-2017 at 05:29 AM.

  8. #8
    Registered User
    Join Date
    02-21-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    9

    Re: Highlight duplicate cell values in a row with different color code

    Thank you PCI. Big help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] highlight duplicate values in their own row even if values aren't adjacent
    By terryhenderson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2016, 01:20 PM
  2. Highlight Duplicate Values
    By voniom in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-18-2015, 08:48 AM
  3. [SOLVED] Conditional Formatting highlight duplicate values IF cell doesn't start with "XX"
    By deneh in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-16-2015, 04:44 AM
  4. Cell color is not changing for duplicate values using VBA
    By kvramana82 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-29-2013, 05:35 AM
  5. Highlight duplicate values
    By abibayley in forum Excel General
    Replies: 3
    Last Post: 04-23-2012, 12:03 PM
  6. Need help on how to highlight duplicate values
    By avadakedava in forum Excel General
    Replies: 3
    Last Post: 04-10-2012, 11:27 AM
  7. Highlight duplicate row values
    By Brian Tam in forum Excel General
    Replies: 5
    Last Post: 08-27-2010, 04:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1