Highlighting rows random colors if there duplicates one column - PART 2

mortenkl

New Member
Joined
Apr 10, 2015
Messages
8
Hello all,

Yesterday I got a lot of help from this thread:

http://www.mrexcel.com/forum/excel-...om-colors-if-there-duplicates-one-column.html

I managed to get my spreadsheet highlighting working using the code provided in that thread:

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

Set Rng = Worksheets("NY Fakturering").Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
Rng.Interior.ColorIndex = xlNone
Colour = 19

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, 31).Interior.ColorIndex = Colour
        Cel2.Offset(0, -1).Resize(1, 31).Interior.ColorIndex = Colour

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

Colour = Colour + 1

End If

Next

End Sub

I'm very satisfied with the result, but thought: Would it be possible to only switch between two colours? Like:

firstColour = 19
secondColour = 20

Any help adjusting the code is much appreciated, if possible.

Sorry if this should be in the original thread, but it's about 6 months old. Feel free to merge them if needed.

Thank you.

/ Morten
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Like this?:

Code:
Option Explicit
Sub ColourDuplicates()

    Dim Rng As Range
    Dim Cel As Range
    Dim Cel2 As Range
    Dim Colour As Long
    Dim Firstaddress As String
    
    Set Rng = Worksheets("NY Fakturering").Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
    Rng.Interior.ColorIndex = xlNone
    'Colour = 19
    
    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
                    If Colour = 0 Or Colour = 19 Then
                        Colour = 20
                    Else
                        Colour = 19
                    End If
                    Do
                    Cel.Offset(0, -1).Resize(1, 31).Interior.ColorIndex = Colour
                    Cel2.Offset(0, -1).Resize(1, 31).Interior.ColorIndex = Colour
            
                       Set Cel2 = Rng.FindNext(Cel2)
                    
                    Loop While Firstaddress <> Cel2.Address
                    
                End If
            
            'Colour = Colour + 1
    
        End If
    
    Next Cel
    
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,835
Members
449,471
Latest member
lachbee

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