Conditional formatting based off the previous X number of cells - excel

I am trying to setup conditional formatting so that the formatting takes effect if the previous 4 cells in a column are blank. I need the range inside the conditional formatting formula to move as it is applied to cells below it, for example:
If I am tracking sales by store, if there is a 4 week period where a certain store doesn't record any sales I want those 4 weeks to automatically highlight.
My current formula is to select cells B5:B11, enter the formula =SUM(B2:B5)=0 to trigger the cell highlighting.
My problem is that when I look at cell B6 the range it looks for to be blank is still B2:B5 and I need it to be B3:B6.
Is there a way to set this up so that the range will change as it moves down a column?

This one has gotten me thinking a bit, and here is my solution for it.
My solution needs to use four conditional formattings, i.e. set up four conditional formattings in each cell, and then use format painter to apply the conditional format to all the cells.
Suppose your week table is in range A1:D20, and suppose you want to highlight cells if there are at least (not only) four consecutive blank cells.
Highlight cell B2, and then set up conditional formatting using the following four formulas:
=SUM(OFFSET($B2,0,0,4,1))=0
=SUM(OFFSET($B2,-1,0,4,1))=0
=SUM(OFFSET($B2,-2,0,4,1))=0
=SUM(OFFSET($B2,-3,0,4,1))=0
Then should have something similar to the following when examing the conditional formatting window:
Lastly, highlight cell B2, use the Format Painter function under Home tab and then apply the format to the rest of the cells in the week table. Then you should have something similar to the following:
It is easy to find the first blank cell of four consecutive blank cells, but it is difficult to identify the second, third and fourth blank cells using one formula. The workaround uses four different formulas to identify each of the four blank cells (I hope this makes sense). Please note the following is only looking at Column B of my example, not all columns:

Not pretty, but it works. This will highlight all occasions where the store has at least 4 weeks of consecutive donuts.
Columns B:D = Original Data
Columns E:G = Helper Set 1
Columns H:J = Helper Set 2
There's probably a way to get just one helper set, but this works and was easy to validate. First I added 3 empty rows above week 1.
In cells E4, E5 and E6 place a 0. You don't have to, you could also leave these blank.
Cell E5 formula, which is counting the number of consecutive blanks:
=IF(ISBLANK(B5),E4+1,0)
Drag it over to column G and drag it down.
Cell H5 formula, which is looking at 4 weeks later to see if there were consecutive blanks found in helper set 1:
=IF(E8>=4,"Yes","No")
Drag it over to column J and drag it down. Also, in the row above it, repeat week 1 values.
Lastly, select your original data range (B5:D14) and create this conditional formatting formula:
=(OR(H5="Yes",H4="Yes",H3="Yes",H2="Yes"))
Once you're satisfied, you can hide rows 2:4 and can also hide your helper columns.

Apply the following condition to each respective ranges:
Range: B2:D2:
=SUM(B2:B5)=0
Range: B3:D3:
=OR(SUM(B2:B5)=0, SUM(B3:B6)=0)
Range: B4:D4:
=OR(SUM(B2:B5)=0, SUM(B3:B6)=0, SUM(B4:B7)=0)
Range: B4:D8:
=OR(SUM(B2:B5)=0, SUM(B3:B6)=0, SUM(B4:B7)=0, SUM(B5:B8)=0)
Result with sample data:

Related

dynamic conditional formatting based on dropdown

