I would like to shade entire rows in Excel based on the value of one cell. For example say I have the rows below:
**File No**
1122
1122
1144
1155
1155
1155
If you are using MS Excel 2007, you could use the conditional formatting
on the Home
tab as shown in the screenshot below. You could either use the color scales
default option as I have done here or you can go ahead and create a new rule
based on your data set.
I had to do something similar for my users, with a small variant that they want to have a running number grouping the similar items. Thought I'd share it here.
1
in A2=IF(B3=B2,A2,A2+1)
=MOD($A1, 2)=1
as the formulaA simpler version of one of the above answers. Column A is the key.
Yes, it needs a helper column. That's column K.
1) Set first cell in table to TRUE (K8)
2) On second row at K9, and to end of table (K99), paste: =IF(A8=A9,K8,NOT(K8))
This gives a pattern of TRUE...TRUE, FALSE...FALSE,...
3) Select key column A1:A99, or whole table A1:K99
4) Set Home/Conditional/New Rule/Formula =K8
5) Format as you wish for the TRUE cells
6) Select the range from (3) and Right-click Format White (or whatever
background color you want) for the FALSE cells
Note that this solution (and the others) have a major flaw, in that this
highlighting doesn't work properly when you have filters active in your
table. I want that fix :)
I have found a simple solution to banding by content at Pearson Software Consulting: Let's say the header is from A1 to B1, table data is from A2 to B5, the controling cell is in the A column
In MS Excel, first save your workbook as a Macro Enabled file then go to the Developper Tab and click on Visual Basic. Copy and paste this code in the "ThisWorkbook" Excel Objects. Replace the 2 values of G = and C= by the number of the column containing the values being referenced.
In your case, if the number of the column named "File No" is the first column (namely column 1), replace G=6
by G=1
and C=6
by C-1
. Finally click on Macro, Select and Run it. Voila! Works like a charm.
Sub color()
Dim g As Long
Dim c As Integer
Dim colorIt As Boolean
g = 6
c = 6
colorIt = True
Do While Cells(g, c) <> ""
test_value = Cells(g, c)
Do While Cells(g, c) = test_value
If colorIt Then
Cells(g, c).EntireRow.Select
Selection.Interior.ColorIndex = 15
Else
Cells(g, c).EntireRow.Select
Selection.Interior.ColorIndex = x1None
End If
g = g + 1
Loop
colorIt = Not (colorIt)
Loop
End Sub
Use Conditional Formatting.
In it's simplest form, you are saying "for this cell, if it's value is X, then apply format foo". However, if you use the "formula" method, you can select the whole row, enter the formula and associated format, then use copy and paste (formats only) for the rest of the table.
You're limited to only 3 rules in Excel 2003 or older so you might want to define a pattern for the colours rather than using raw values. Something like this should work though: