Failed Conditional Formatting

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks.

I have this bit of code that is not working as expected. The goal is to highlight a cell with either a red font highlighted interior color if it's value is below or above a given benchmark. The benchmarks are different for each cell...
B21 - 3% or lower
C21 - 25% or higher
D21 - 20% or higher
E21 - 15% or lower
F21 - 15% or higher
G21 - 22% or lower

In some instance there is no value in the cell and in those cases I simply want to ignore the cell and move on. What's happening is even if a cell is blank, it ends up getting either a red font or highlighted interior as if there is a value in it. Here's what I'm working with... I can't wait to see how the guru's here compress this down to three or four lines of code. :)

VBA Code:
    'Column B WTD +/-
    If Range("B21") > 0.03 Then
        Range("B21").Font.Color = 255
    Else
        If Range("B21") < 0.03 Then
            Range("B21").Interior.Color = 65535
        Else
        End If
    End If
    
    'Column C WTD +/-
    If Range("C21") < 0.25 Then
        Range("C21").Font.Color = 255
    Else
        If Range("C21") >= 0.25 Then
            Range("C21").Interior.Color = 65535
        Else
        End If
    End If
    
    'Column D WTD +/-
    If Range("D21") < 0.2 Then
        Range("D21").Font.Color = 255
    Else
        If Range("D21") > 0.2 Then
            Range("D21").Interior.Color = 65535
        Else
        End If
    End If
    
    'Column E WTD +/-
    If Range("E21") > 0.15 Then
        Range("E21").Font.Color = 255
    Else
        If Range("E21") < 0.15 Then
            Range("E21").Interior.Color = 65535
        Else
        End If
    End If
    
    'Column F WTD +/-
    If Range("F21") < 0.15 Then
        Range("F21").Font.Color = 255
    Else
        If Range("F21") > 0.15 Then
            Range("F21").Interior.Color = 65535
        Else
        End If
    End If
    
    'Column G WTD +/-
    If Range("G21") > 0.22 Then
        Range("G21").Font.Color = 255
    Else
        If Range("G21") < 0.22 Then
            Range("G21").Interior.Color = 65535
        Else
        End If
    End If
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Just add another condition to each IF to make sure they are not blank, i.e.
change
VBA Code:
If Range("B21") > 0.03 Then
to
VBA Code:
If (Range("B21") > 0.03) And (Range("B21") <> "") Then
 
Upvote 0
Solution
Just add another condition to each IF to make sure they are not blank, i.e.
change
VBA Code:
If Range("B21") > 0.03 Then
to
VBA Code:
If (Range("B21") > 0.03) And (Range("B21") <> "") Then
Thanks! I didn't think about that.
 
Upvote 0
You are welcome.

Actually, now that I think about, you don't need it in all instances
For example, if you are checking for it to be "> 0.03", there is no way it can be that and blank (zero) at the same time.
So really, you only need it for ones where zero can be found, like this:
VBA Code:
    If Range("C21") < 0.25 Then
as 0 is < 0.25, so that one would need to be:
VBA Code:
    If (Range("C21") < 0.25)  And (Range("B21") <> "") Then

But ones like:
VBA Code:
If Range("B21") > 0.03 Then
should be fine, as-is.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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