Is it possible to combine AND() and OR() in Excel formulae? - excel

Is there an easy way to combine AND() and OR() within Excel formulae. IE:
IF(OR(AND(A2="",B2=""),AND(A2="(blank)",B2="(blank)")),"BLANK","NOT BLANK")
Or do you need to use nesting to achieve the same end goal?

If you're a programmer, this synax might appeal more to you:
=IF((A2="")*(B2="")+(A2="(blank)")*(B2="(blank)"),"BLANK","NOT BLANK")
If you actually need to check some arbitrarily sized range, whether it's empty (using "(blank)" as an alias for empty value), you might use this formula instead:
=IF(COUNTIF(CheckedRange,"(blank)")=COUNTA(CheckedRange),"BLANK","NOT BLANK")
you can easily extend the list of empty value aliases by just adding more COUNTIFS

The formula you provided is valid and works.
Also, in the formula you posted, you are already using nesting (the AND is nested within the OR).
On a side note, the fomula will only work if both A2 and B2 are blank, or if A2 and B2 are both set to (blank). A potentially better formula for what you're trying to do is AND() first then OR() like this:
=IF(AND(OR(A2="", A2="(blank)"), OR(B2="", B2="(blank)")), "BLANK", "NOT BLANK")
With this new formula, you would get the result BLANK if A2 is empty, and B2 is (blank), and vice versa.

Related

How do I add a condition to my Excel formula?

I would like Excel to execute the following formula ONLY if B2 has a value in it. How do I write that into this formula?
=IF( ISNUMBER( MATCH( MATCH(A2,AllResources4[Title],0), SUBTOTAL(3,OFFSET(AllResources4[[#Headers],[Title]], ROW(AllResources4[Title]) - ROW(AllResources4[[#Headers],[Title]]),))*(ROW(AllResources4[Title]) - ROW(AllResources4[[#Headers],[Title]])),0)), VLOOKUP(A2,AllResources4[#All],8,0),"")
I want Excel to look for A2 in the "AllResources4" table or return the result only if its corresponding B2 cell has a value. Same with A3, A4, etc.
Using ISBLANK is the way to go. However, I sometimes get sketched out since you can't see how ISBLANK is actually working. That being said, it sometimes it worth inserting a column next to your data dedicated to the ISBLANK function. Something like:
=IF(ISBLANK(cell),0,1)
Then, you can check to make sure that the function works and use sort/filter to get the cells that you need to use the function on. Of course, if you want the function applied to all the data but only to work on the non-blank cells, you could add a clause to the IF for whether the column value we jsut made is one or zero.
NOTE* if you are working with numbers, the count function might also be helpful
This worked! =IF(B2=0,"",Formula)
=IF(ISBLANK(B2),Formula,"")
The ISBLANK function will check if a cell is blank (has no value), combined with a if statement will run your formula only when B2 has something in it. The above formula will output nothing if B2 is blank.

Add ISBLANK validation before comparing values

Actually I am not much familiar with Excel formulas.
I am using dates from Column E and F to color cells
=+IF(AND(DATEVALUE(I1)>=DATEVALUE($E$2);DATEVALUE(I1)<=DATEVALUE($F$2));1;2)
This formula is working fine when there is value present for column E and F.
if there is no value present for E and F it results error value.
I know little bit about ISBLANK() function.
I want to add condition to check cell E2 and F2 are not blank before my existing formula. Can you please suggest the proper syntax?
Thanks
You need to wrap your current operations in a function that checks each cell. If either of them is blank, you probably want to return "", or else the IF will automatically return FALSE.
You could use a chain of ifs, but since ISBLANK returns a boolean we can instead use an OR.
OR(ISBLANK($E$2),ISBLANK($F$2))
will return TRUE if either of the two cells is blank, so you'll need to put your operations into the False option, giving us:
=IF(OR(ISBLANK($E$2),ISBLANK($F$2)),"",IF(AND(DATEVALUE(I1)>=DATEVALUE($E$2),DATEVALUE(I1)<=DATEVALUE($F$2)),1,2))
Although you seem to have strange syntax, probably down to language settings or something, so you may prefer this:
=+IF(OR(ISBLANK($E$2),ISBLANK($F$2));"";IF(AND(DATEVALUE(I1)>=DATEVALUE($E$2);DATEVALUE(I1)<=DATEVALUE($F$2));1;2))
Use this condition which will ensure that both are not blank:
IF(OR(ISBLANK($E$2),ISBLANK*($F$2), your operations, "")
Thanks to all for your suggestions and valuable time.
But after reading some office.com documentation I find a proper solution.
IFERROR is more suitable than ISBLANK for my problem
instead of checking for blank cells I directly apply DATEVALUE() on cell . so if there is a blank cell DATEVALUE() return error and IFERROR() condition set value 2.
=+IF(AND(DATEVALUE(G1)>=IFERROR(DATEVALUE($E$7),0),DATEVALUE(G1)<=IFERROR(DATEVALUE($F$7),0)),1,2)

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.

Multiple values for an AND/OR statement

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")

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