Background Color based on difference with cell

夙愿已清 提交于 2019-12-13 07:17:30

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/36429924/background-color-based-on-difference-with-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!