I have 7 Columns, 5 with data and the first two with names. Is there a formula I can use in conditional formatting where if I change my column from the drop-down, conditional formatting will look in that column and highlight the name with less than 25, and when i change the value of Capture, the purple highlight across the columns changes too?
.
Hope it makes sense
Suppose you have the following named ranges:
Capture being the column number that you want to change dynamically;
ListFOR being the list of names you have in the FOR column;
RngData being the values in your table excluding the first column and column headers.
I will use the following example to demonstrate my answer:
To highlight a specific column by changing Capture value:
Highlight range B3:G10 (which is the 6 columns including headers on the right) with cell B3 being the active cell;
Use the following formula as the Formatting Rule in conditional formatting and set the background color as desired:
=COLUMN(A$1)=Capture
The logic is to compare the value returned by COLUMN function with the Capture value, if they are the same i.e. TRUE, apply the conditional format.
To highlight a specific name or names with value less than 25 in the highlighted column from previous step:
Highlight range A4:A10 (which is the first column without header) with cell A4 being the active cell;
Use the following formula as the Formatting Rule in conditional formatting and set the background color as desired:
=INDEX(RngData<25,MATCH($A4,ListFOR,0),Capture)
The logic is to first convert the values into TRUE or FALSE by comparing them with 25, then use INDEX+MATCH function to return the logical result based on the given name, if TRUE apply the conditional format.
Here is a live demo:
Let me know if you have any questions. Cheers :)

How to do a Conditional Formatting with hh:mm:ss format on Excel

