I have a three page work book. Pages 2 and 3 hold similar data but from different time periods (Cases Current, Cases Historic). Takes together the information from Cases Current and collates certain information using COUNTIF.
I have been trying to get a conditional format to change the text colour if the COUNTIF from Cases Current is higher or lower than a COUNTIF on the same data in the historic page. E.g.:
Cell C3 has the following formula :
'=COUNTIFS('Cases '!$D:$D,C$2,'Cases '!$C:$C,$A3) and displays the figure 7.
I have used a IF formula in a regular cell and this following does work. (In this example the COUNTIF returns 10.)
IF(C3<COUNTIFS(Compare!$D:$D,C2,Compare!$C:$C,$A3),"Lower","Higher")
I have used the same formula in Conditional Formatting but it triggers no change in font colour. I have stripped the two end conditions off the end of the IF statement as the conditional formatting is triggered on a TRUE return.
=IF(C3<COUNTIFS(Compare!$D:$D,C2,Compare!$C:$C,$A3)
This formula is incorrect for what you're attempting:
=IF(C3<COUNTIFS(Compare!$D:$D,C2,Compare!$C:$C,$A3)
It is actually an incomplete IF statement - try to copy it into a cell, and you will see that you don't have an argument for IF's "TRUE" state (which is required). Instead, you don't need an IF statement at all, just:
=C3<COUNTIFS(Compare!$D:$D,C2,Compare!$C:$C,$A3)
So this will check the value of cell C3, then check the value of the COUNTIFS function, and then check that C3 < COUNTIFS. If it is, it's a true statement, resulting in TRUE. Otherwise, it returns FALSE.
Related
I have a very unexpected outcome of OR used in conditional formatting formula.
Take a simple sheet:
Now add conditional formatting with formula:
=B4:G7=$A$1
It works as expected, with true value for cells containing "A"
Now try another formula:
=OR(B4:G7=$A$1;FALSE)
Which should basically mean the same. Well.. the outcome is very different:
What's even more confusing is that chaging, for example, cell I7 to "a" influences the outcome:
This is exact formula I have used, with "LUB" being "OR", and "FAŁSZ" being "FALSE":
Edit:
I forgot to stand the question. Question is: why does it happens? How do I use OR correctly in that context?
First, you need to understand how absolute and relative reference works in conditional formatting. Formula references refer to the very top left cell in the applies to range. If the formula has relative references, then for each applies to cell the cell in the formula reference shifts accordingly, if absolute - then the reference does not change. In your case, the formula =B4:G7=$A$1 is applied to cell B4, to cell C4 - =C4:H7=$A$1, to cell B5 - =B5:G8=$A$1, the same thing happens with formula =OR(B4:G7=$A$1;FALSE).
Second, the formulas =B4:G7=$A$1 and =OR(B4:G7=$A$1;FALSE) work completely differently. The first case actually compares the first cell of the range with $A$1, i.e. in the previous example =B4=$A$1, =C4=$A$1, B5=$A$1. In the second case, the range is divided into cells, they are compared with $A$1 and the results are passed to the OR function, i.e. =OR(B4:G7=$A$1;FALSE) => =OR(B4=$A$1;C4=$A$1;D4=$A$1 ... G7=$A$1;FALSE) and if even if one is TRUE, then you also get the resulting TRUE.
Consequently, we can conclude that the following formula will work correctly for you in this case:
=OR(B4=$A$1;FALSE)
I would like to highlight a cell which is the closest the a specific value but still lower (below) or equal according to a formula.
For instance, if I have 14 in B4, I would like the cell 13 (or 14) highlighted but not 15 in the range.
The value I need to look for would be in B4 and the range that I must apply the highlight formula to would be L1:L371.
So far, the best result I was able to achieve was with (from what I was able to find using Google or Stackoverflow already existing content):
=SMALL($L1:$L317,COUNTIF($L1:$L371,"<="&$B$4))
Although, not only it selects the value I am looking for, it also selects all the values below.
May someone provide me some help so I can achieve the expected result please?
Thank you for your time and help, it is greatly appreciated.
Based on your description, it seems like you need to find the maximum value of a range (L1:L137) that is less than or equal to an input variable (B4) and highlight that value in the search range. Depending on what version of Excel you have, here's what you need to do:
For Excel 2019 or Excel 365
You can use the MAXIFS function. You would actually put that function in a conditional formatting rule, but more on that in just a second. As a test, put the following code in cell B5:
=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
The first argument is your "MAX_RANGE", or the range that contains the values you want to find the max of. It is also our "CRITERIA_RANGE", the second argument. The third argument is the criteria itself, which is that the search range must be less than or equal to the value in cell B4. Essentially, we are going through the list and creating a subset of numbers that are less than or equal to our search value, and discarding the rest. I assume you are aware of relative vs. absolute referencing, since you used the "$" anchor in your referenced code. If not, here's a description of the difference.
Now, the result of that formula should be the highest number in the list less than or equal to the search value, but that doesn't highlight it for us in our list. To do this we need conditional formatting. To do this, highlight your data range (L1:L137) and go to the conditional formatting drop down on the home tab of the menu ribbon. Choose "New Rule". On the dialog that pops up, choose, "Use a formula to determine which cells to format". In the formula input box, enter:
=L1=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
This will compare whether the value in the cell being evaluated (L1, for instance) is equal to the result of that formula we talked about above. Since L1 is only relatively referenced, this formula will work for every cell in the data range.
Now, before you hit "OK" on the dialog, select the Format button. This will allow you to adjust your highlighting and formatting as you desire. Click "OK" on the formatting dialog, then "OK" on the Conditional Formatting Rule dialog. This should now highlight any data cell that is equal specifically to the result of our formula, and not everything that's less than our value.
For Earlier Versions of Excel
The concept is the same in earlier versions of Excel, but unfortunately, the MAXIFS function is not present in these versions. Instead, we must use an array formula. Array formulas are a whole other can of worms, but ExcelJet is an excellent resource. In fact, they talk about this very issue, here.
Unfortunately, we can't put the array formula in the conditional formatting formula like we did above, so we'll need to put this formula on a cell in the worksheet, then the conditional formula should reference that new cell. So in cell B5, if you put:
=MAX(IF(L1:L137=B4,L1:L137)
And then, instead of pressing Enter, you must press Ctl + Shift + Enter
This keyboard combination will tell Excel that you are trying to enter an array formula. If you don't press these keys, then the formula will error. Once you have entered the array formula, if you put your cursor in cell B5, you will see the formula bar at the top has added curly braces ({ , }) around the formula to look like
{=MAX(IF(L1:L137=B4,L1:L137)}
This leads to the same result as above, but is just achieved slightly differently. Now, following the same process described above for conditional formatting, you will simply set the formula to:
=L1=$B$5
And that should be it! Hope this helps!
I'm trying to format a row based off a cell within it.
My table runs from A6 to K207 and I want to highlight the row in bold if the cell in B6 starts with SA.
I tested my formula
=IF(LEFT(TRIM(B26),2)="SA","YES","NO")
in another cell and that works ok - but I can't get the formatting to work for some reason...
I removed the yes/no and placed into a conditional formatting rule applying to my whole table and it doesn't work. Even if I apply it to just a row it doesn't work.
Am I using if statements within conditional formatting right? I've looked on Google and here and have found similar issues but not the same one, they've all used set values/ranges but not a 'starts with' condition.
The conditional formatting is an IF statement by default, and formats depending on whether the formula is TRUE or FALSE therefore an additional IF statement is not needed:
=LEFT($A6,2)="SA"
This will do the trick, apply this to the entire range as needed.
It will test whether all cells in column a start with "SA" and highlight that cell. If it is applied to a range including more columns, the $ sign fixes the column as A so tests that cell and highlights every cell based on that, effectively highlighting the entire row.
You can also use an OR statement to pass multiple arguments like so:
=OR(LEFT($A6,2)="SA",LEFT($A6,2)="BO")
Welcome to SO. When doing conditional formating (CF) in a formula, you need to apply references.
Switch between relative, absolute, and mixed references
Right now your formula is =IF(LEFT(TRIM(B26),2)="SA","YES","NO"). You are using relative references, so the CF rule will check according to their position. In A6 it will check the value of A6, and I guess you would like the rule to check the value always in column B.
So the first fix you need in yor formula will be (it needs more, hold on)
=IF(LEFT(TRIM($B26),2)="SA","YES","NO")
Now, it will check always the value in column B. But we need to fix something else. To make the CF based on a formula, you need to formulate something that returns a boolean value, True or False. Your values Yes or NO are strings (text), not boolean, so they don't work.
So try this formula and let's see if it works:
=IF(LEFT(TRIM($B26),2)="SA",TRUE,FALSE)
An IF Functions allows you to make a question where the answer must be yes/no (true/false) and, depending of the answer, Excel will do an option or another.
With CF rule based on a formula, you just need the question itself, you don't need to choose an option. So the short version of this formula, as #Plutian states in his answer, is just doing:
=LEFT(TRIM($B26),2)="SA"
Try to adapt this to your needs.
I am trying to get a cell to highlight, given 2 criteria. The cell must be blank, and today's date must be after a predetermined date that is listed in another cell(R8C3). I'm also using R1C1 notation, but understand A1 notation as well.
The part I don't understand is that each criteria works independently, but when I use the AND function it no longer works. The relative references are used since this formatting will span multiple columns.
=ISBLANK(RC) evaluates to true and highlights correctly.
=TODAY()>=INDIRECT(ADDRESS(ROW(R8C3),COLUMN(RC))) evaluates to true and highlights appropriately.
=AND(ISBLANK(RC),TODAY()>=INDIRECT(ADDRESS(ROW(R8C3),COLUMN(RC)))) Does not highlight.
A slightly modified version pasted in a cell ( to check that the formula works) evaluates appropriately to TRUE or FALSE.
=AND(ISBLANK(RC3),TODAY()>=INDIRECT(ADDRESS(ROW(R8C3),COLUMN(RC3))))
I'm using Excel 2010 (version 14) on a Windows 7 device.
Try this:
=(J$1>K$1)*(ISBLANK(A1))
where J1 has the current date and K1 the threshold date.
The indirect statement seems unnecessarily convoluted... why not just use a mixed reference?
=AND(ISBLANK(RC), TODAY()>=R8C)
Can't believe I don't know this, but is there a way to avoid repeating a formula in an if statement if the logical test is dependent on it?
i.e.
=IF((SUMIFS formula)=0,"",SUMIFs formula)
I want to replace that SUMIFS function in the false scenario with something short that will tell it to just programmatically repeat the formula it originally tested for. Repeating the formula twice has to have detrimental effects on processing speed. Negligible, maybe, but want to go for best-practices here. Thanks.
You can force an error like #DIV/0! and then use IFERROR, e.g.
=IFERROR(1/(1/SUMIFS_formula),"")
You can assign a Name to a formula and use the Name..............See:
Assigning a name to a formula
Relevant excerpt -
For example, let's suppose we frequently use a formula like:
=SUM(A1:A100)-SUM(B1:B100) and this resides in A101 and is copied across many columns on row 101. It would be better in this case to
create a custom formula that does this in each cell on row 101. Here
is how;
1) Select cell A101 (this is vital).
2) Go to Insert>Name>Define and
in the "Names in workbook" box type: SalesLessCosts
3) Now click in
the "Refers to" box and type: =SUM(A1:A100)-SUM(B1:B100) then click
Add.
Now you can replace the formula in cell A101 with: =SalesLessCosts.
You can also copy this across row 101 and it will change its relative
references just as the formula =SUM(A1:A100)-SUM(B1:B100) would. The
reason it does this is all down to the fact we selected A101 before
going to Insert>Name>Define and used relative references in
=SUM(A1:A100)-SUM(B1:B100) when we added it to the "Refers to" box.
If all you need to do is hide zeroes, there is an easy way:
Select all cells where you wish to hide zeroes
Go into Custom Number Formatting
Set format to "General;General;"
The custom formatting has a structure of [positive numbers];[negative numbers];[zeroes]
By making the last part blank you are effectively hiding zeroes, but showing everything else.
The advantage over conditional formatting is that you can use this on any background.
A neat trick which I sometimes use is to hide the cell value completely by using a custom format of ";;;". This way you can put images inside the cells, like the conditional formatting ones, and not see the value at all.
Try using the SUBSTITUTE function like this :
=SUBSTITUTE( VLOOKUP( H4; $D$5:$E$8; 2; 0 ); $H$1; $I$1 )
Here is an example:
Here the formula I don't want to repeat twice is the VLOOKUP function.
The result of VLOOKUP is a string found in another table (ex : "Green").
I want to check if that string matches a specific string value in $H$1 (here, "Yellow").
If it does, SUBSTITUTE replaces it with$I$1 (the error string you want. Here, "FORBIDDEN").
If it doesn't, it displays the VLOOKUP result string (the normal authorized output, like "Green").
This is useful for me because my actual formula is quite long, so I don't want to write it twice.
I also dont want to use two different cells, because I'm already applying this formula on 10 columns, meaning I should add an extra 10 columns to make it work.
In some scenarios, MAX() or MIN() can do a wonderful job.
E.g., something like this:
=IF(SUMIFSformula>0,SUMIFSformula, 0)
Can be shortened to this:
=MAX(0,SUMIFSformula)
The LET formula can be used for this exact scenario. You can define the formula as a variable and then within that same cell you can reference the variable in your formula.
The LET formula format looks like this:
=LET(name,name_value,calculation)
SUMIFS Example
Here's how it would work with your SUMIF example so that you don't have to repeat the formula:
In this screenshot we have an array A1:B7. We want to sum the values (Col B) if the name in ColA is "apple".
For this we have a standard SUMIFS formula of
=SUMIFS(B1:B7,A1:A7,"apple")
The formula is showing in E2. The result is shown in E3.
To put this into the IF statement without having to repeat the formula we can use LET as shown in the screenshot.
We create a variable with the SUMIFS formula as the value of that variable. We then write our IF statement using the variable name instead of rewriting the formula multiple times.
=LET(name,name_value,calculation)
Variable name: sumapples
Variable value: SUMIFS(B1:B7,A1:A7,"apple")
Calculation: IF(sumapples=0,"",sumapples)
Put together in the LET function it looks like this:
=LET(sumapples,SUMIFS(B1:B7,A1:B7,"apple"),IF(sumapples=0,"",sumapples))
This LET function can be used in any Excel formula, and is very useful for shortening long formulas that have repetition.
Optional: Extra complexity
If you want to you can get extra complicated by naming multiple variables.
=LET(name,name_value,name2,name_value2,calculation)
Since Excel 2007, the IFERROR statement does what the OP asked. From the help file:
Description:
Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula. [italics mine]
Syntax:
IFERROR(value, value_if_error)
I've since realised that this was already answered by #barry houdini above.
Here is a hack - depending on whether you are just interested in the displayed value, or whether you need to use the value in another formula:
Put your SUMIF formula in the cell (without the IF part)
Create a conditional formatting rule which sets the font color to the background color when the cell value is 0
And hey presto, you get the desired result.
As I said - it's a hack, but it does prevent the double evaluation.
There is no "clean" solution that I am aware of.