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
What you can do is create a new column over on the right side of your spreadsheet that you'll use to compute a value you can base your shading on.
Let's say your new column is column D, and the value you want to look at is in column A starting in row 2.
In cell D2 put: =MOD(IF(ROW()=2,0,IF(A2=A1,D1, D1+1)), 2)
Fill that down as far as you need, (then hide the column if you want).
Now highlight your entire data set - this selection of cells will be the ones that get shaded in the next step.
From the Home tab, click Conditional Formatting, then New Rule.
Select Use a formula to determine which cells to format.
In "Format values where this formula is true" put =$D2=1
Click the Format button, click the Fill tab, then choose the color you want to shade with.
Examples here:
you could use this formular to do the job -> get the CellValue for the specific row by typing: = indirect("$A&Cell()) depending on which column you have to check, you have to change the $A
For Example -> You could use a customized VBA Function in the Background:
Public Function IstDatum(Zelle) As Boolean IstDatum = False If IsDate(Zelle) Then IstDatum = True End Function
I need it to check for a date-entry in column A:
=IstDatum(INDIREKT("$A"&ZEILE()))
have a look at the picture:
This one has puzzled me for ages. Don't like the idea of creating an extra (irrelevant) row/column just to calculate formatting. Finally came up with the following rule:
=INDIRECT("A"&ROW())<>INDIRECT("A"&(ROW()-1))
This creates the reference A2<>A1
for row 2, A3<>A2
for row 3 etc.
Adjust the letter "A" to be the column you wish to compare
I hate using these in-cell formulas and having to fill in a new column, and I finally learned enough to make by own VBA macro to accomplish this effect.
This might not be all that logically different from another answer, but I think the code looks a hell of a lot better:
Dim Switch As Boolean
For Each Cell In Range("B2:B" & ActiveSheet.UsedRange.Rows.Count)
If Not Cell.Value = Cell.Offset(-1, 0).Value Then Switch = Not (Switch)
If Switch Then Range("A" & Cell.Row & ":" & Chr(ActiveSheet.UsedRange.Columns.Count + 64) & Cell.Row).Interior.Pattern = xlNone
If Not Switch Then Range("A" & Cell.Row & ":" & Chr(ActiveSheet.UsedRange.Columns.Count + 64) & Cell.Row).Interior.Color = 14869218
Next
My code here is going by column B, it assumes a header row so it starts at 2, and I use the Chr(x+64) method to get column letters (which won't work past column Z; I haven't yet found a simple-enough method for getting past this).
First, the boolean variable will alternate whenever the value changes to a new one (uses Offset to check cell above), and for each pass the row is checked for either True or False and colors it accordingly.
Like at least 1 other contributor here, I also have never liked having to add a extra "helper" column, which can create some hassles in various situations. I finally found a solution. There are a couple different formulas that you can use depending on needs and what is in the column, whether there are blank values, etc. For most of my needs, I have landed on using the following simple formula for the Conditional Formatting (CF) formula:
=MOD(Fixed(SUMPRODUCT(1/COUNTIF(CurrentRange,CurrentRange))),2)=0
I create a Named Range called "CurrentRange" using the following formula where [Sheet]
is the sheet on which your data results, [DC]
is the column with the values on which you want to band your data and [FR]
is the first row that the data is in:
=[Sheet]!$[DC]$[FR]:INDIRECT("$[DC]$" & ROW())
The sheet reference and column reference will be based on the column that has the values you are evaluating. NOTE: You have to use a named range in the formula because will throw an error if you try to use range references directly in the CF rule formula.
Basically, the formula works by evaluating for each row the count of all of the unique values for that row and above to the top of your range. That value for each row essentially provides an ascending Unique ID for each new unique value. Then it uses that value in the place of the Row() function within the standard CF MOD function formula for simple alternating row colors (i.e. =Mod(Row(),2)=0).
See the following example that breaks down the formula to show the resulting components in columns to show what it is doing behind the scenes.
In this example, the CurrentRange
named range is defined as:
=Sheet1$A$2:INDIRECT("$A$" & ROW())
The Unique ID column contains the following portion of the CF formula:
=Fixed(SUMPRODUCT(1/COUNTIF(CurrentRange,CurrentRange)))
You can see that, as of row 3, the count of unique values from that row and above in the "Color" column is 2 and it remains 2 in each subsequent row until row 6 when the formula finally encounters a 3rd unique value.
The Band column uses the remainder of the formula referring to the result in column B =MOD(B2,2)
to show how it gets you to the 1s and 0s that can then be used for CF.
In the end, the point is, you don't need the extra columns. The entire formula can be used in the CF rule + a named range. For me, that means I can stick the basic formula in a template that I use to drop data in and not have to worry about messing with an extra column once the data is dropped in. It just works by default. Additionally, if you need to account for blanks or other complexities or large sets of data you can use other more complex formulas using frequency and match functions.
Hope this helps someone else avoid the frustration I have had for years!