Listing Format Conditions

I wrote some code to list out all the conditional formatting rules in a worksheet. It wasn’t as easy as I thought it would be. In Excel 2007, MS introduced some new format conditions like Icon Sets and Databars that complicate things. As a result, I didn’t get too fancy with the output.

And I got output that looks like this

I couldn’t figure an easy way to loop through all the conditional formatting on a sheet. The FormatConditions collection is a property of the Range object not the Worksheet object. So I looped through all the cells with conditional formatting using SpecialCells. Then to remove the duplicates, I put the FormatCondition object into a collection keyed on the AppliesTo address. Collections don’t allow duplicate keys, so I only get one entry for that FormatCondtion that applies to G5:G10. It’s not fool-proof though. I could have two FormatConditions that apply to the same range and only the first would be taken. That was problem #1.

The second problem was that all conditional formatting aren’t FormatConditions. I put a ColorScale on my sheet and it didn’t like

The FormatConditions.Item property doesn’t return a FormatCondition object, but a ColorScale object. Many of the properties are the same between the FormatCondition and ColorScale objects so I changed cf to a Variant.

I only listed the Formula1 and Formula2 properties if cf was a FormatCondition and ignored all the other types of objects cf could be. It would take too long to list out all the parameters for all the object types and I lost interest.

To convert the Type to a string, I used this function

I wish there was a better way to get at enumerations.

Then, of course, I wanted to show an example of the conditional formatting. Well, that wasn’t going to happen. In conclusion, Excel 2007 conditional formatting is great, but trying to recreate the built-in conditional formatting dialog box is the opposite of great.

