Conditional formatting bug?

Albatros

New Member
Joined
Aug 31, 2011
Messages
26
Hi guys

This is driving me to dispair.

I have set two rules to format the data body range of a pivot table.

The first rule changes the colour of the font to blue for cells whose value is between 0 and 8.
The second rule changes the colour of the font to red for cells whose value is greater than 8.

Cells with an exact value of 8 should remain with its font colour in black.

First of all I make sure that no old conditional formatting rules are functioning by deleting them.

pt.DataBodyRange.FormatConditions.Delete

'Pt is the variable that points to my pivot table
'Now I create the first formatting condition

pt.DataBodyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=0.1", Formula2:="=7.99"

'Once created I apply the formatting
pt.DataBodyRange.FormatConditions(1).Font.Color = -1003520 (blue)

'This makes the font blue for cells whose value is between 0 and 8
'So far no problem. The cells that meet the condition are displayed with a blue font.

'Now I create a new rule

pt.DataBodyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=8"

'and for cells that meet this rule (value greater than 8) I want their font in red


pt.DataBodyRange.FormatConditions(2).Font.Color = -16776961(this is red colour)


'Note that I am applying this formatting to the second rule created. But instead of colouring in red the cells greater than 8, it applies the formatting to the first set of cells, the ones with their value bigger than 0 and smaller than 8.

'If I try something like
.DataBodyRange.FormatConditions(2).Interior.ThemeColor = xlThemeColorAccent3

'changing this time the interior colour it works without any problem, and the formatting this time applies to cells greater than 8 as it should.

'If I go to the conditional formatting menu and try to do it manually I have no problem setting different font colours for different rules, but for some reason it doesn't work when doing it programmatically.

Any help?

Thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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