Conditional formatting for blank cells in VBA

Stev087

New Member
Joined
Feb 4, 2021
Messages
3
Platform
  1. Windows
I have a report template that colours cells according to their number value higher than a value, lower than a value and in between.
The in-between value will normally include 0.
In my report I have a blank row between groups of data. At present this is being colored green (same as 0 values).
I want blanks to have no colour to them but can’t they the coding right.
Below is my coding, I just want something to add below this to ensure that blanks on column 8 are not colored the same as zeros.

VBA Code:
ActiveSheet.ListObjects("Table").ListColumns(8).DataBodyRange.Select

With Selection.FormatConditions.Add(xlCellValue, xlGreater, "=$C$22")
    .Interior.Color = RGB(255, 199, 206)
    .Font.Color = RGB(156, 0, 6)
End With

With Selection.FormatConditions.Add(xlCellValue, xlLess, "=$C$23")
    .Interior.Color = RGB(200, 200, 255)
    .Font.Color = RGB(0, 0, 120)
End With

With Selection.FormatConditions.Add(xlExpression, xlBetween, "=$c$23", "=$C$22")
    .Interior.Color = RGB(198, 239, 206)
    .Font.Color = RGB(0, 97, 0)
End With
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

aRandomHelper

Board Regular
Joined
Jan 14, 2021
Messages
222
Office Version
  1. 2016
Platform
  1. Windows
Try changing your 3rd conditional formatting to this:
VBA Code:
Dim a
a = Selection.Resize(1, 1).Address(0, 1)
With Selection.FormatConditions.Add(xlExpression, Formula1:="=AND(" & a & ">=$C$23," & a & "<=$C$22," & a & "<>0)")
    .Interior.Color = RGB(198, 239, 206)
    .Font.Color = RGB(0, 97, 0)
End With
Not sure if I got your upper and lower limits right, if not, just swap C22 and C23 or the > and < symbols
 

Stev087

New Member
Joined
Feb 4, 2021
Messages
3
Platform
  1. Windows
Thanks RandomHelper. That has got me close, but now 0 values are also not coloured. I want these to be green
image.PNG
 

aRandomHelper

Board Regular
Joined
Jan 14, 2021
Messages
222
Office Version
  1. 2016
Platform
  1. Windows
My bad, no idea why I had 0 in mind when what you want to exclude are blanks.

Change to this
VBA Code:
With Selection.FormatConditions.Add(xlExpression, Formula1:="=AND(" & a & ">=$C$23," & a & "<=$C$22," & a & "<>"""")")
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,318
Messages
5,623,976
Members
416,003
Latest member
indyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top