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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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
 
Upvote 0
Thanks RandomHelper. That has got me close, but now 0 values are also not coloured. I want these to be green
image.PNG
 
Upvote 0
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 & "<>"""")")
 
Upvote 0
Solution

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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
Back
Top