Excel - conditional formatting with multiple criteria doesn't work - excel

I'm trying to color a cell red when the contents matches (at least) one of two other cells.
I've been trying this:
=AND(COUNTIF(CB17:CE17;CB55)=1,COUNTIF(CB18:CE18;CB55)=1)
and this:
=AND(COUNTIF(CB17:CE17;CB55)=1,COUNTIF(CB18:CE18;CB55)=1) = TRUE
but Excel doesn't recognize this as a formula ("There's a problem with this formula. Not trying to type a formula? ...")!
I can't get any conditional formatting formula to work with the AND operator it seems (and my Excel is the English version).
Any advice?
Edit:
some screenshots to clarify:
Error I get when using AND or the '+' operator in the formula:
Current contents of the criteria cells:

AND will be true of the expression inside results to true.
First of, you do not need to evaluate to 1 for AND to work. Secondly, you used ; and , instead of ;. You mentioned changing them to ; solved it.
So =AND( COUNTIF(...), COUNTIF(...) ) would already be enough.
Your formula would be =AND(COUNTIF(CB17:CE17;CB55);COUNTIF(CB18:CE18;CB55))
Now that being said, you want it to work for either of the values being true, that means you need an OR operator, not AND. The syntax is basically the same, just replace AND with OR. Your formula would be =OR(COUNTIF(CB17:CE17;CB55);COUNTIF(CB18:CE18;CB55))
Now since your ranges are CB17:CE17 and CB18:CE18 and they both evaluate to CB55, you could of course simply use =COUNTIF(CB17:CE18;CB55) which would be a lot simpler. I have explained the AND so you can learn from it, in case you really need to use more ranges and/or evaluate against different cells.

Related

Excel formula doesn't work for conditional formatting

I did this formula to get a percentage of wrong documents / all the documents:
=SUM(COUNTIF(F5:M5; {"*Pending*";"*Rejected*";"*Expired*";"*Empty*"})) / COUNTIF(F5:M5;"<>*N/A*")
The thing is I want this formula to be a conditional formatting for the whole column, but I get this error:
You may not use reference operators such as unions intersections and ranges, array constants or the lambda function.
Any way to solve it? Thanks!
Two things:
Instead of checking <>*N/A*, you might use the function IfNA().
What kind of conditional formatting are you using? Some are switches (put a colour or not), and some are progressable (like changing colour bars).
For the switches, you need a formula which gives TRUE or FALSE.
You can't use array constants in CF formula, but you can enter that constants in some range and use range in formula:
=SUM(COUNTIF(F2:M2;$T$2:$T$5)) / COUNTIF(F2:M2;"<>*N/A*")

Excel AVERAGEIFS like formula which treats text as 0 and not ignore them

Is there anyway to use AVERAGEIF or a similar function that treats text value as 0 and not just ignore them. Something similar to AVERAGEA function, but with multiple criteria?
I saw a similar question here which wanted to treat blanks or empty cells as 0. The answer suggested to use array formulas to add 0 to it. Not sure if it holds for other texts as well.
If you do NOT want to count blanks, you can use:
=SUM(A1:A10)/COUNTA(A1:A10)
(If you wanted to count blanks, something like sum(rng)/rows(rng) would do it)
You can use a similar technique to include criteria for AVERAGEIF
For example, given:
If you want to average all the values in Column A where a is in Column B:
=SUMIF(B1:B10,"a",A1:A10)/COUNTIF(B1:B10,"a")
It would be similar to the other link:
=AVERAGE(IFERROR(--A1:A10,0))
It is an array formula and with Excel to confirm an array formula one must use Ctrl-Shift-Enter instead of Enter when exiting edit mode.
One Note: This will treat blanks as 0 also, so make sure it only refers to the data set desired.
Similar to Rohan's post, I personally would break it down to see how many strings are pushing down your average. But if you want it all in one cell:
That is an asterisk inside the double quotes
one cell: =SUM(E1:E10)/(COUNTIF(E1:E10,"")+COUNTIF(E1:E10,"<>*"))
break down:
Non-Numeric: COUNTIF(E1:E10,"*****")
Numeric: COUNTIF(E1:E10,"<>*")
spreadsheet link
1

Conditional Formatting for a cell containing a particular equation

I tried using steps identified here but realized that it refers to cells that have any formula in them. I would like to format based on one particular formula.
Is it possible to use conditional formatting to refer to a cell that only has a particular equation, for example an "IF" statement in the cell?
You don't mention what the formula is but do mention an IF statement so suggest (though clearly not Conditional Formatting in the conventional sense), HOME, Editing - Find & Select, Find what:
=*IF(*
Find All, Ctrl+A, and formatting of your choice.

IF function - is there a way to avoid repeating formula

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.

Are Excel's Conditional Formatting Formulas Broken

Ok, so I am using excel 2007, and am trying to do some fairly complicated conditional formatting. Consider the following:
=1=1
Highlights the cell.
=INDIRECT(ADDRESS(2,COLUMN()))=5
Highlights cells in a column where the second row has 5 in it.
=AND(1=1,2=2)
Highlights the cell.
=AND(INDIRECT(ADDRESS(2,COLUMN()))=5,1=1)
Highlights nothing?
It seems that any complicated things I can do simply don't work. Even when the expression evaluates true when put into the cell directly, it doesn't always make the formatting work correctly. Has anybody else experienced this? Anybody have a workaround?
I get the same results on Excel 2003. Even your second example (which is missing the "=" BTW) has strange behaviour, since not all the cells in the column are highlighted until I minimize and restore the window.
I was able to get it to work by defining a name "foo" as "=INDIRECT(ADDRESS(2,COLUMN())" and then using "=AND(foo=5,1=1)" for the condition.
To define a name, use Insert->Name->Define, type "foo" (or whatever) in the Name box, and the formula in the Refers To box. Normally a named range refers to a fixed cell, but it can also refer to a constant, a formula, a range, etc. See Chip Pearson's site for details.
In Excel 2007, it would not accept my conditional format of
=AND(expr1, expr2)
where expr1 and expr2 were fairly complicated.
Just by adding an extra comparison, it started taking it.
=TRUE = AND(expr1, expr2)
I suspect it is validating for the presence of some basic operator, and it would not accept the condition without it.

Resources