13

In an EXCEL 2010 spreadsheet, how can I create alternating fill color for clustered cell values, changing when a new value cluster is reached?

Assume column A contains the following values:

VALUE

123

123

123

456

456

789

789

789

789

I'd like all of the 123 values in A2:A4 to have the same fill color (say, green), all of the 456 cells in A5:A6 a new fill color (say blue), and all of the 789 cells in A7:A10 back to the fill color used for the 123 values (green again).

This is different from the many duplicate value examples.

It's an exercise that I'd like to repeat over and over for reports where the values will change dynamically, and can't be anticipated.

A VBA solution would be fine.

Thanks.

1

3 Answers 3

20

If you want alternating colours, try this:

Format the whole range in blue. Enter this formula into a helper column, for example column B, starting in row 2 and copied down.

=IF(A2=A1,B1,IF(B1=1,0,1))

Then select the rows and add conditional formatting with this formula

=$B2

Select green as a cell fill. See screenshot. You can hide column B.

enter image description here

1
  • THIS is splendid! Thank you so much! You know, I was pursuing the path of a helper column but never got beyond evaluating the previous row to the current row, to use an IF function. I'll post a slight modification on this method below.
    – 504more
    Commented May 11, 2013 at 16:30
6

From what I understand, you can use regular plain simple conditional formatting.

(Keep in mind that I am translating from french so your menu may be a bit different).

  1. Select your data;
  2. On the Home tab click Conditional formatting and then highlight Color scales and select whichever suits you most. You can customize this if you want.

This will use the same color for each duplicate number. This may not be appropriate if your range is big and you have close data (color will be very similar, hard to differentiate).

enter image description here

2
  • 2
    +1 Best way to go. Just a note Color Shades would be Color Scales (at least for US Excel) Commented May 11, 2013 at 0:49
  • 1
    I passed right over this in the documentation. I guess presenting the solution as an image would make it easier to find the solution than trying to interpret the step-by-step instructions. Still trying to see if this will work to span the colors across the entire row. I played with it a bit and don't think it's possible.
    – 504more
    Commented May 11, 2013 at 16:27
0

Thanks to @teylyn for steering me right on a solution to this puzzle.

I'm adding here a slight variation, adding two levels of conditional formatting to control color, and demonstrating how to get the color shading to span across the rows.

In this expanded solution, I've added an additional "Value" column just to satisfy the additional requirement of adding fill across the rows.

The "Helper" column works the same as @teylyn suggested, evaluating each cell value in column A to the cell value above. If this evaluates to true, the helper column uses the column C value in the previous row to set the current row column C value. If false, the current row column C value is set to 0.

Once these values are set, the conditional formatting can be done by selecting all values in column C (select C2, click ctrl-shft-down arrow), and then setting the first conditional rule to use green fill if $C2 evaluates to 1. Add a new rule, setting the fill color to blue if $C2 evaluates to 0.

To get the colors to span the rows, change the "Applies to" textbox to span the entire range of values from $A$2:$C$10.

It's a splendid solution. Now, to add a third color, or maybe a random color ... there's another day for that one.

Stack Overflow may not allow me to post an image showing the solution (sorry).

No image, but here's what the sheet values look like:

Row Value   Helper
123 ABC 1
123 DEF 1
123 GHI 1
456 JKL 0
456 MNO 0
789 PQR 1
789 STU 1
789 VWX 1
789 YZA 1

And here's the formula to update values in column C:

=IF(A2=A1,C1,IF(C1=1,0,1))

2
  • And another modification both eliminates the hassle that sometimes arises with printing sheets containing hidden fields, and limits the ability of users to break the formatting. Rename a tab "Helper" and create a "Helper" column which stores the values used for conditional formatting on Sheet1 like this: =IF(Sheet1!F2=Sheet1!F1,Helper!A1,IF(Helper!A1=1,0,1)) When setting the conditional formatting, the evaluation statements look like: =Helper!$A2=0 =Helper!$A2=0 The Helper tab can then be hidden to avoid user mischief.
    – 504more
    Commented May 11, 2013 at 17:15
  • 1
    Note that referencing another sheet in conditional formatting formulas only works in Excel 2013 and newer. For earlier versions, the helper column must be on the same sheet. Also, if the helper column contains only 1 or 0 values, they already act like TRUE or FALSE in a condition evaluation, so you don't need to use the = comparison. =B2 is TRUE if it contains a 1, and it is FALSE if it contains a 0.
    – teylyn
    Commented May 11, 2013 at 22:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.