The DisplayFormat OM

New features in conditional formatting extend to the object model as well. Excel has traditionally allowed users to access the formatting of their cells through the object model. For example, Users can use the interior class which lives off the range object to access the formatting of the cell. Selection.Interior.Color returns the background color of the cell.

However, Selection.Interior.Color does not take into account the conditional formatting applied to the cell.  In Excel 2010, we’ve introduced a new class called DisplayFormat which lives off the range object.  This class provides an easy way to access the formatting applied by conditional formatting rules and other things like table styles that format the cells but don't really reflect it in the cell formatting properties today.

For example, the following code gets the fill color of the active cell (after taking into account any conditional formatting applied to the cell):

Selection.DisplayFormat.Interior.Color

Let us consider a simple example as to how the DisplayFormat object might be used.  A bookstore provides bonuses to its employees based on how well they have met their sales targets for the year. The bonuses are awarded as follows:

  • 15% bonus – Sales targets have been met for each quarter for the year.
  • 5% bonus – Sales target was met for at least one quarter during the year.
  • No Bonus – Failed to meet even a single sales target.

At the start of each year, the bookstore compiles a sales target for each employee as follows:

image

Then, the bookstore records the actual sales on a separate worksheet and uses conditional formatting to highlight quarters where the employees failed to meet their sales target.  These quarters are highlighted with a red background as follows:

image

Since the bonuses awarded at the end of the year depend on how well the salespeople met their targets, you can imagine that a formula to compute the bonus percentage would be quite complex.  Using the new DisplayFormat object, you can easily write custom user defined functions that leverage the smartness in conditional formatting rules to compute the bonuses.

In this case, the function to compute the employee bonuses looks as follows:

Function BonusAward(quarters As Range) As Double

Dim iBelowTarget As Integer ' Number of cells below target

' initialize variables

BonusAward = 0.15 'default 15% bonus-met target for all qtrs

iBelowTarget = 0

' loop through each cell

For Each qCell In quarters

'if conditional formatting cell background is NOT red(255)

If qCell.DisplayFormat.interior.color > 255 Then

  BonusAward = 0.05 ' award less bonus (failed quarter)

  iBelowTarget = iBelowTarget + 1

End If

Next qCell

' Award zero bonus if all quarters were below target

If iBelowTarget = quarters.Cells.count Then BonusAward = 0

End Function

The DisplayFormat object will provide access to conditional formatting results using many existing classes and properties such as the Borders, Characters, Font, Interior and Style.  Since we use existing classes and properties, it means that you can easily extend your existing code to work with it.

This wraps up my series of posts on conditional formatting. In our next Excel 2010 post we’ll continue on the theme of data visualization improvements by discussing some things we did with charts.

Office Blogs Comments

Comments: (6) Collapse

  • This object model extension seems to be of limited value. In the example, the VBA code is looking for a specific type of conditional formatting -- which can  be changed easily by a user who doesn't like the color red as a background.

    It would make more sense to add a simple binary property to the FormatConditions object. For example, add a property named Satisfied. It would return True if that condition is satisfied; False otherwise.

    Or maybe Excel 2010 includes something like that and I haven't found it yet?

  • "Since the bonuses awarded at the end of the year depend on how well the salespeople met their targets, you can imagine that a formula to compute the bonus percentage would be quite complex."

    Actually, a formula like =IFERROR(CHOOSE(SUMPRODUCT(--(B12:E12>B2:E2)),0.05,0.05,0.05,0.15),0) copied down the Yearly Bonus column isn't too complex. (Targets assumed in B2:E6, Actuals assumed in B12:E16).

    John makes a good point.

  • John: yeah the "satisfied" would be useful as well to have off of the CF rule.  You'd have to cross it with the other cf rules its stacked up on, and diff the type of rules, but i can def see how its helpful. It wouldn't cover the table style case, or future features that might do something similar ... in general we wanted to provide a easy access to the color / border / ... that got rendered to the screen.  I'll be sure amit sees this.

    Colin: thanks, I'll pass it on to amit as well.

  • CF's neglected(as it has nothing to do with Colors) cousin Data validation has a "Satisfied" property called "Value"...

  • Hi Colin,

    1. Actually your formula will not return the correct results if the employee exactly meets the goal.

    2. You can shorten the solution to at least the array entered:

    =INDEX({0,0.05,0.05,0.05,0.15},1+SUM(--(B12:E12>=B2:E2)))

    or its equivalent SUMPRODUCT.

    Or with a little lookup table

    =LOOKUP(SUMPRODUCT(--(B12:E12>=B2:E2)),J$1:K$3)

    or the array equivalent

    =LOOKUP(SUM(--(B12:E12>=B2:E2)),B$19:C$21)

  • Hi Shane,

    Thanks for the correction. In transcribing the formula from an Excel sheet, I didn't notice that I had omitted the equal sign (>=).

    I purposely chose a non-array formula for this, but your solutions (both arry and non-array) are quite good.

Comments

Comments: (loading) Collapse