1
votes

I'm creating an Excel document at runtime that has a bunch of values I'd like to have conditionally formatted. In going through various attempts from scratch as well as using/modifying code outputted from the Excel's macro recorder, I'm having a consistent issue related to formatting overwrites.

I've posted a snippet of the code below and can say that I've tested to ensure my selection ranges are valid and appropriate for what I want conditionally formatted. There is some overlap but what's bizarre is that the first conditional format takes on just one property of the second conditional format. Meaning D5:End of the worksheet ends up having a green color font as opposed to the red it should be. Commenting each section of the code does allow them to work independently but I'm guessing this is an issue with specifying conditional formats further somehow? I've tried a few different case scenarios and below is the code with modifications:

EDIT (Updated Code):

'First conditional format, check sheet for values > 50 and make text red.
With xl.range("D5:" & theLastColumn & lastRow)
  .FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=50"
  With .FormatConditions(1).Font
    .Color = -16383844
    .TintAndShade = 0
  End With
  .FormatConditions(1).StopIfTrue = False
End With


'Second conditional format, check specific row (row 5 in the example) 
'for values > 40, and fill interior with green in addition to dark green text.
With xl.range("D" & Infectivity & ":" & theLastColumn & Infectivity)
  .FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=40"
  With .FormatConditions(2).Font
    .Color = -16752384
    .TintAndShade = 0
  End With
  With .FormatConditions(2).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 13561798
    .TintAndShade = 0
  End With
End With

So what's the best way to have multiple conditional formats (that may overlap ranges) and still have them all function as intended? I've tried debugging this so much I'm certain there's something easy I'm overlooking. I've also tried a few different methods to specify separate formatconditions(1) and formatconditions(2) but still receive strange issues.

EDIT:

VBA Code where I continue to have the same issue.

Sub conditionalFormat()
  With Range("D5:BA9")
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=50"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Font
      .Color = -16383844
      .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
  End With

  With Range("D9:BA9")
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=40"
    With .FormatConditions(2).Font
      .Color = -16752384
      .TintAndShade = 0
    End With
    With .FormatConditions(2).Interior
      .PatternColorIndex = xlAutomatic
      .Color = 13561798
      .TintAndShade = 0
    End With
    .FormatConditions(2).StopIfTrue = False
  End With    
End Sub

Even with the SetFirstPriority on the appropriate (red text) conditional format, it just gets overwritten somehow. Am I missing something here?

2
I am trying to understand this. You can help me by giving an example. Let's say the first range is A1:G1 and the 2nd range is D1:J1 so there is an over lap on D1:G1. So what are you expecting?Siddharth Rout
I'm just getting a lot of mixed results where the area that isn't overlapping still receives strange conditional formatting related to the second conditional format. I'm sorry it's a bit difficult to understand but I'll try to mockup an example in my original post.Bernard
Yeah Conditional Formatting can be a PIA if not handled properly... Will wait for your example.Siddharth Rout
I'm doing a bit more testing to see if I can figure out why only a portion of the formatting from one conditional format is being applied elsewhere. It's a bit frustrating when you're dealing with something as simple as a font color (and you're red/green color deficient). Regardless, thank you for the willingness to help @SiddharthRout. I look forward to your assistance.Bernard
Just to be sure.. Which excel version are you using?Siddharth Rout

2 Answers

1
votes

Sorry. I don't have Excel 2007. Tested this in Excel 2010.

When it comes to conditional formatting, you have to be really careful of what the macro recorder spits out. This is one particular case where it makes a mess of the code.

Also you are setting then 2nd rule as .SetFirstPriority which is incorrect besides letting the 2nd rule run in spite of rule 1 get getting satisfied :)

Here is a very basic example. Let's say my range looks like this D5:G7

enter image description here

Now this is the VBA code that I tested

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set rng = ws.Range("D5:G7")

    With rng
        .FormatConditions.Add Type:=xlCellValue, _
        Operator:=xlGreater, Formula1:="=50"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Font
            .Color = -16776961
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = True

        .FormatConditions.Add Type:=xlCellValue, _
        Operator:=xlGreater, Formula1:="=40"
        With .FormatConditions(2).Font
            .Color = -11489280
            .TintAndShade = 0
        End With
        With .FormatConditions(2).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.599963377788629
        End With
    End With
End Sub

And this is the result that I got.

enter image description here

I am sure it will be very easy for you to port the above code to vb6.

FOLOWUP (From Comments)

Using latebinding... would earlybinding be better suited for doing this type of conditional formatting? – Bernard 2 mins ago

If you are using LateBinding then declare this code at the top of your code

Const xlCellValue as Long = 1
Const xlGreater as Long = 5
Const xlAutomatic as Long = -4105
Const xlThemeColorAccent3 as Long = 7
0
votes

After much thought and reworking the code we came to the conclusion that what I was doing (multiple conditions overlapping) was the cause of the mixed results. At the simplest level, I was able to add .FormatConditions.Delete to my additional conditional formats to ensure only one format was applied.

The corrected final code is shown below:

Dim Infectivity As Long
Infectivity = Application.WorksheetFunction.match("Infectivity", range("A1:" & "A" & lastRow), 0)

With xl.range("D5:" & theLastColumn & lastRow)
    .FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, _
    Formula1:="=50"
    .FormatConditions(.FormatConditions.count).SetFirstPriority
With .FormatConditions(1).Font
    .Color = -16383844
    .TintAndShade = 0
End With

    .FormatConditions(1).StopIfTrue = False
End With

If Infectivity > 0 Then
With xl.range("D" & Infectivity & ":" & theLastColumn & Infectivity)
    .FormatConditions.Delete
    .FormatConditions.add Type:=xlCellValue, Operator:=xlGreater, _
     Formula1:="=40"
With .FormatConditions(1).Font
    .Color = -16752384
    .TintAndShade = 0
End With
With .FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 13561798
    .TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
End If

My downfall was related to the macro recorder giving me a false of the ideal method of formatting these cells. It's always best to simplify before moving forward.

Major thanks to Siddharth Rout for all the help.