0

I am conditionally formatting a range of cells. I want to do greater than and less than another cell value. I have found this code online that lets me do that but I was wondering if there is a way to skip formatting if the condition cell is blank.

Example: I want to highlight "G14" red if the value is greater than "D14"+"E14". Vise versa for minus. If the value of "E14" is blank it still make s it red because it is acting as if it is zero.

Here is my code:

'Format Measured
 Dim rg As Range
 Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition
 Set rg = Range("G14", Range("G14").End(xlDown))

'clear any existing conditional formatting
rg.FormatConditions.Delete

'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=$D14+$E14+$H14")
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlLess, "=$D14-$F14")

'define the format applied for each conditional format
With cond1
.Font.FontStyle = "Bold"
.Font.Color = vbRed
End With

With cond2
.Font.FontStyle = "Bold"
.Font.Color = vbRed
End With
3
  • Hm, maybe different routes here. I'm thinking along the lines of SpecialCells or an adaptation of your formula to include "<>""" Commented Nov 20, 2019 at 14:40
  • So should the conditional formatting check if either D14 or E14 is blank, or just E14? Commented Nov 20, 2019 at 14:42
  • Just E14. The way I have my spreadsheet is I have a measurement in D14 and a tolerance in E14 and F14. Some measurements do not have tolerances. Commented Nov 20, 2019 at 14:54

2 Answers 2

1

Proposing the alternate solution to use a formula:

Sub Test()
    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition
    Set rg = Range("G14", Range("G14").End(xlDown))

    'clear any existing conditional formatting
    rg.FormatConditions.Delete

    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlExpression, , "=AND($G14>$D14+$E14,$E14<>"""")")
    Set cond2 = rg.FormatConditions.Add(xlExpression, , "=AND($G14<$D14-$E14,$E14<>"""")")

    'define the format applied for each conditional format
    With cond1
        .Font.FontStyle = "Bold"
        .Font.Color = vbRed
    End With

    With cond2
        .Font.FontStyle = "Bold"
        .Font.Color = vbRed
    End With
End Sub

Note that if the format is the same for each rule, then you could combine the two into one.

Sign up to request clarification or add additional context in comments.

18 Comments

This worked for G14. is there a way to do the whole selection or loop this?
I can't replicate that - works for the entire range for me. How did you implement it?
Unless OP needs xlUp (meaning maybe G15 was empty?), but for me it also works =)
I copied and pasted the new code into my original code. It worked for the first 2 cells but not the rest of the cells where the condition was blank. the rest of the spreadsheet formats fine.
What is the range that the conditional formatting is applied to? You can see it under Conditional Formatting > Manage Rules.
|
0

You can do this multiple ways, but as per my comment, for example:

Set rg = Range("D14", Range("G14").End(xlDown))
Set rg = Intersect(rg.Columns(1).SpecialCells(2).Rows.EntireRow, Range("G:G"))

I would also consider to be explicit about your range object, consider refering to a Sheet object.

Btw, if you also want to avoid if E14 is blank, you can extend the Intersect:

Set rg = Intersect(rg.Columns(1).SpecialCells(2).Rows.EntireRow, rg.Columns(2).SpecialCells(2).Rows.EntireRow, Range("G:G"))

Obviously, another answer could be on how you could use a formula as a condition which checks if cells are '<>""`

2 Comments

I am really new to VBA. Do you know of a formula to use?
@brandoncampbell, check out the other answer for that alternative =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.