Multiple values for an AND/OR statement - excel

Is it possible to combine AND (or OR) statement criteria to avoid repeating a cell name?
For example, I want to see if cell C2 contains the any of the numbers 2,3,5,7, or 10. I want to avoid writing
IF(AND(C2=2,C2=3... etc.
and simplify it to an array of the numbers like
IF(AND(C2=[2,3,5,7,10]...
Unfortunately, I have a lot more than just 5 numbers to add so it's getting be very laborious. Anyone have an easier way than repeating the cell name=__ over and over?

You can use an "array constant" like this
=IF(OR(C2={2,3,5,7,10}),"Yes","No")
.....or for a large set of numbers you could put all the numbers in a cell range, e.g. Z2:Z100 and do the same
=IF(OR(C2=$Z$2:$Z$100),"Yes","No")
although when you use a range rather than an array constant the formula becomes an "array formula" so needs to be confirmed with CTRL+SHIFT+ENTER
perhaps better to use COUNTIF and avoid "array entering"...
=IF(COUNTIF($Z$2:$Z$100,C2)>0,"Yes","No")

=IF(ISERROR(MATCH(C2,{2,3,5,7,11},0)),"no","yes")
No need to enter this as an array formula. How it works: MATCH returns a #N/A! error if it can't find the lookup value in the lookup array. ISERROR catches this.
But as barry houdini suggests, you may want to put your numbers in some range e.g. Z1:Z5 instead of hard-coding them into your formula. So you would have this formula instead:
=IF(ISERROR(MATCH(C2,Z1:Z5,0)),"no","yes")

Related

COUNTIFS with unique values Excel

I am trying to produce a count of the number of times different strings come up in an Excel table. An example table, currently in SHEET1, would be this:
I have another table in another spreadsheet where I want to indicate, for each letter on the left in Table 1, how many entries for "za", "zc" or "zd" come up on the right. However, I would only like to only consider one entry of each.
The end result, on row B of SHEET2, would have to be something like this:
At the moment I am using a combination of SUM and COUNTIFS to do the job.
More specifically, applied to the example, I am using the following formula:
=SUM(COUNTIFS(Sheet1!A1:A18,Sheet2!$A1,Sheet1!B1:B18,{"za","zc","zd"}))
The formula is doing some of what is intended. However, it is not counting each entry just one time. Instead, its is counting, for each letter on the left, every entry of "za","zc" or "zd". The table that the formula is returning is as follows:
How can I change the formula so that it does what I intend?
Thank you.
My initial thought would be:
=SUM(MIN (1,COUNTIFS(Sheet1!A1:A18,Sheet2!$A1,Sheet1!B1:B18,{"za","zc","zd"}))
but I’m not where I can test if the MIN will apply properly to the COUNTIFS array of results. ;-)
EDITED: The MIN function is taking minimum of 1 or all of the items in the COUNTIFS array, rather than minimum of 1 and each item in the COUNTIFS array, which is what I was afraid of. Using
=MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"za"),1)+MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"zc"),1)+MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"zd"),1)
will gain the desired results. It is a little clunky, but simpler than an array formula. If you want an array formula, you can use:
=SUM(FREQUENCY(IFERROR(MATCH({"za","zc","zd"},(IF(Sheet1!$A$1:$A$18=$A5,Sheet1!$B$1:$B$18)),0),""),IFERROR(MATCH({"za","zc","zd"},(IF(Sheet1!$A$1:$A$18=$A5,Sheet1!$B$1:$B$18)),0),"")))
This uses the FREQUENCY function to take a set of values and see how many items in another set of values fall within each of the data ranges. Since you need text instead of numbers, we use the MATCH function to find out the first time the value occurs in your list, returning "" with the IFERROR function if it doesn't. (We only need the first occurrence since you don't want to know how many occurrences there are). Since it is text, we use the same input for both arguments for FREQUENCY.
Therefore, if you need to change the values you are looking for or the ranges in which you are searching, make sure to change both! Alternately, you could list the values out somewhere, say in F1:F3, and make a named range for this, another one for A1:A18, and another for B1:B18. Your formula would then look something like this:
=SUM(FREQUENCY(IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),""),IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),"")))
Then you need only change your named range definitions and your formulas would update. :-)
NOTE: Since this is an array formula, you must close out of the cell by pressing CTRL+SHIFT+ENTER rather than only ENTER. When you look at the formula bar, you should see
{=SUM(FREQUENCY(IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),""),IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),"")))}
It does NOT work to enter the curly braces yourself. ;-)
You can use this formula at B1 and fill down:
B1:
=SUMPRODUCT(((Sheet1!$A$1:$A$18=A1)*(Sheet1!$B$1:$B$18= {"za","zc","zd"}))/
COUNTIFS(Sheet1!$A$1:$A$18,Sheet1!$A$1:$A$18,Sheet1!$B$1:$B$18,Sheet1!$B$1:$B$18))

Excel array formula with wildcard

