Difference between criterion and logical expression in excel - excel

I came across an interesting question regarding excel and I would like to know the theoretical background of it, google didn't help...
Lets say you want to sum the values of column B if the date (stored in column A) is this week. So what you do is =SUMIF(A:A;">"&TODAY()-8;B:B)
I am interested in the explanation why there is a need for the " " and the & symbols.
If I use an if function, there is no need, eg.: =IF(A3>TODAY()-8;TRUE;FALSE) works perfectly.
So why is it that
=SUMIF(A:A;>TODAY()-8;B:B) ----> formula parse error
=IF(A3>TODAY()-8;TRUE;FALSE) ---> works
I noticed that the IF function requires a "logical test", while a SUMIF function requires a "criteria", but what is the difference between a criteria and the logical expression theoretically, how can I explain why this is necessary to use? (other than "just because excel works this way...)

The majority of the question appears answered by a comment from #Jeeped
Excel is looking for specific information for each of these parameters. First, I would recommend you familiarize yourself with Excels operators a little bit.
=IF(logical_test, [value_if_true], [value_if_false])
In this, we are looking for a Boolean expression from the logical test. Something to resolve to either true or false, with possible actions for either scenario. So comparison operators would fit naturally in there.
=SUMIF(range, criteria, [sum_range])
In this function excel is doing a direct comparison of the criteria to values existing in the range. If the criteria is true, then add to the sum array and move to the next. Once all true statements are found, sum them all and provide the result. So a comparison within the criteria doesn't work as the function is already doing a comparison of the criteria to the range.
The difference here, is the logical test in the IF statement must resolve to true or false and it is not expecting a string. The criteria in the SUMIF statement must exist as a value within the range to be summed. So the criteria must resolve to a value that exists within a range. This is done by interpreting the SUMIF criteria as a string to compare it to a ranges value string.
Why then is the quote and ampersand needed?
It boils down "just because excel works this way..."
Or, more specifically, quotes and ampersands are part of the basic syntax and operations of excel itself.
Or, in a totally non-technical sense:
Excel operators are like problem children. They always need both hands held. Always something on the left and right. They don't like leaving a hand free.

Related

How to concat the formula in all reference cell into one formula by VBA?

after googled a while seems no one has such question. Basically, I have fews cells has formula and referencing each other. Below is an example. How can I make them into one formula?
A B C
1 1 =(A1+C1) =B2
2 3
So, I want to parse them into =1+3. How can I do that
Not easily is the answer. I've done similar before.
Casting my mind back, I read in the formula into VBA, i.e.
F1 = cells(1,2).Formula
Then parse that by operators. i.e.
F1b = split(F1, "*", -1)
You'll need to do it for all operators and store the operators and their location too though! So I'd advise a custom function that can work with many operators at once rather than using split over every potential operator (even if the custom function uses split).
After getting the various cell addresses, you recurse through them doing the same thing, until you build up two array of arrays, the first containing the tree of cell addresses, the second the tree of operators.
Then you'll need to flatten the tree.
This answer isn't very helpful in terms of code - I started doing a bit and remembered its a can of worms. For instance, do you expect any IF() or AND() within formulae?

How to use OR condition with COUNTIFS?

I want to count all the cells in the column where the date is either after 30th April[&DATE(2018,4,30)] or it is blank[""].
=Countifs($N$2:$N$24388,{">"&DATE(2018,4,30),""})
Going by the syntax, I feel I am doing it right since it's not throwing any error either but it's not giving the correct result.
Later I find out that it's rendering only up till the first condition i.e. the above formula is rendering like this:
=Countifs($N$2:$N$24388,{">"&DATE(2018,4,30)})
It's not accounting for the condition meant for blank("") cells. So the result is incorrect or lesser than what it's supposed to be.
Later I changed the order of condition i.e. to this
=Countifs($N$2:$N$24388,{"",">"&DATE(2018,4,30)})
Now, it is acknowledging the only ("") condition and ignoring the latter one. So, it's giving the same result as what the below formula would give.
=Countifs($N$2:$N$24388,{">"&DATE(2018,4,30)})
Eventually, I tried the following and it worked but it's a lengthy formula and defeats the purpose of having Countifs
=sum(COUNTIFS(N2:N24388,{""})+COUNTIFS($N$2:$N$24388,{">"&DATE(2018,4,30)}))
As I stated, I expect to get the count of all the cells which are either blank OR satisfy a given condition.
Just add two separate COUNTIFs together:
=COUNTIF($N$2:$N$24388,">"&DATE(2018,4,30))+COUNTIF($N$2:$N$24388,"")
This doesn't defeat the purpose of COUNTIFS as you suggest, since COUNTIFS counts values which satisfy all the criteria you specify, whereas you're interested in values which satisfy at least one criterion.
you can use COUNTBLANK:
=COUNTIF($N$2:$N$24388, ">"&DATE(2018, 4, 30))+
COUNTBLANK($N$2:$N$24388)
Your COUNTIFS returns an array of results, so just sum them:
=SUM(Countifs($N$2:$N$24388,{">"&DATE(2018,4,30),""}))

Avoiding duplicate long expressions in Excel

I often find myself writing Excel formulas that have something like this in the formula:
= IF(<long expression>=<some condition>,<long expression>,0)
Is there any way to accomplish this without needing to type out <long expression> twice (and also without using helper cells)?
Ideally, something that works similar to IFERROR, i.e.
= IFERROR(<some expression>,0)
This checks if <some expression> would return any type of error, and if it doesn't, it automatically returns <some expression> (without needing to again explicitly type it out a second time).
Is there an Excel function (or combination of Excel functions) similar to IFERROR but instead of checking an error condition, it checks a general (user-defined) condition based on the formula?
When it comes to formula efficiency and calculation speed, using helper cells can be of great value, even if they may initially muck up the spreadsheet design.
Put calculations into a helper cell and refer to the helper cell in the IF statement. That way the calculation will only happen once.
This method is preferred by spreadsheet auditors over the alternative of packing everything into one formula, because it is also much easier to follow and pick apart.
With careful spreadsheet planning you can house helper cells in a different (hidden) sheet or in columns that you hide to tidy up the design.
This answer applies to Excel 365 as of March 2020. A new function is now available in Insider builds of Excel. It is called LET() and is used to define, and assign a value to, a variable that can then be used multiple times inside the braces of the LET() function.
Example:
=LET(MyResult,XLOOKUP(C1,A1:A3,B1:B3),IF(MyResult=0,"",MyResult))
The first parameter is the name of a new variable, MyResult. The variable is assigned a value with the second parameter. This can be a constant, like a number or a text, or like in this case, a formula.
The third parameter is a calculation that can use the variable value.
In the following screenshot, the Xlookup returns a 0 because the found cell is empty.
In the next formula down, the Xlookup is wrapped in an IF statement, evaluated, and then repeated. This is the approach where the calculation is duplicated, as described in the question.
The third formula shows the LET() function and its result.

simplifying Excel formula currently using INDEX, ROW, SUMPRODUCT and IFERROR

Does anyone have any brilliant ideas to simplify this difficult formula? Don't panic when you see it, I will try to explain.
=IFERROR(INDEX(rangeOfDesiredValues,(1/SUMPRODUCT((rangeOfSerials=$D20)(rangeOfApps=cfgAppID)(rangeOfAccessIDs=cfgAccessID)*ROW(rangeOfDesiredValues))^-1)),"")
Currently I am using SUMPRODUCT to do the equivalent of a VLOOKUP with multiple columns as criteria. Usually that only works with number results, but since I need to find text, I'm using SUMPRODUCT in combination with ROW and INDEX.
Unfortunately when no cell is found, my SUMPRODUCT returns 0. This causes the formula to return the incorrect cell rather than blank. For this reason I am running the result through this calculation:
(1 / result)^-1
This way results of 0 become an error, and other results remain unchanged. I feed this into IFERROR, so that errors become blanks.
Does anyone know how to make this neater? I am not able to create new columns in any of my spreadsheets.
It's always best to avoid using multi-condition summing functions like SUMPRODUCT when you want to find a single value (it would obviously give you an incorrect result or error if there's more than one row which matches all three conditions, I assume you expect one match at most here?). ROW function can also be problematic if you insert any rows in the worksheet.....
There are several approaches that can work. For a single formula, using MATCH is the most common - MATCH will only give the correct position or an error so no problems with zero values. That would look like this:
=IFERROR(INDEX(rangeOfDesiredValues,MATCH(1,(rangeOfSerials=$D20)*(rangeOfApps=cfgAppID)*(rangeOfAccessIDs=cfgAccessID),0)),"")
That's an "array formula" that needs to be entered with CTRL+SHIFT+ENTER......or you can make it into a regular formula with an extra INDEX function like this
=IFERROR(INDEX(rangeOfDesiredValues,MATCH(1,INDEX((rangeOfSerials=$D20)*(rangeOfApps=cfgAppID)*(rangeOfAccessIDs=cfgAccessID),0),0)),"")
A third alternative is to use LOOKUP which doesn't need "array entry"
=IFERROR(LOOKUP(2,1/(rangeOfSerials=$D20)/(rangeOfApps=cfgAppID)/(rangeOfAccessIDs=cfgAccessID),rangeOfDesiredValues),"")
That differs slightly from the previous versions in the case of multiple matches - it will give you the last match rather than the first in that scenario (but I assume you have only one match at most, as stated above).
Finally, if you don't mind using helper columns you could simplify the formulas considerably. Just use a "helper" column to concatenate the three criteria columns separated by dashes and then you can use a simple VLOOKUP or INDEX/MATCH, e.g.
=IFERROR(INDEX(rangeOfDesiredValues,MATCH($D20&"-"&cfgAppID&"-"&cfgAccessID,Helper_Column,0)),"")

