简体   繁体   中英

Excel colour cell automatically according to cell content

In a Nutshell, what I would like to do is the equivalent of what you can do when you click the conditional formatting button but a bit more advanced.

Imagine you have a column with different values such as:

value1
value1
value2
value3
value3
value3
value1

etc..

What I want to do is assign a different colour for every different valueX. I know it's easy to do via the conditional formatting button, but say I don't know how many values I have in total... Could be valueX to valueY or to valueZ. So Since I don't know how many different values I have in the column I would like excel to figure it out and assign a random colour for each value found accordingly.

I thought of using the RGB values and increment the RGB number for each value like valueX = FFFF00 valueY = FF0000 valuez = 000000 or something like that,

but that would require VB scripting or whatever scripting language excel is using nowadays and that's far beyond my personal knowledge of Excel.

Could somebody point me to the right direction? Would that be hard to do in a script?

Thanks very much

I do something like this with VBA to conditionally color cells I select depending on their number value.

The easiest way would be like this:

Sub colorValues()

    For Each cell in Selection
        If cell.Value = "Value1" Then
            cell.Interior.Color = 65535
        ElseIf cell.Value = "Value2" Then
            cell.Interior.Color = 255
        ElseIf cell.Value = "Value3" Then
            cell.Interior.Color = 13762516
       End If
    Next cell

End Sub

Very basic, but you could expand on it and add multiple colors. The number values are related to colors and I just picked random ones.

This example assumes a fixed number of values. If you wanted to dynamically color the cells that will be a little harder. You will have to create an array of the unique values in the selection and assign a color to each corresponding value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM