背景颜色与细胞差异 [英] Background Color based on difference with cell
问题描述
我正在尝试创建一个VBA代码,它允许我根据与左边的邻居单元格的区别将背景颜色分配给单元格。
允许我解释:
http://imgur.com/UUfaFRA
当您查看上述图片时,您可以看到:
-
C3
是红色的,因为它的值高于B3
。 -
C4
没有颜色,因为它的值等于B4
-
C5
是绿色,因为它的价值低于B5
我试图把它VBA代码但不成功:
表(x)。范围(C3)。FormulaR1C1 =
/ pre>
= IF(RC> RC [-1],Interior.ColorIndex = 3,IF(RC
另一个尝试是通过录制IF函数:
如果表格(x)。范围(C3) >表格(x)。范围(D3)然后
pre>
表格(x)。范围(C3)。Interior.ColorIndex = 3
我希望有人能指出正确的方向,因为我(显然)没有VBA大师。
解决方案
If Sheets(x)。Range(C3)>表格(x)。范围(D3)然后
pre>
表格(x)。范围(C3)。Interior.ColorIndex = 3
这段代码可以工作。有一些范围问题原创导致此代码失败。
感谢您的支持!
I am trying to create a VBA code which allows me to allocate background colors to a cell based on its difference with the neigbouring cell to the left.
Allow me to explain: http://imgur.com/UUfaFRA
When you look at the image above you can see:
C3
is red because it's value is higher thanB3
.C4
has no color as it's value is equal toB4
C5
is green because it's value is lower thanB5
I tried to put this into a VBA code but don't succeed:
Sheets("x").Range("C3").FormulaR1C1 =
"=IF(RC>RC[-1],Interior.ColorIndex = 3,IF(RC<RC[-1,Interior.ColorIndex= 4))"
another attempt was by recording an IF function:
If Sheets("x").Range("C3") > Sheets("x").Range("D3") Then
Sheets("x").Range("C3").Interior.ColorIndex = 3
I'm hoping someone could point me in the right direction as I'm (clearly) no VBA guru.
If Sheets("x").Range("C3") > Sheets("x").Range("D3") Then
Sheets("x").Range("C3").Interior.ColorIndex = 3
This piece of code works. there were some range issues which originaly resulted in a failure of this code.
Thank for your support!
这篇关于背景颜色与细胞差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!