I have two different columns. Each one contains timestamps in hh:mm:ss format, and I want to do a between comparition and check if the Value Colum 2 is between Value Column 1 Row 1 and Value Column 1 Row 2, and then, paint the cell in green, or in red if the condition is not satisfied.
Here my columns:
Column 1 Column 2
16:06:13 16:07:34
16:06:41 16:08:42
16:08:39 16:09:49
16:09:47 16:11:02
16:10:53 16:12:10
16:12:08 16:13:40
16:13:39 16:14:48
16:14:47 16:16:04
16:18:06 16:18:06
I applied a Conditional Formatting -> HighLight Cell Rules -> Between
then from Column 2 Row 1, I selected the first value from Column 1 Row 1 and the next value from Column 1 Row 2, but nothing happens.
Anyone can help me?
Thanks!!
Following the process you did:
Highlight the values in Column 2
Conditional Formatting -> Highlight Cell Rules -> Between
Select the first value from Column 1 Row 1 then delete the dollar signs
Select the second value from Column 1 row 2 then delete the dollar signs
Note: I see that there is sometimes a glitch in some versions of Excel which replaces your cells with cells at the bottom of the spreadsheet. I find if I just repeat the steps another time, it fixes the issue.
Two rules with two different formulas:
1st:
=MEDIAN(B1,A1,A2)=B1
And format it to Green
2nd:
=MEDIAN(B1,A1,A2)<>B1
Another alternative method:
Highlight the values in column 2.
Conditional Formatting -> New Rule
Select "Use a Formula to determine which cells to format"
Under "Format values where this formula is true", input: "=AND(B1 >= A1,B1 <= A2)"
Select "Format..." and choose the formatting you want
Click OK
Note that this method, vs. the other one I gave you, treats the last cell differently (since there's no value after it). The first one will highlight it, this one will not.
Note: With both methods, I see that there is sometimes a glitch where Excel replaces your cells (A1, A2, and B1 in this case) with cells at the bottom of the spreadsheet. I find if I just repeat the steps another time, it fixes the issue.
If you wish to stick with in-between you can
The data I have in those two fields is
=INDIRECT(ADDRESS(ROW(),COLUMN()-1))
=INDIRECT(ADDRESS(ROW()+1,COLUMN()-1))
You will still need two of these as said in other answers.

Conditional formatting rows based on cell dynamic

I’m wondering if someone can assist with a conditional formatting issue i just can't wrap my head around
We currently have a table with names in and next to the name a number
Bob 5
Michael 6
Now if i type bob in cell A1 i would like it to highlight that row and the four below it.
another example would be if i type Michael in cell A1 it would highlight that row and 5 below that
any help is appreciated
I couldn't write a simpler formula, but that one worked with the data I tested.
A1 will be where names will be inserted.
The table will range from C1 to D7 (assuming there are no column labels).
Select the table C1 to D7 and insert conditional formatting with formula and use the formula:
=AND(COUNTIF(OFFSET(C1,IF(ROW()-VLOOKUP($A$1,$C$1:$D$7,2,0)<=0,1-ROW(),1-VLOOKUP($A$1,$C$1:$D$7,2,0)),0,VLOOKUP($A$1,$C$1:$D$7,2,0)),$A$1)>0,ROW()>=MATCH($A$1,C:C,0))
And pick the formatting your want.
The formula checks two conditions:
COUNTIF(OFFSET(C1,IF(ROW()-VLOOKUP($A$1,$C$1:$D$7,2,0)<=0,1-ROW(),1-VLOOKUP($A$1,$C$1:$D$7,2,0)),0,VLOOKUP($A$1,$C$1:$D$7,2,0)),$A$1)>0
This checks if there is at least 1 match within a designated range depending of the value in D. It will check if there is a match x rows above the current row where x is the value. If there is, the we get the first boolean value (true or false).
The second ROW()>=MATCH($A$1,C:C,0) ensures that the match is above or on the current row.
Google Spreadsheet Demo

Formatting a cell based on perpendicular row/column values

Trying to create a Gantt chart look in Excel. I have two columns, A and B (A = start date, B = End Date).
Across the top of the page (Row 2) I have a column with a date for every date of the project (custom formatted with "d" for readability, with the name of the month in Row 1.)
I'm now trying to apply conditional formatting rules to turn the cell in the column a specific colour (say, green) if:
the value in A[this row] is greater-than-or-equal-to [this column]2.
and
the value in B[this row] less-than-or-equal-to [this column]2.
I've dug through a few answers recommending ADDRESS() and INDIRECT(), but I'm stumped on getting this to work. Any thoughts?
You can use AND to combine the conditions. I'm assuming the 'Gantt chart' starts as from column C and the active row is 2 here.
Select C2 and the rest of the row (to 31, 30 or 28/29 depending on the number of days in the month).
Pull up conditional formatting with formula and put:
=AND(C2>=$A2,C2<=$B2)
Pick the format fill green and that should do it
In conditional formatting if you use the first cell of your selection in the formula it automatically turns that into a relative formula.
For example if you use the formula: =A1>5 and select cells A1:B5 it will check each cell to see if its >5 not just cell A1 (so it automatically increments the row and the column for you). Usually this is preferred over using indirect but sometimes indirect is necessary.
So using indirect you can utilize the row() and column() functions. So in your example:
the value in A[this row] is greater-than-or-equal-to [this column]2.
and
the value in B[this row] less-than-or-equal-to [this column]2.
Would look like:
=AND(INDIRECT("A"&ROW()) >= INDIRECT(CHAR(COLUMN()+64)&"2"), INDIRECT("B"&ROW()) <= INDIRECT(CHAR(COLUMN()+64)&"2"))
Hopefully that helps
It works for me without ADDRESS or INDIRECT. This is the formula inside the conditional formatting. If I have to guess what's going on, it's most likely that you are not placing the proper anchors ($).
=AND(C$2>=$A3,C$2<=$B3)

Multiple conditions in Excel conditional formatting

I'm fairly new to Excel and so not very experienced, but I'm trying to get an entire row to highlight red based on 2 other cells, using conditional formatting. The requirements are for the corresponding M cell in the row to have the value 'N' and for the corresponding E cell to have a past date value '<*NOW'.
I've tried a formula based on an answer for multiple conditions:
=AND(M="(N)";E<*NOW)
A formula for formatting an entire row:
=INDIRECT("m"&ROW())="N",("e"&ROW())E<*NOW
And combining the two:
=AND=INDIRECT("m"&ROW())="N",=INDIRECT("e"&ROW())E<*NOW)
(ignore all the asterisks its the only way i could get the NOW to appear)
None of these are working, please help!
Assuming your data starts at row 2 then select all rows, e.g. 2 to 100 and then apply the condition for row 2, i.e. use this formula
=AND($M2="N",$E2<TODAY())
That will now apply to the whole range. The $ signs make it format the whole row

Resources