背景颜色与细胞差异 [英] Background Color based on difference with cell

查看:143
本文介绍了背景颜色与细胞差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个VBA代码,它允许我根据与左边的邻居单元格的区别将背景颜色分配给单元格。



允许我解释:
http://imgur.com/UUfaFRA



当您查看上述图片时,您可以看到:




  • C3 是红色的,因为它的值高于 B3

  • C4 没有颜色,因为它的值等于 B4

  • C5 是绿色,因为它的价值低于 B5



我试图把它VBA代码但不成功:

 表(x)。范围(C3)。FormulaR1C1 = 
= IF(RC> RC [-1],Interior.ColorIndex = 3,IF(RC
/ pre>

另一个尝试是通过录制IF函数:

 如果表格(x)。范围(C3) >表格(x)。范围(D3)然后
表格(x)。范围(C3)。Interior.ColorIndex = 3
pre>

我希望有人能指出正确的方向,因为我(显然)没有VBA大师。

解决方案

  If Sheets(x)。Range(C3)>表格(x)。范围(D3)然后
表格(x)。范围(C3)。Interior.ColorIndex = 3
pre>

这段代码可以工作。有一些范围问题原创导致此代码失败。



感谢您的支持!


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 than B3.
  • C4 has no color as it's value is equal to B4
  • C5 is green because it's value is lower than B5

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