SUMIFS source code for excel 2003 AND the ability to calculate on columns

we are looking to find the original? source code for SUMIFS to use in out excel sheet (for both 2003 and 2007. Here is why:
2003 doest support the SUMIFS method
When we do have SUMIFS we cannot utilize formulas "around" the columns (like YEAR())
For example, we want to calculate the ANSWERS that match the YEAR value of the date in cell A1 with the date values in range L:L. Now this doesnt work because we cant use YEAR(L:L) and hence we need to make another column M:M with the YEAR values from L:L
Thus we need the source to be able to upgrade the code further
=SUMIFS(ANSWERS;L:L;"="&YEAR(A1)) <= This works
=SUMIFS(ANSWERS;YEAR(L:L);"="&YEAR(A1)) <= This doesnt
Many thanks
With referenc to these questions:
Replacing SUMIFS in Excel 2003
VBA code for SUMIFS?
I doubt that you'll find the original code, which is, I imagine, C++ and part of the Excel internals. If that's correct, then it wouldn't help much, even if Microsoft gave it to you!
In general, I recommend against using =SUMIF(), =SUMIFS() and other functions that take a string to define the condition for testing: apart from anything else, I'm concerned that they're going to be slow, since my best guess is that internally they construct a string for evaluation for each value. In XL2007 (all I have available right now) at least, this turns out not to be necessarily true (see comments below).
I'm generally much happier with array functions. This, for example, should work in both Excel 2003 & 2007:
=SUMPRODUCT(--(YEAR(L:L)=YEAR(A1)),ANSWERS)
This gets the same answer:
{=SUM(IF(YEAR(L:L)=YEAR(A1),ANSWERS,0))}
In the latter case, you'd enter the formula without the curly braces ({ & }) and confirm it using Control+Shift+Enter to tell Excel it's an array formula.
In the first example, we build a list of boolean results with YEAR(L:L)=YEAR(A1) and convert it into an array of 1s and 0s using the double-negative. Then SUMPRODUCT takes care of the rest. This version requires that ANSWERS has the same dimension as L:L, i.e. it should be the entire column (or the range in L should be constrained in size).
In the second, Excel will run through each entry in L:L. If its year matches that in A1 then the corresponding ANSWERS value will be used, otherwise zero. This formula seems to be more tolerant of dimension differences but I'd still be careful.

Resources