31 thoughts on “Listing Format Conditions

  1. It gets even more interesting, if you overlap two areas with separate CF’s.
    XL2007 will double count the number of FormatConditions in the overlapped areas.
    XL2003 on the other hand does not.

    The reason appears to be that the SpecialCells(xlCellTypeAllFormatConditions) method returns separate (non overlapped areas) in xl2003 and xl2007 does not.
    I got around this (I hope) in my Formats & Styles add-in (in xl2007) by iterating every cell in a rectangular range that encompassed the returned areas.
    Be aware… “It can’t be bargained with. It can’t be reasoned with. It doesn’t feel pity, or remorse, or fear. And it absolutely will not stop, ever, until you are dead.”

    Formats & Styles (free)…

  2. This is exactly what I need, but how could I get it to work for Excel 2003?

    I have a lot of CF’s in an inherited workbook, and I need to make sure that they are all corrected pointed to the relevant cells.

    A sheet that gives the information as shown above woudl save hours of work – if it is possible.

  3. I’ve been looking at conditional formatting as a faster way of applying complex formulae on large amounts of data. The result is blindingly fast but I can’t get access via vba to the fill colour that I’ve applied to the cell. Because the whole range has essentially the same formula applied, when I drill down to look at the cells conditionalformat to check it’s result, they are all the same!

    Has anyone managed to find a way around this? Any help much appreciated.

  4. Update in case anyone else has a similar issue…I’ve used Chip Pearson’s code which seems to still work fine in 2007 but have updated the lines where Case xlExpression is true to evaluate a string based on the formula within the conditional formatting but using the current row value rather than the default row value applied to the whole column. Works like a dream :)

  5. Hi Dick,

    I do hope you are still around. I have been looking for a way to document all the conditional formatting rules used on a sheet and I found this very impressive bit of code you wrote.

    I have tried to use it in Excel 2010 and it works perfectly, partly.

    I have 3 conditions in each cell and only the first expression is listed:
    Type Range StopIfTrue Formual1 Formual2
    Expression $E$59:$P$59 TRUE =(NOW()>E$7)*(E59<=5)*(E59″”)
    Expression $E$60:$P$60 TRUE =(NOW()>E$7)*(E60<=3)*(E60″”)
    Expression $E$17 TRUE =(NOW()>E$7)*(SUM($E17:E17)>=$D17/2)
    Expression $F$17 TRUE =(NOW()>F$7)*(SUM($E17:F17)>=$D17/2)
    Expression $G$17 TRUE =(NOW()>G$7)*(SUM($E17:G17)>=$D17/2)
    Expression $H$17:$J$17 TRUE =(NOW()>H$7)*(SUM($E17:H17)>=$D17/2)

  6. Hi Dick,

    I do hope you are still around. I have been looking for a way to document all the conditional formatting rules used on a sheet and I found this very impressive bit of code you wrote.

    I have tried to use it in Excel 2010 and it works perfectly, partly.

    I have 3 conditions in each cell and only the first expression is listed:


    It would be great if you could help with this.
    I would really like to list all 3 conditions (all conditions are expressions).
    Even if it can be done for a single cell at a time (i.e. no counting required), it would be of great assistance.

    Thank you so much.

  7. Bob, I would be interested in a link to that as I cannot find it on VBAExpress and have a similar request.

  8. In case someone like me is searching on just this issue and finds this excellent post, you will want to change


    This will make sure the code works for whatever sheet you’re currently viewing in Excel.

    ALSO make sure you copy BOTH PARTS of his code; the main macro AND the function! For novices like me, that may not have been immediately obvious…

    I love it when people post JUST the solution I needed because they had the same problem – and know how to create a solution! Many thanks to you, Dick, and to the others who provide very valuable information like this on the interwebs! :-)

  9. I have a ws where the users are often deleting/adding rows which leads to fragmenting of the condition format ranges into hundreds of subsets. The code here is just what I want, but keeps throwing index error on the loop “For i = ” with message: Error # 457 – This key is already associated with an element of this collection”. There are two rules (on same range) in the ws but the formatconditions.count is 1. The faults occurs when the loop repeats.

    Using Excel 2010 on Windows 7 Professional.

    Any help appreciated!

  10. Mark: Did you take out the On Error statement. That’s what prevents that message. It prevents it on purpose because I know there will be CFs that apply to more than one cell and I don’t want to include them for every cell in the applies to address, just once per range. If you have an example spreadsheet you can mail to and I’ll take a look at it.

  11. Thanks Dick,
    I’ll email the workbook shortly. It would be so much easier if conditional formats were attached directly to the sheet rather than to the range object (IMHO).


  12. Mark: If you’re getting an error, it’s because you have Break on All Errors set under VBE – Tools – Options – General. That’s ignoring the error handling.

    However, even if you change that to Break in Class Modules, the code still won’t work if you have two CFs that have exactly the same range, which you do. I tried to find a quick way to fix that, but there isn’t one. What I tried to avoid, and what’s causing the problem, is that there are a lot of different “types” of CF and you would have to figure out what type each CF is and deal with it appropriately. There is a Type property, but in my experience that doesn’t tell the whole story.

  13. Thanks Dick for the tip on the ALl Errors setting – changed it and code runs OK.
    As you say it only picks up one of the two conditions sharing the same range, but I suppose that’s par for the course with new fangled features – buggy and their objects are not completely accessible to VBA.

  14. Wonderful code but I believe it only picks up the first instance of a cell’s conditional formatting, i.e. if a cell has multiple rules they don’t all get listed?

  15. Here is a modified version that lists all the rules.

    The modifications I did was that I changed the settings for the collection, so all cells with a conditional format is included.
    When this code runs through a worksheet where conditional format is applied to a large number of rows and columns, it will take forever, so I restricted the range to

    sRange = ActiveSheet.Range(“A6”).CurrentRegion.Address
    For Each rCell In ActiveSheet.Range(sRange).SpecialCells(xlCellTypeAllFormatConditions).Cells

    After the code has run, I filled the result into a variant to gain some speed advantage when writing the result into the sheet.
    The result will have one row per rule per cell in the scanned range.
    The filter and extract at the end of the code will select only unique rows, resulting in the correct result with one row for each rule within each range.

    The result works for me.

    Sub ShowConditionalFormatting()
    Dim aResult() As Variant
    Dim sRange As String

    Dim cf As Variant
    Dim rCell As Range
    Dim colFormats As Collection
    Dim i As Long
    Dim wsOutput As Worksheet

    Application.ScreenUpdating = False

    Set colFormats = New Collection

    ‘ Modify here to select the desired range
    ‘ Useful when conditional formatting is applied to huge ranges and
    ‘ you only want to see the conditional formats for the used range
    ‘ or any other user specified range
    sRange = ActiveSheet.Range(“A6”).CurrentRegion.Address

    For Each rCell In ActiveSheet.Range(sRange).SpecialCells(xlCellTypeAllFormatConditions).Cells
    For i = 1 To rCell.FormatConditions.Count
    On Error Resume Next
    ‘ Modified to let everything through
    colFormats.Add rCell.FormatConditions.Item(i) ‘, rCell.FormatConditions(i).AppliesTo.Address
    On Error GoTo 0
    Next i
    Next rCell

    ‘ Headers for the
    Set wsOutput = Workbooks.Add.Worksheets(1)
    wsOutput.Range(“A1:E1”).Value = Array(“Type”, “Range”, “StopIfTrue”, “Formual1”, “Formual2”)
    ‘ Extract range and filter
    wsOutput.Range(“J1:N1”).Value = Array(“Type”, “Range”, “StopIfTrue”, “Formual1”, “Formual2”)
    wsOutput.Range(“H1”).Value = “StopIfTrue”
    wsOutput.Range(“H2”).Value = “”””””

    ‘ Resize the Variant range and grab the headers
    sRange = Range(“A1”).Resize(colFormats.Count + 1, 5).Address
    aResult() = Range(sRange).Value

    ‘ Move data from the collection into the variant.
    ‘ Instead of writing cell by cell, the complete array is written
    ‘ at the same speed as writing one cell
    For i = 1 To colFormats.Count
    Debug.Print i
    Set cf = colFormats(i)
    aResult(i + 1, 1) = FCTypeFromIndex(cf.Type)
    aResult(i + 1, 2) = cf.AppliesTo.Address
    aResult(i + 1, 3) = cf.StopIfTrue
    On Error Resume Next
    aResult(i + 1, 4) = “‘” & cf.Formula1
    aResult(i + 1, 5) = “‘” & cf.Formula2
    On Error GoTo 0
    Next i
    ‘ Write the result
    Range(sRange).Value = aResult()

    ‘ Filter and extract
    Range(sRange).AdvancedFilter _
    Action:=xlFilterCopy, _
    CriteriaRange:=Range(“H1:H2”), _
    CopyToRange:=Range(“J1:N1”), _

    ‘ Delete the redundant data from the variant


    Erase aResult

    Application.ScreenUpdating = False

    End Sub

  16. Awesome code!

    I’m curious if this can be reversed? That is, take a worksheet with the criteria listed (as you did in your output) and using VBA apply them to other worksheets/workbooks? That way, you could have a set list of criteria that would be portable and could be applied anywhere.

    Just curious.

  17. This looks like exactly what I need and is getting some glowing praise but I can’t get it to work in 2003.

    I’m pretty fresh to vba, when Lori G says that I need to copy both the sub and the function how do I use both?


  18. What is the latest version of this, containing all of the fixes. I grabbed the original macro, then started running into the same fixes that others had encountered. Do you have a “final version” with all the fixes?

Leave a Reply

Your email address will not be published. Required fields are marked *