Highlight Duplicates with Alternate Colors

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello i have data from column A to BG, i want to check duplicates based on values in columns R, AH, AJ, AN and AO. Row range is dyanmic one. If the mentioned columns values are duplicated, the VBA has to highlight the entire rows with a color. Similarly next set of duplicates has to be highlighted with another color. Next set with same as first color and so on. Data listed in ascending order so duplicates are always in next rows it wont exist in anywhere else. refer the example for more info. If the data in the mentioned column has no duplicates then leave it as it is. It has to highlight only the duplicated rows. If the color is in RGB format i can change later if required. Thank you.

Book1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBG
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Header27Header28Header29Header30Header31Header32Header33Header34Header35Header36Header37Header38Header39Header40Header41Header42Header43Header44Header45Header46Header47Header48Header49Header50Header51Header52Header53Header54Header55Header56Header57Header58Header59
2AAAeBBBCCCCCCDDD
3AAAeeBBBCCCCCCDDD
4AAAreBBBCCCCCCDDD
5BBBgCCCDDDEEEFFF
6GGGrHHHIIIJJJKKK
7GGGrHHHIIIJJJKKK
8HHHIIIJJJKKKLLL
9HHHIIIJJJKKKLLL
10MMMNNNOOOPPPQQQ
11NNNPPPQQQRRRSSS
12NNNPPPQQQRRRSSS
Sheet4
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hello i have data from column A to BG, i want to check duplicates based on values in columns R, AH, AJ, AN and AO. Row range is dyanmic one. If the mentioned columns values are duplicated, the VBA has to highlight the entire rows with a color. Similarly next set of duplicates has to be highlighted with another color. Next set with same as first color and so on. Data listed in ascending order so duplicates are always in next rows it wont exist in anywhere else. refer the example for more info. If the data in the mentioned column has no duplicates then leave it as it is. It has to highlight only the duplicated rows. If the color is in RGB format i can change later if required. Thank you.

Book1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBG
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Header27Header28Header29Header30Header31Header32Header33Header34Header35Header36Header37Header38Header39Header40Header41Header42Header43Header44Header45Header46Header47Header48Header49Header50Header51Header52Header53Header54Header55Header56Header57Header58Header59
2AAAeBBBCCCCCCDDD
3AAAeeBBBCCCCCCDDD
4AAAreBBBCCCCCCDDD
5BBBgCCCDDDEEEFFF
6GGGrHHHIIIJJJKKK
7GGGrHHHIIIJJJKKK
8HHHIIIJJJKKKLLL
9HHHIIIJJJKKKLLL
10MMMNNNOOOPPPQQQ
11NNNPPPQQQRRRSSS
12NNNPPPQQQRRRSSS
Sheet4
Will duplicate rows always be consecutive? (one after another)
 
Upvote 0
One last question, should all row content match?

I mean what if AH2=JJJ, AH3=JJJ but AJ2=CCC, AJ3=KKK? I mean AH rows are matching but AJ rows not. Is it a match?
Or should all R, AH, AJ, AN and AO match at the same time?
 
Upvote 0
I mean what if AH2=JJJ, AH3=JJJ but AJ2=CCC, AJ3=KKK? I mean AH rows are matching but AJ rows not. Is it a match?
Or should all R, AH, AJ, AN and AO match at the same time?
Hey, All the columns R, AH, AJ, AN and AO should match at same time. If even one column data is not matching then no need to consider it as duplicate. Hope it helps.
 
Upvote 0
Then this will work for you:
VBA Code:
Sub test()
  Dim myRange As Variant, colour As String
  
  colour = "Grey"
  myRange = Intersect(ActiveSheet.UsedRange, Range("A:AO"))
  ReDim Preserve myRange(1 To UBound(myRange), 1 To UBound(myRange, 2) + 1)
  
  For r = 2 To UBound(myRange) - 1
    If myRange(r + 1, 18) = myRange(r, 18) And myRange(r + 1, 34) = myRange(r, 34) And myRange(r + 1, 36) = myRange(r, 36) And myRange(r + 1, 40) = myRange(r, 40) And myRange(r + 1, 41) = myRange(r, 41) Then
      myRange(r, UBound(myRange, 2)) = colour
      myRange(r + 1, UBound(myRange, 2)) = colour
    Else
      colour = IIf(colour = "Grey", "Blue", "Grey")
    End If
  Next
  
  For i = 2 To UBound(myRange)
    Select Case myRange(i, UBound(myRange, 2))
    Case "Grey"
      Rows(i).Interior.Color = RGB(166, 166, 166)
    Case "Blue"
      Rows(i).Interior.Color = RGB(142, 169, 219)
    End Select
  Next
