Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Detecting Errors in Conditional Formatting Formulas.

Detecting Errors in Conditional Formatting Formulas

Written by Allen Wyatt (last updated December 11, 2018)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


5

Allan uses a lot of conditional formatting, nearly always using formulas to specify the conditions for the formatting. Recently he discovered, by chance, that he had a #REF! error in one of his conditional format formulas. As far as Allan could figure, this was the result of deleting the row of a cell referred to in the formula. The impact is that the conditional formatting won't work for that condition. This has made Allan concerned that there are other instances of conditional formats that have become corrupted since originally being set up. He wonders if there is any simple way of checking all conditional formatting so that these errors can easily be found.

The best way is to use a macro to step through all the conditional formats defined for a worksheet. The following macro does just that, looking for any #REF! errors in the formulas.

Sub FindCorruptConditionalFormat()
    Dim c As Range
    Dim fc As Variant

    Selection.SpecialCells(xlCellTypeAllFormatConditions).Select
    For Each c In Selection.Cells
        For Each fc In c.FormatConditions
            If InStr(1, fc.Formula1, "#REF!", _
              vbBinaryCompare) > 0 Then
                MsgBox Prompt:=c.Address & ": " _
                  & fc.Formula1, Buttons:=vbOKOnly
            End If
        Next fc
    Next c
End Sub

If an error is found, then a message box displays both the address of the cell and the formula used in the conditional formatting rule.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11361) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Detecting Errors in Conditional Formatting Formulas.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Summing Only the Largest Portion of a Range

Given a range of cells, you may at some time want to calculate the sum of only the largest values in that range. Here is ...

Discover More

Getting Rid of the Ctrl+Click Message

When you add a hyperlink to a document, you can later click that link to display whatever is linked to. Well, you ...

Discover More

Selecting to the Bottom of a Column in a Macro

Need to select a range of cells in a column? This tip can help, as it shows how to select from a specific cell all the ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Conditionally Making a Sound

Need to have a sound played if a certain condition is met? It is rather easy to do if you use a user-defined function to ...

Discover More

Conditional Formats for Odd and Even Columns

Setting up conditional formatting can be challenging under some circumstances, but once set it can work great. Unless, of ...

Discover More

Highlighting After-Hours Times

The Conditional Formatting capabilities of Excel are powerful. This tip shows how you can use a simple approach to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 0?

2020-03-04 11:29:16

J. Woolley

Like many, I've been frustrated when my carefully crafted conditional formatting becomes bifurcated after innocently performing a copy/paste or some other such sin. And Excel's miserable Conditional Formatting Rules Manager makes it a chore to fix the mangled rules. So I've written macros to backup and restore the active sheet's conditional formatting (CF) using named ranges that auto-adjust to row/column changes. You can download my M_CFBackup.bas module (text) file from Google Drive here:
https://drive.google.com/open?id=171GNuD7LSFpPz9ep-oF0gB-aY_MUgakv

First make sure the current CF is correct by use of the Conditional Formatting Rules Manager (Alt+H+L+R), then use CFBackup. If you later find the CF has become corrupted, using CFRestore will usually fix the problem. I have tested CFBackup and CFRestore with many scenarios, but not all. If you discover any issues, please add a comment to this thread.


2020-03-03 12:52:42

Roy

@David Gray:

Sure, we all do that, unless applying the simplest of things or something ad hoc that won't survive closing the spreadsheet.

The problem addressed in this tip is that the circumstances of the formatting formulas change over time, sometimes in ways that matter. And that Excel does not offer any built-in way to notice those times, that nothing draws attention to them, that no error messages are passed to US.

Which is NOT addressed by the work process followed setting up the rules, unless one can anticipate a particular failure in a given case and work out a way around it before it ever happens.

What we really need is someone to put some "real" effort into a commercial Add-In that reliably notices them. Perhaps sell it directly for $10, or sell it to someone like Ku--Tools or AbleBits for inclusion in their products.

It would seriously behoove MS to improve this. The validity of spreadsheet models, even the possibility of their validity being demonstrated, for non-simple products that companies commit mega-ducats based upon, is a SERIOUS question in business today. My take on spreadsheets, if one has programming available in-house, or services routinely available out-of-house, is one wants to "spreadsheet" as a product maturing process, then turn it over to programmers to write a dedicated program for. One that CAN be verified to do what it claims. Of course, who does that? But as the yeers pass and the available programming labor continues to pile up as more and more people enter the field, that kind of thing could replace "serious" spreadsheets. Then Excel's market would become a rather declining market, maybe, as, if the development of the tool would not be permanently in spreadsheet hands, other, free, spreadsheets could be used. Only has to work a year perhaps? Who cares if it's a Google spreadsheet then? It doesn't need slickness, just to provide the functionality and once that's road-tested, it'll be replaced with a proper program/app. Even MS is pushing that with their PowerApps. Maybe that's why they don't fix primitive services like Conditional Formatting. Huh...


2018-10-06 15:32:47

David Gray

When I am concerned that a formula upon which I intend to base a conditional format might raise errors, I have a two-step process, applied at design time, that has served me well for over a decade.

First, I code the formula in a regular cell, and thoroughly test it by supplying a variety of problematic inputs. This is easier than you think, since live cells that affect a formula can be temporarily changed, one at a time, and restored by way of the Ctrl-Z (undo) accelerator.

Second, once I have the conditional format set up, I apply the same technique, changing inputs to force the formula to evaluate to TRUE, and verifying that the color changes as expected.

If you are a software engineer, there is a name for this technique, unit testing. Though not as formal as the approaches applied in traditional software engineering, such informal unit testing is effective, and I use it regularly to test complex formulas.


2018-10-06 13:22:35

Roger Shaw

PROBLEM:
For Each fc In c.FormatConditions
If InStr(1, fc.Formula1, "#REF!", _
vbBinaryCompare) > 0 Then ...

failed with:
Run-time error 438: "Object doesn't support this property or method"

VBA debug quicklook shows the following:
Expression | Value | Type
--------+---------+----------------------------
c | "DOW" | Variant/Object/Range
fc | | Variant/Object/UniqueValues

(fc is empty / nothing)

VBA debug quicklook for "fc.Formula1" shows same error as above

The c Value was "DOW" - which I found in cell A1 (and ONLY in Cell A1 in in the whole workbook)
Cell $A$1 had a "Duplicate Values" conditional formula for a range of cell $A$1 ONLY! (And which I never entered, and have no idea where it came from!)
After deleting the Conditiona Format from $A$1, the Macro ran...
I added a "Duplicate Values" fc back to cell $A$1, and got the same error.

I have seen this with 'For Each' loops before - you can hit a type of Variant that does NOT have the property (.Formula1) and blows up the VBA. I had to put special checks in the code to catch this problem.


2018-10-06 10:07:33

Col Delane

The variable fc has not been declared.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.