简体   繁体   English

VBA 颜色单元 取决于值

[英]VBA Color Cells Depending on the value

I would like to color cells depending on their value.我想根据它们的值对单元格进行着色。 If the value is 0, the cell should be green, if 1 then red.如果值为 0,则单元格应为绿色,如果为 1,则为红色。 The whole thing should be a progression so the closer the value is to 1 the redder the cell should be.整个事情应该是一个进步,所以值越接近 1,单元格应该越红。

Is there a suitable function for this?有合适的 function 吗?

Many thanks:)非常感谢:)

My current solution looks like that but its not very pretty;)我目前的解决方案看起来像这样,但不是很漂亮;)

Cells(i, 20) = (l / j) * 1 
        
Cells(i, 20).Interior.Color = RGB(255 * (l / j), 255 * (1 - (l / j)), 0)

Think it makes more sense to use conditional formatting for this, but the code below produces the results in the picture.认为为此使用条件格式更有意义,但下面的代码会产生图片中的结果。

Sub x()

Dim r As Range

For Each r In Range("A1:A11")
    r.Interior.Color = RGB(255 * r.Value, 255 * (1 - r.Value), 0)
Next r

End Sub

在此处输入图像描述

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

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