End Sub
 
Upvote 0
A slightly more efficient version could be:
VBA Code:
Sub test()
  Dim myRange As Variant, colour As String
 
  colour = RGB(166, 166, 166)
  myRange = Intersect(ActiveSheet.UsedRange, Range("A:AO"))
  ReDim Preserve myRange(1 To UBound(myRange), 1 To UBound(myRange, 2) + 1)
 
  For r = 2 To UBound(myRange) - 1
    If myRange(r + 1, 18) = myRange(r, 18) And myRange(r + 1, 34) = myRange(r, 34) And myRange(r + 1, 36) = myRange(r, 36) And myRange(r + 1, 40) = myRange(r, 40) And myRange(r + 1, 41) = myRange(r, 41) Then
      myRange(r, UBound(myRange, 2)) = colour
      myRange(r + 1, UBound(myRange, 2)) = colour
    Else
      colour = IIf(colour = RGB(166, 166, 166), RGB(142, 169, 219), RGB(166, 166, 166))
      myRange(r + 1, UBound(myRange, 2)) = xlNone
    End If
  Next
 
  For i = 2 To UBound(myRange)
    Rows(i).Interior.Color = myRange(i, UBound(myRange, 2))
  Next
End Sub
 
Upvote 0
This is nowhere near as neat and tidy as @Flashbond 's version, but I think it does the alternative colouring closer to what you wanted in your original post?
VBA Code:
Option Explicit
Sub AltColors()
    Dim Col_One As Long, Col_Two As Long
    Col_One = RGB(208, 208, 206)
    Col_Two = RGB(132, 151, 176)
    
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ change to actual sheet name
    ws.UsedRange.Offset(1).Interior.Color = xlNone
    
    Dim LCol As Long, LRow As Long
    LCol = ws.Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    LRow = ws.Cells(Rows.Count, "R").End(xlUp).Row
    
    Dim i As Long, z As Long, ArrIn, ArrOut
    z = 1
    ArrIn = ws.Range("R2:R" & LRow + 1)
    ReDim ArrOut(1 To UBound(ArrIn, 1), 1 To 1)
    
    If ArrIn(1, 1) = ArrIn(2, 1) Then ArrOut(1, 1) = z
    For i = 2 To UBound(ArrIn, 1) - 1
        If ArrIn(i, 1) = ArrIn(i - 1, 1) Or ArrIn(i, 1) = ArrIn(i + 1, 1) Then
            If ArrIn(i, 1) <> ArrIn(i - 1, 1) Then
                If z = 1 Then z = 2 Else z = 1
            End If
            ArrOut(i, 1) = z
        End If
    Next i
    ws.Cells(2, LCol).Resize(UBound(ArrOut, 1)).Value = ArrOut
    
    Dim c As Range
    For Each c In ws.Range(ws.Cells(2, LCol), ws.Cells(LRow, LCol))
        Select Case c.Value
            Case Is = 1
                c.Offset(, -(LCol - 1)).Resize(, 59).Interior.Color = Col_One
            Case Is = 2
                c.Offset(, -(LCol - 1)).Resize(, 59).Interior.Color = Col_Two
        End Select
    Next c
    ws.Columns(LCol).EntireColumn.ClearContents
End Sub
 
Upvote 0
This is nowhere near as neat and tidy as @Flashbond 's version,
And maybe even faster :cool: Just joking, I haven't tested. I don't care if its slower :) thanks for your nice compliments @kevin9999

For my code, another best practice would be to change colour variable type from String to Long, since RGB() value is a Long type.
VBA Code:
Dim myRange As Variant, colour As Long
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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