How to highlight rows with different colors by groups of duplicates?

谁说胖子不能爱 提交于 2020-06-27 04:14:06

问题


How do I highlight rows with different colors by groups of duplicates?

I don't care about which colors are used per se, I just want the duplicate rows one color, and the next set of duplicates another color.

For example, if I wanted the '1s' green, the '2s' blue and so on. It goes up to 120 in my column.

Thank you.


回答1:


Try out this simple code and modify it per your needs. Its quite self explanatory,

Sub dupColors()
Dim i As Long, cIndex As Long
cIndex = 3
Cells(1, 1).Interior.ColorIndex = cIndex
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    If Cells(i, 1) = Cells(i + 1, 1) Then
        Cells(i + 1, 1).Interior.ColorIndex = cIndex
    Else
        If Cells(i + 1, 1) <> "" Then
            cIndex = cIndex + 1
            Cells(i + 1, 1).Interior.ColorIndex = cIndex
        End If
    End If
Next i
End Sub




回答2:


The solution by Gowtham is only specific to numbers and uses VBA. I have a simple workaround that works with any type of data and doesn't need VBA.

We could use another column that generates a unique value for all the duplicates using a formula and use the "Conditional Formatting > Color Scales" for that column. Screenshot below.

The formula that you can use is

"=ROW(INDEX(A$2:A$12,MATCH(A2,A$2:A$12,0)))"

In the above formula, A$2:A$12 is the range that we want to search for duplicates.

The formula basically searches for the first instance of the duplicate value in the given range and inputs the row number of that first instance.

P.S: In the above formula, the range "A$2:A$12" is a fixed range, using the above formula in a Table is much simpler as a Table Range is dynamic

One other benefit of using Table is that we can even sort the data to group the duplicate values together

=ROW(INDEX([Column1],MATCH(A2,[Column1],0)))



回答3:


Gowtham's answer is great, and I wouldn't have figured out the below without them! I had the same need for unique color assignment, however, I needed more variance than the 56 colors that colorindex provides, so I slightly modified Gowtham's code to provide a bit more variability by using RandBetween along with RGB to create randomized colors via randomized red, blue, and green values.

I kept the color range between 120 & 255, since some of the lower values could result in cells that were too dark to read, but you can certainly customize to your liking. The code below can certainly be improved upon, as I'm no expert, but it was able to obtain the 100+ colors needed.

EDIT: I will add that there is a possibility that RGB values could overlap. I just needed to color-code for visual aid; but if you will need strict unique color values, this code will not guarantee that.

Dim rCount, RandCol1, RandCol2, RandCol3, i As Long

rCount = Sheet1.Range("A" & Rows.Count).End(xlUp).Row

    For i = 1 To rCount
        If Sheet1.Cells(i, 1) = Sheet1.Cells(i + 1, 1) Then
            Sheet1.Cells(i + 1, 1).Interior.Color = RGB(RandCol1, RandCol2, RandCol3)
        Else
            If Sheet1.Cells(i + 1, 1) <> "" Then
                RandCol1 = WorksheetFunction.RandBetween(120, 255)
                RandCol2 = WorksheetFunction.RandBetween(120, 255)
                RandCol3 = WorksheetFunction.RandBetween(120, 255)
                Sheet1.Cells(i + 1, 1).Interior.Color = RGB(RandCol1, RandCol2, RandCol3)
            End If
        End If
    Next i


来源:https://stackoverflow.com/questions/44187293/how-to-highlight-rows-with-different-colors-by-groups-of-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!