Highlighting rows with random colors if there are duplicates in one column

korak30

New Member
Joined
Jun 15, 2015
Messages
18
Hello, I'd like to highlight rows with random colors if there are duplicates (anywhere between 3-10) in one of the columns. My data set looks like this:

1_800_flowerscom1 Old Country Rd Ste 500Carle PlaceNY11514-1847United States
1_800_flowerscom1 Old Country Rd Ste 500Carle PlaceNY11514-1847USA
1_automotive_group950 Echo LaneHoustonTX77024-2756United States
1_automotive_group800 Gessner Rd Ste 500HoustonTX77024-4498United States
1_automotive_group950 Echo LaneHoustonTX77024-2756United States of America
1_chambers_court_family_garden_law
1_chambers_court_family_garden_law1 Garden Court TempleLondonEC4Y 9BJUnited Kingdom

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>

Basically I'd like to highlight the rows, for duplicate values in column A, with a unique color. The reason I need a random color is because there are 17000 rows.

I've tried some basic conditional formatting but that's not working.

Any help is much appreciated. Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I've tried something along the lines of the following
Sub test()
Worksheets("Remaining Groups").Range("A1:AC17265").Select


Do While ActiveCell.Text <> Empty
Do While ActiveCell = True
ActiveCell.Offset(0, 5).Resize(2, -5).Interior.Color = RGB(Int((255 * Rnd)), Int((255 * Rnd)), Int((255 * Rnd)))
Loop
ActiveCell.Offset(1, 0).Select
Loop
End Sub

But it's not working.. anybody? Thank you so much!
 
Upvote 0
Korak30,

Have a try of this code. It will only highlight duplicates in Col A.

Code:
Sub ColourDuplicates()
Dim Rng As Range
Dim Cel As Range
Dim Cel2 As Range
Dim Colour As Long




Set Rng = Worksheets("Sheet1").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Rng.Interior.ColorIndex = xlNone
Colour = 6
For Each Cel In Rng


If WorksheetFunction.CountIf(Rng, Cel) > 1 And Cel.Interior.ColorIndex = xlNone Then
Set Cel2 = Rng.Find(Cel.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchDirection:=xlNext)
    If Not Cel2 Is Nothing Then
        Firstaddress = Cel2.Address
        Do
        Cel.Interior.ColorIndex = Colour
        Cel2.Interior.ColorIndex = Colour
            Set Cel2 = Rng.FindNext(Cel2)
        
        Loop While Firstaddress <> Cel2.Address
    End If




Colour = Colour + 1


End If
Next


End Sub
 
Upvote 0
Scott,

That was wonderful!!

Could you guide me on how to modify this to highlight the entire row?

Thank you so much!
 
Upvote 0
Do you want the "entire row" or just the columns with data in them? If so, what is the last col?
 
Upvote 0
The columns with the data. I'm sorry I should have specified! The column with the repetitions to be identified is column B, but the data ranges from column A to Z.

Thanks!
 
Upvote 0
Try-

Code:
Sub ColourDuplicates2()
Dim Rng As Range
Dim Cel As Range
Dim Cel2 As Range
Dim Colour As Long




Set Rng = Worksheets("Sheet1").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Rng.Interior.ColorIndex = xlNone
Colour = 6


For Each Cel In Rng

If WorksheetFunction.CountIf(Rng, Cel) > 1 And Cel.Interior.ColorIndex = xlNone Then
Set Cel2 = Rng.Find(Cel.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchDirection:=xlNext)
    If Not Cel2 Is Nothing Then
        Firstaddress = Cel2.Address
        Do
        Cel.Offset(0,-1).Resize(1,26).Interior.ColorIndex = Colour
        Cel2.Offset(0,-1).Resize(1,26).Interior.ColorIndex = Colour

           Set Cel2 = Rng.FindNext(Cel2)
        
        Loop While Firstaddress <> Cel2.Address
    End If




Colour = Colour + 1


End If
Next


End Sub
 
Upvote 0
Yes it still says Subscript out of Range again, unfortunately.

I've also stumbled upon another challenge.

The data I have is below:
51_800_flowerscomLegal GSO SFDC1 Old Country Rd Ste 500Carle PlaceNY11514-1847United States
61_800_flowerscomInterAction1 Old Country Rd Ste 500Carle PlaceNY11514-1847USA
71_automotive_groupLegal GSO SFDC950 Echo LaneHoustonTX77024-2756United States
81_automotive_groupEPM SFDC800 Gessner Rd Ste 500HoustonTX77024-4498United States
91_automotive_groupInterAction950 Echo LaneHoustonTX77024-2756United States of America
101_chambers_court_family_garden_lawInterAction
111_chambers_court_family_garden_lawLegal GSO SFDC1 Garden Court TempleLondonEC4Y 9BJUnited Kingdom
121_chancery_laneLegal GSO SFDC1 Chancery LaneLondon WC2A 1LFUnited Kingdom
131_chancery_laneInterAction
141_court_hareLegal GSO SFDC1 Hare Court TempleLondonEC4Y 7BEUnited Kingdom
151_court_hareInterAction
161_court_pumpInterAction1 TempleLondon EC4Y 7ABUnited Kingdom
171_court_pumpLegal GSO SFDC1 Pump Court Chambers Elm Court, TempleLondon EC4Y 7AHUnited Kingdom
181031_group_taxLegal GSO SFDC - EliteRichmondVA23235US
191031_group_taxInterAction100 Corporate Dr Ste 201TrumbullCT06611-6343United States
2011_bench_kings_walkInterAction3 Park CourtLeeds LS1 2QHUnited Kingdom
2111_bench_kings_walkLegal GSO SFDC3 Park CourtLeeds LS1 2QHUnited Kingdom
2212_benchwalk_kingsLegal GSO SFDC12 King's Benchwalk TempleLondonEC4Y 7ELUnited Kingdom
2312_benchwalk_kingsInterAction12 King's Benchwalk TempleLondonEC4Y 7ELUnited Kingdom

<colgroup><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

<colgroup><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

I now need to match columns D, E and G, and report how many matches, 1,2 or 3 there are for each set of duplicates.

Is this something that can be done?

I'm sorry I'm asking so much, I'm pretty stressed about this. :(
 
Upvote 0
Is there anything special/different at row 106?

Is the code doing everything right up to this point?

In your post #9 can you give an example of the matching you are referring to.
 
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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