Referring to “this cell” using Excel conditional formatting
If you already know about conditional formatting and navigated here via Google, please jump straight to the hack. If not, I hope the following introduction is useful. You might also like to check out the WikiHow introduction to conditional formatting in Excel. This post is actually concerned with an interesting hack that lets you reference the value of a cell itself when setting up formula-based conditional formatting rule.
Excel’s conditional formatting feature is a boon to heavy spreadsheet users like me. It is a flexible and powerful tool that (among other things) lets me highlight data according to a set of rules so that I can easily spot the interesting bits in what would otherwise be an almost impossibly dense and meaningless cloud of numbers. Here’s an example; a table of the correlations between 32 different statements (taken from some ongoing work looking at a simple blogger typology.)
I’m sure you’d agree that your eyes would go squiggly if you looked for interesting data points in that mess (and that’s the processed table!) Instead, I use Excel’s conditional formatting to look through it for me. Here’s the same table with interesting points coloured red, orange, and yellow (in decreasing order of interest.)
Useful, eh? However, there are some very tight restrictions. Among them are:
- The rule must evaluate to TRUE or FALSE. Either the cell is more than x, for example, or it isn’t.
- Like wishes, you can only apply three conditional-formatting rules to any cell.
- Once Excel has evaluated a condition as TRUE it stops processing any further rules. It evaluates Condition 1, and if that’s FALSE will move on to Condition 2 and if that’s TRUE will never evaluate Condition 3 (however interesting Condition 3 may be.)
Here’s a much reduced version of the table of correlations example I used above. I still wouldn’t want to look through a table like this very often, but it’s easier to make my point when you can read the numbers.
I did this by applying the following conditional formatting rules.
For the sake of this analysis, any correlation of 0.5 or over is seen to be “possibly interesting”, 0.6 or over “interesting”, and 0.7 or over “really quite interesting indeed.” I should probably come up with a better scale before I share this stuff in future.) But as it happens, correlations can go two ways, positive (“people who agree with this statement tend to agree with that statement”) or negative (“people who agree with this statement tend to disagree with that statement”.) So I also want to highlight any correlation lower than -0.5, -0.6, or -0.7. You can see from the following screen grab that this isn’t working.
But I only have three rules. What am I to do?
As it happens, Excel lets one specify a formula as a rule rather than simply a cell value. So, for example, I could check to see if:
=OR(A1<= 0.5, A1>=0.5)
But I’d have to code the conditions for each separate cell in the table. I am notoriously lazy and therefore unlikely to do something like this, particularly for the 32×32 matrix in the first example.
What I need to do is refer to ‘this cell’ in the formula. Now in normal Excel use this would lead to recursion, a circular reference, and therefore an error. For this logical reason, Excel’s developers never saw the need to let a cell refer to itself. But that’s exactly what I need to do!
The Excel Conditional Formatting Hack
After much faffing, I finally worked out a way around the problem.
Which you will see works very nicely.
Let’s look at how it works (I’ve put the bit that matters in bold face.)
That probably doesn’t help all that much. On the assumption that for many of you it’s the first time you’ve seen these functions, let’s pull it apart a bit.
INDIRECT is a way of linking to a cell using a “reference text” – that is, a text string that looks like a cell reference (“A1″ for example, or “R1C1″ which is the same thing – pointing to Row 1 Column 1)
We actually employ it like this:
Where ref_text is the “reference text” and a1 is a TRUE or FALSE value that tells Excel whether to expect an A1-style reference (TRUE or omitted) or an R1C1-style reference (FALSE)
ROW() tells Excel to insert the value for the current Row, and…
COLUMN() tells Excel to insert the value for the current Column, so:
INDIRECT("R"&ROW()&"C"&COLUMN(),FALSE) gives us the R1C1-style reference to the current cell. This cell! Hooray!
These didn’t work…
Incidentally, I also tried these which I think should work but don’t. I include them for completeness’s sake.