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!
 
Nothing different in Row 106, really. Actually the code from #8 does not run at all and gives a subscript out of range error before starting any coloring.

So the matching is something like this:

I'm trying to produce a column to the right of the data that has the number of matches.
So H5 and H6 should say "3"
H7, H8 and H9 should say "1" because only "Houston" matches in all 3
H10 and H11 should give "0"
.
.
.
H20 and H21 should say "3"

This is based on comparing columns D, E, and G within each set of duplicates (and there can be none or many duplicates)
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Nothing different in Row 106, really. Actually the code from #8 does not run at all and gives a subscript out of range error before starting any coloring.

OK I can see an error in that code. 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("B1:B" & Range("B" & 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

As a matter of interest (RE: row 106) step through this code (using F8) and see what the value of lr (by hovering over it), is

Code:
Sub lr()

dim lr as long

lr= Range("B" & Rows.Count).End(xlUp).Row

End sub
 
Last edited:
Upvote 0
Hi, the code stops at A105 again with an runtime error 9 supscript out of range error. The value of lr is 0.
 
Upvote 0
Oh! There might be an issue but I'm not sure. The values in cell B107 and B108, just below where the code stops, don't have any underscores. I'm not sure if this helps though.
 
Upvote 0
When you say the code stops at row 106, I am assuming the duplicates are highlighted in colour (above that point)?

Can you post a screenshot of rows 100 to 110 after the code has been run? Check out here for instructions- http://www.mrexcel.com/forum/about-board/508133-attachments.html


With matching your duplicates I am assuming that USA=United States=United States of America????
 
Upvote 0
When you say the code stops at row 106, I am assuming the duplicates are highlighted in colour (above that point)?

Can you post a screenshot of rows 100 to 110 after the code has been run? Check out here for instructions- http://www.mrexcel.com/forum/about-board/508133-attachments.html
Excel Workbook
ABIJKLMNOTWXYZ
999866_phillipsEPM SFDC3010 Briarpark DrHoustonTX77042-3706USA07842344705656Master
1009966_phillipsInterAction3010 Briarpark DrHoustonTX77042-3706United States07842344705656Merge
1011007_bedford_rowLegal GSO SFDC7 Bedford RowLondonWC1R 4BUUnited KingdomMerge
1021017_bedford_rowInterAction6-7 Bedford RowLondonWC1R 4BSUnited Kingdom216015391Master
1031027_elevenInterActionSte 1000DallasTX75201United States616913935Merge
1041037_elevenLegal GSO SFDC1722 Routh St Ste 1000DallasTX75201-2506United States007347602Master
105104722_investmentsLegal GSO SFDC244 Fifth Ave #2272New YorkNY10001Merge
106105722_investmentsInterAction244 Fifth Ave Suite 2272New YorkNY10001United States of AmericaMaster
1071067kbwInterAction7 Temple King's Bench WalkLondonEC4Y 7DSUnited Kingdom423875595Master
1081077kbwLegal GSO SFDC7 King's Bench WalkLondonEC4Y 7DSUnited KingdomMerge
1091089_buildings_stoneInterActionLincoln's Inn Lde: 314 Chancery LaneLondonGreater LondonWC2A 3NNUnited KingdomMerge
1101099_buildings_stoneLegal GSO SFDCLincoln's Inn Lde: 314 Chancery LaneLondonWC2A 3NNUnited KingdomMaster
Remaining Groups


With matching your duplicates I am assuming that USA=United States=United States of America????
No, I'm just looking to match columns J,K, and M.

Thank you so much!
 
Upvote 0
Do rows 105 and 106 need to be there, since they "look" blank? Can we delete them?

Are all the entries in groups/sorted or are they randomly down the page?
 
Upvote 0
I tested the code on some mock-up data and it skipped the 'true' blank rows (ie was not coloured), based on Col B.

However, when I put a space in visually blank cells (ie not 'truely' blank) it colours the rows. Hence your cells B105 and B106 have spaces in them. This can also be tested using =LEN(B105). If it does not equal 0 then there are spaces within the cells that we can not see.

But this does not solve the question as to why the code does not proceed past the rows in question.

Hmmmmm......
 
Upvote 0
Do rows 105 and 106 need to be there, since they "look" blank? Can we delete them?

Are all the entries in groups/sorted or are they randomly down the page?

They actually are populated, but just with the same color as the shading is. I should have changed that, but for some reason my screenshot had the text a darker shade than the color, so the text was visible for rows 105 and 106.

They are all in groups, or at least should be, because I sorted them.
 
Upvote 0
Excel Workbook
ABIJKLMNOTWXYZ
1011007_bedford_rowLegal GSO SFDC7 Bedford RowLondonWC1R 4BUUnited KingdomMerge
1021017_bedford_rowInterAction6-7 Bedford RowLondonWC1R 4BSUnited Kingdom216015391Master
1031027_elevenInterActionSte 1000DallasTX75201United States616913935Merge
1041037_elevenLegal GSO SFDC1722 Routh St Ste 1000DallasTX75201-2506United States007347602Master
105104722_investmentsLegal GSO SFDC244 Fifth Ave #2272New YorkNY10001Merge
106105722_investmentsInterAction244 Fifth Ave Suite 2272New YorkNY10001United States of AmericaMaster
1071067kbwInterAction7 Temple King's Bench WalkLondonEC4Y 7DSUnited Kingdom423875595Master
1081077kbwLegal GSO SFDC7 King's Bench WalkLondonEC4Y 7DSUnited KingdomMerge
1091089_buildings_stoneInterActionLincoln's Inn Lde: 314 Chancery LaneLondonGreater LondonWC2A 3NNUnited KingdomMerge
1101099_buildings_stoneLegal GSO SFDCLincoln's Inn Lde: 314 Chancery LaneLondonWC2A 3NNUnited KingdomMaster
Remaining Groups
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,827
Members
449,470
Latest member
Subhash Chand

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