1004 Error in VBA Conditional Formatting Code – Fails in 2007

S

snicks

I have a VBA sub which works in Excel 2003 but fails in Excel 2007.

When the worksheet is recalculated, such as a pivot table update, the sub is
called and the error is generated. The sub changes the font and interior
color based upon the value in two specified cells. As written the failure
occurs on the following line:

Selection.FormatConditions(2).Font.ColorIndex = 2

Interestingly I can comment out the following two lines and line above works
without issue.

Selection.FormatConditions(1).Font.ColorIndex = 2
Selection.FormatConditions(1).Interior.ColorIndex = 3

The sub follows:

Sub AddFormatConditions()
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'Percent Funded'", _
xlDataAndLabel
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=$G$5"
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=$J$5"
Selection.FormatConditions(1).Font.ColorIndex = 2
Selection.FormatConditions(1).Interior.ColorIndex = 3
Selection.FormatConditions(2).Font.ColorIndex = 2
Selection.FormatConditions(2).Interior.ColorIndex = 41

ActiveSheet.PivotTables("PivotTable1").PivotSelect "'Percent Funded'", _
xlLabelOnly
Selection.FormatConditions.Delete
Range("F14").Select
Range("a1").Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top