I have an array formula that refers to a drop down cell ($AG$7) to determine which cells to evaluate. This works well, however, I need to include an additional item in the drop down which is "All".
When this is selected, I want the array formula to use "*" to return all instances from the array, but i can't get it to work.
This is the formula I'm currently using;
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")=$AG$7),1,0))}
I've tried using
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7),1,0))}
and
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7&"*"),1,0))}
But these don't work.
Does anybody have any ideas?
Thanks
A explicit = comparison cannot use wildcards. COUTIFS and SUMIFS can. As far as I see, you want to count only (conditional sum 1 and 0).
Problem is, COUTIFS and SUMIFS will not deal with INDIRECT ranges. But INDIRECT can and should (because of its volatile bahavior) often replaced by INDEX- MATCH.
So:
=COUNTIFS(tblSkillsMatrix[role],[#role],INDEX(tblSkillsMatrix,,MATCH($V$2,tblSkillsMatrix[#Headers],0)),"*"&$AG$7)
If $AG$7 is empty then it counts independent of the column named in $V$2.
Btw.: Within a table (ListObject) this needs not be entered as a array formula.
This is not 100% replacement of your formula since it not works if $V$2 is empty and so no table column title is given. Your formula will then look at all columns but this is not possible using COUNTIFS where each additional range must have the same number of columns as the criteria_range1 argument. So if $V$2 shall also can be empty, then this will not work.
If so then you could use
{=SUM((tblSkillsMatrix[role]=[#role])*(LEFT(INDIRECT("tblSkillsMatrix["&$V$2&"]"),LEN($AG$7))=$AG$7))}
Advantage: both $V$2 and $AG$7can be empty.
Disadvantage: Volatile behavior of INDIRECT and this formula then must be a array formula even in a ListObject-table. It must be confirmed using Ctrl+Shift+Enter.

How can I specify a range in a formula?

I know that a formula can be applied on a range of cells by utilizing an intermediate column, but can I directly specify a range in a formula somehow?
E.g., is it possible to do something like this, to get the sum of the results of dividing each cell in E12-E26 by each cell in C12-C26:
=SUM(E12/C12:E26/C26)
An answer, either positive or negative, would be accepted.
You can do what you are wanting to do with an Array formula or CSE formula (Different name for the same thing). To do this, enter:
=SUM(E12:E26/C12:C26)
Then, instead of hitting "Enter" on the keyboard use Ctrl+Shift+Enter (CSE). This will put squirrely brackets around the formula and will apply the division to each pair in the range and then sum the results.
You could also use =SUMPRODUCT() for this, which essentially acts like an array formula, but with the Ctrl+Shift+Enter nonsense:
=SUMPRODUCT((E12:E26)/(C12:C26))
Sumproduct is useful since it will do logic inside the parantheses first across an array/range of cells then sum up the results. You can pull of some cool stuff with the formula, but it's also a beast and cat get out of control pretty easily.

IF function with two cell ranges

I have two sheets with the same line of cells, for example, A1:A5.
I need to check if the value of every cell in Sheet1!A1:A5 is equal to Sheet2!A1:A5 but the hitch is the values will be letters, and all values are different. Simply typing the formula got me a #VALUE! error.
I know I can just write the formula:
=IF(Sheet1!A1=Sheet2!A1;1;0)
and then simply retype it in a number of cells with different values, but I'm looking for a way to shorten the formula.
Any suggestions?
To shorten the formula use array function. With that you will be able to check the whole range at once.
=IF(AND(Sheet1!A1:A5=Sheet2!A1:A5);1;0)
After typing the formula press Ctrl+Shift+Enter instead of just Enter key to confirm array formula.
This one is a little shorter
=(Sheet1!$A1=Sheet2!$A1)
You could use
AND(EXACT(Sheet1!A1, Sheet2!A1), EXACT(Sheet1!A2, Sheet2!A2), EXACT(Sheet1!A3, Sheet2!A3), EXACT(Sheet1!A4, Sheet2!A4), EXACT(Sheet1!A5, Sheet2!A5))
But in the following way:
Have a separate column with the code (let's say, column G)
EXACT(Sheet1!$A1, Sheet2!$A2)
To the column next to that, have a single cell with the code
AND(G1:G5)
Use the AND() function:
IF(AND(Sheet1!A1=Sheet2!A1,Sheet1!A2=Sheet2!A2,Sheet1!A3=Sheet2!A3,Sheet1!A4=Sheet2!A4,Sheet1!A5=Sheet2!A5),1,0).
EDIT
Not realy sure about your aim,
If you want it short because it is too difficult to write the above function, then try the method below:
=IF(CONCATENATE(Sheet2!A1,Sheet2!B1,Sheet2!C1,Sheet2!D1,Sheet2!E1)=CONCATENATE(Sheet1!A1,Sheet1!B1,Sheet1!C1,Sheet1!D1,Sheet1!E1),1,0)
But this is not without catch, it could return false positive. So use it with care. To overcome the false positive, I could only make the formula longer (but still relatively easy to write out).
=IF(CONCATENATE(Sheet2!A1,"|",Sheet2!B1,"|",Sheet2!C1,"|",Sheet2!D1,"|",Sheet2!E1)=CONCATENATE(Sheet1!A1,"|",Sheet1!B1,"|",Sheet1!C1,"|",Sheet1!D1,"|",Sheet1!E1),1,0)

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.

Resources