I am trying to use countifs function. I have to do this:
countifs('Sheet 2'!$E$7:$U$88, "Completed", 'Sheet 2'!$E$4:$U$4, L4, 'Sheet 2'!$V$7:$V$88, M4)
But the function returns me #Value.
Can anyone help?
It's hard to say definitively that this will eliminate the error you're seeing, but note that neither your criteria_range2 nor your criteria_range3 has the same dimension as your criteria_range1, which is necessary per the documentation:
IMPORTANT Each additional range must have the same number of rows and columns as the criteria_range1 argument. The ranges do not have to
be adjacent to each other.
Note that this might not be "the whole solution", since (as of this answer's first posting) it isn't clear that there aren't other potential problems with the data in the ranges provided or with the criteria corresponding to those ranges.
Related
Taking sum of HP. But my formula is providing me wrong amount despite of criteria not being fulfilled!
I think it's worth posting an explanation as to why your original formula does not work, as it highlights an interesting feature of the function SUMIF.
As stated in one of my commments, you are passing a range parameter to that function of CN_2021[[#All],[HP]:[Sum of ORDER_AMT]], which is a reference to the entire table (including headers). Hence, you are effectively searching for the entry from P14 in all three columns, not just the first.
However, you are also using a sum_range parameter of CN_2021[[#All],[Sum of ORDER_QTY]], which is only one column. One might think that the construction should flat out error, since your range and sum_range parameters are not of an equal dimension (the former comprises 3 columns, the latter only 1).
The reason it does not error is due to the fact that, in such cases, Excel redimensions the sum_range so as to be of an equal dimension to the range.
So, if we replace the Table references with their equivalent worksheet references (assuming that table begins in A3) for the sake of simplification, we have that
=SUMIF(CN_2021[[#All],[HP]:[Sum of ORDER_AMT]],P14,CN_2021[[#All],[Sum of ORDER_QTY]])
which is equivalent to
=SUMIF(A3:C19,P14,B3:B19)
should error, though the single-column range
B3:B19
is redimensioned to a three-column range in line with the range, i.e.
B3:D19
which means that you are effectively performing
=SUMIF(A3:C19,P14,B3:D19)
This could have potential consequences were column D also populated: for example, place a 3 in cell C14 and 1000 in cell D14. The formula will now pick up this extra 1000 in the sum.
Strangely, unlike SUMIF, SUMIFS does not appear to be so lenient with respect to this redimensioning:
=SUMIFS(B3:B19,A3:C19,P14)
errors.
I understand how to use each method: VLOOKUP (or HLOOKUP) vs. INDEX/MATCH.
I'm looking for differences between them not in terms of personal preference, but primarily in the following areas:
Is there something that one method can do that the other cannot?
Which one is more efficient in general (or does it depend on the situation)?
Any other advantages/disadvantages to using one method vs. the other
NOTE: I am answering my own question here but looking to see if anyone else has other insights I hadn't thought of.
I prefer to use INDEX/MATCH in practically every situation because it is far more flexible and has the potential to be much more efficient depending on how large the lookup table is.
The only time when I can really justify using VLOOKUP is for very straight-forward tables where the column index number is dynamic, although even in this case, INDEX/MATCH is equally viable.
I'll give a few specific examples below to demonstrate the detailed differences between the two methods.
INDEX/MATCH can lookup to the left (or anywhere else you want)
This is probably the most obvious advantages to INDEX/MATCH as well as one of the biggest downfalls of VLOOKUP. VLOOKUP can only lookup to the right, INDEX/MATCH can lookup from any range, including different sheets if necessary.
The example below cannot be accomplished with VLOOKUP.
INDEX/MATCH has the potential to use smaller cell ranges (thus increasing efficiency)
Consider the example below. It can be accomplished with either method.
Both of these formulas work fine. However, since the VLOOKUP formula contains a larger range than the INDEX/MATCH formula, it is unnecessarily volatile.
If any cell in the range B1:G4 changes, the VLOOKUP formula must recalculate (because B1:G4 is within the range A1:H4) even though changing any cell in B1:G4 will not affect the outcome of the formula. This is not an issue for INDEX/MATCH because its formula does not contain the range B1:G4.
Using VLOOKUP with fixed col_index_number is dangerous
The main issue I see with having a fixed column index number is that it will not update as it should if full columns are inserted. Consider the following example:
This formula works fine unless a column is inserted within the lookup table. In that case, the formula will lookup the value to the left of where it should. See below, result after a column has been inserted.
This can actually be alleviated by using the following VLOOKUP formula instead:
= VLOOKUP("s",A1:H4,COLUMN(H1)-COLUMN(A1)+1,FALSE)
Now H1 will automatically update to I1 if a column is inserted, thus preserving the reference to the same column. However, this is entirely unnecessary because INDEX/MATCH can accomplish this without this problem with the formula below.
= INDEX(H1:H4,MATCH("s",A1:A4,0))
I realize this is an unlikely scenario, but it always bothered me that VLOOKUP by default looks up based on a fixed column index that does not automatically update if columns are inserted. To me, it just seems to make the VLOOKUP function more fragile.
INDEX/MATCH can handle variable column indexes just as well, but longer formula
If the column index number itself is dynamic, this is really the only case when I think VLOOKUP simplifies things a bit, but again the INDEX/MATCH alternative is just as good, just slightly more confusing. See below examples.
INDEX/MATCH is more efficient for multiple lookups
(thanks to #jeffreyweir)
If multiple lookup values are needed for a single match value, it is much more efficient to have a helper cell with the match value. This way, the match only has to be computed once, instead of one for each lookup formula. See example below.
This match value can then be used to return the appropriate lookup values. See example below, (formula has been dragged to the right).
This manual "splitting" of the match value and index values is not an option with VLOOKUP since the match value is an "internal" variable in VLOOKUP and cannot be accessed.
INDEX/MATCH can look up a range, allowing another operation
Let's say for example you want to find a max value in a column based on the column name.
You can first use MATCH to find the appropriate column, then INDEX to return the range of that entire column, then use MAX to find the max of that range.
See example below, the formula in H4 looks up the max value of the column name specified in cell G4. This cannot be accomplished using VLOOKUP alone.
MATCH doesn't have to match an exact value
Usually MATCH is used with the third argument as 0, meaning "find an exact match". But depending on the situation, using -1 or 1 as the third argument of MATCH can be very useful.
For example, the following formula returns the row number of the last row in column A that contains a number:
= MATCH(-1E+300,A:A,-1)
This is because this formula starts from the bottom of the A column and works its way toward the top, and returns the first row number in the A column where the value is greater than or equal to -1E+300 (which is basically any number).
Then INDEX can be used in combination with this to return the value in that cell. See example below.
In Summary
VLOOKUP is, at best, as good as INDEX/MATCH and admittedly slightly less confusing in some situations. And at worst, VLOOKUP is much more unsafe and volatile than INDEX/MATCH.
Also worth noting that if you want to look up a range instead of a single value, INDEX/MATCH must be used. VLOOKUP cannot be used to look up a range.
For these reasons, I generally prefer INDEX/MATCH in practically all situations.
Let's say I have an excel table with 2 columns: dates in cells A1 to A10 and values in B1 to B10.
I want to sum all the values of May dates. I have 3 possibilities:
{=SUM((MONTH(A1:A10)=6)*(B1:B10))}
or
=SUMPRODUCT((MONTH(A1:A10)=6)+0;B1:B10)
or
=SUMIFS(B1:B10;A1:A10;">="&DATE(2016;6;1);A1:A10;"<="&DATE(2016;6;30))
What is the best formula to use? In which case? And why?
I have found answers regarding the last two formulas, but nothing regarding the first one.
The first formula would give you an error if B1:B10 contains any text values, the second one won't (it will just ignore text in B1:B10). You can change the first one to allow text in B1:B10 by switching to this syntax:
=SUM(IF(MONTH(A1:A10)=6;B1:B10))
Both of the first two formulas will also give you an error if A1:A10 contains text - SUMIFS won't and can also handle error values in those ranges (as long as not in the sum range on a row that satisfies the conditions)
For those reasons SUMIFS is better, and faster as Scott says.
Disadvantages of SUMIFS:
Can't work with closed workbooks - is less flexible in that it can't accept arrays, so you can't use functions on the ranges
In your specific example SUMIFS only sums amounts for June 2016. The first two formulas will sum for any June date in any year, so that flexibility may suit you better in some circumstances
The first and Second (SUM and SUMPRODUCT) are array type formulas; they will iterate through the range, this is slow and if too many will cause a slow down in the calculation speed and even crash excel.
The third is not an array type formula and has been optimized, and as such can use full column references without detriment to speed.
When ever SUMIFS can be used it is recommended to use it.
I have many columns all labeled with many many values underneath, which can be words or numbers
Here is the current equation =INDEX(AK6:AK94,MODE(MATCH(AK6:AK94,AK6:AK94,0))) I have this on the in cell 5 of each column.
The number of values in each column may increase or decrease. If i reference the entire column (until the end of the worksheet) the blank spaces interfere with an accurate output.
How do I reference cell A6 to Last Non-Blank
There are much more efficient - and non-volatile - set-ups available for determining the last non-blank cell in a range than, for example, the SUMPRODUCT/MAX one given by sancho.s, though only if the blank cells within that range are all "genuine" blanks, and not the null string "" e.g. as a result of formulas in those cells.
If this can be guaranteed, then, for a range containing mixed datatypes (some text, some numerics) you can use:
=MAX(MATCH(REPT("z",255),A:A),MATCH(9.9E+307,A:A))
which will be far more efficient than any solution (such as the SUMPRODUCT/MAX set-up) which tests each individual cell within the specified range as to whether it is blank or not.
What's more, the above construction can reference the entirety of column A with no detriment to calculation speed, thus eliminating the need to select a limited range. (Note that using the same range, i.e. A:A, within SUMPRODUCT (or any other array formula) would not at all be a good idea, since this would be forcing Excel to calculate more than a million cells individually, leading to noticeably slower workbook performance).
As for forming a dynamic range, I'm constantly surprised that so many sources around the internet continue to advocate set-ups involving volatile functions such as OFFSET and INDIRECT (I've even seen several sites using ADDRESS for this purpose), especially when there is a perfectly good non-volatile (actually, not fully non-volatile, but near enough) INDEX set-up available, viz:
AK6:INDEX(A:A,LastRow)
where LastRow is a Defined Name given the formula I posted above.
Regards
You need to determine the row of last non-blank cell in the column. The method for this would depend on whether there are blank cells in the middle, for instance.
Two alternatives are (taken from here*):
=SUMPRODUCT(MAX(($AK6:$AK94<>"")*(ROW(AK6:AK94))))
=INDEX(MAX(($AK6:$AK94<>"")*(ROW(AK6:AK94))),0)
Then you can use this value with OFFSET to get a reference to the target cell. So your range will be (using the second form)
A6:OFFSET(AK1,INDEX(MAX(($AK6:$AK94<>"")*(ROW(AK6:AK94))),0)-1,0)
This expression will be embedded in a formula.
Notes:
You may have to change absolute/relative references.
Depending on the formula you embed the expression in, I foresee you might need to enter your formula as an array formula, with Ctrl+Shift+Enter.
*This aims at getting the last non-blank value instead of a reference to the cell, but some of the results posted are useful.
Would counting the non blank cells work, then use offset to move that number of rows.
Have a look at this:
MATCH(1,A6:OFFSET(A6,COUNTIF(A6:A600,">0"),))
the offset & count resolve to complete A6:A14 on my simple test sheet.
One option is to increase the number of rows in the formula to be as high as you might need, and add an extra IF function in the formula to handle blanks, e.g. this version will allow you up to 995 rows of data
=INDEX(AK6:AK1000,MODE(IF(AK6:AK1000<>"",MATCH(AK6:AK1000,AK6:AK1000,0))))
.....but will still work if you have fewer rows and blanks in that range
confirm with CTRL+SHIFT+ENTER
I need to use the Excel Indirect() function to reference a non-contiguous range. This (How to define a non continuous range in COUNTIF) answer gives an example.
In summary, the OP has two ranges, C1:C15 and A16. Each range contains either an A or a B, and the way to count the number of B's across these two ranges is
=SUM(COUNTIF(INDIRECT({"C1:C15","A16"}),"B"))
If I change all the B's to 1's and the A's to 0's, and change this formula slightly to
=SUM(COUNTIF(INDIRECT({"C1:C15","A16"}),"1"))
Then this still works ... BUT! this doesn't:
=SUM(INDIRECT({"C1:C15","A16"}))
I can work around this, but am I missing something fundamentally magic about the COUNTIF() function, that somehow influences the INDIRECT() function to behave as expected?
It's not possible to use a non-contiguous range in COUNTIF.
What's actually happening with the first formula is that COUNTIF is being fed with an array of (two) separate ranges and therefore the result is an array of the results of two counts, then SUM is used to sum the array.
If you actually have 1s and zeroes wouldn't you just SUM them with this formula
=SUM(C1:C15,A16)
If you are just trying to find why your last formula doesn't work then, yes, I think COUNTIF does work differently - it's able to handle an array of ranges while some other functions can't. This is common to the "IFS" family of functions, so SUMIF, for example, can do the same