Background Color based on difference with cell

2019-09-05 23:03发布

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条回答
Melony?
2楼-- · 2019-09-05 23:51
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!

查看更多
登录 后发表回答