Conditional rormatting rules not applying consistently in Excel 2010

mstrpeez

New Member
Joined
Aug 23, 2011
Messages
6
I've come across some strange behaviour in Excel 2010 when using VBA to apply conditional formatting rules to multiple ranges. What appears to be happening is that after corrently creating 3 conditional formatting rules for 20 different ranges any additional conditional formatting rules created will assign their formatting properties to the incorrect conditional formatting rule.

I tried this in both Excel 2007 and Excel 2010 and it only happens in Excel 2010. (on both Windows XP and Windows 7)


An example :
- Create 3 conditional formatting rules for 50 different cells (i know that i can create 3 rules and apply them to a single range but for the purposes of an easy example i am creating 3 separate rules for each cell)
- rule 1 : "The Green Rule" - make font color green
- rule 2 : "The Yellow Rule" - make font color yellow
- rule 3 : "The Red Rule" - make font color red

What Happens :
The conditional formatting rules are created correctly for the first 20 cells but after that the green font color is applied to the Yellow rule, the Yellow font color is applied to the red rule and the Green rule has no formatting.

Please help, i cannot understand what is causing this behaviour.

Here is the code i used to generate this scenario. You can just paste it into a module in a blank workbook, run it, then take a look at the conditional formatting in cells A20 versus cell A21.

Code:
Sub CFTest()
  Dim lRow As Long
  For lRow = 1 To 50
    With ActiveSheet.Cells(lRow, 1)
      .FormatConditions.Add Type:=xlExpression, Formula1:="=""Red"""
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      .FormatConditions(1).Font.Color = -16777024 ' a red color
      .FormatConditions(1).StopIfTrue = True
    
      .FormatConditions.Add Type:=xlExpression, Formula1:="=""Yellow"""
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      .FormatConditions(1).Font.Color = -16711681  ' a yellow color
      .FormatConditions(1).StopIfTrue = True
    
      .FormatConditions.Add Type:=xlExpression, Formula1:="=""Green"""
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      .FormatConditions(1).Font.Color = -11480942 ' a green color
      .FormatConditions(1).StopIfTrue = True
    End With
  Next lRow
End Sub

Any help would be much appreciated.
 
I was curious about the same thing and tried it earlier and the formatting error still occurs at and after the 21st range.
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,224,521
Messages
6,179,283
Members
452,902
Latest member
Knuddeluff

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