简体   繁体   English

根据单元格内容自动生成Excel彩色单元格

[英]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. 我要做的是为每个不同的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. 我知道通过条件格式按钮很容易做到,但是说我不知道​​我总共有多少个值...可能是valueX到valueY或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. 因此,由于我不知道该列中有多少个不同的值,所以我希望Excel能够将其找出并为找到的每个值分配一个随机的颜色。

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, 我想到了使用RGB值并为每个值增加RGB数量,例如valueX = FFFF00 valueY = FF0000 valuez = 000000或类似的东西,

but that would require VB scripting or whatever scripting language excel is using nowadays and that's far beyond my personal knowledge of Excel. 但这需要VB脚本或当今使用的excel脚本语言,这远远超出了我对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. 我用VBA做这样的事情来有条件地给我根据它们的数值选择的颜色。

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. 您将必须在选择中创建唯一值的数组,并为每个对应的值分配颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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