Excel array countif formula - arrays

I want to use COUNTIF function to evaluate how many items out of 2,0,0,5 are greater than 2? In Countif function, first argument is range and second is criteria. I have tried the below formula. Even tried using Ctrl+Shift+Enter at the end to evaluate. But doesn't seem to work.
=COUNTIF({"2","0","0","5"},">2")

COUNTIF doesn't accept array constants (as far as I know). Try this:
=SUMPRODUCT(--({2,0,0,5}>2))
You could also create a countif-style formula like this (the combination ctrl+shift+enter):
=COUNT(IF({2,0,0,5}>2,1,""))

Recommended reading:
Array vs Range
Some functions like Offset, SumIf, CountIf, SumIfs, and CountIfs are designed to operate only on (multi-cell) range objects. Sum, SumProduct, Frequency, Linest, lookup functions, etc. take both range and array objects.
Array means: {2,0,0,5}
Range means:
To use countif, you have to use range in cells, defining the array in the formula on the go will not work.
=COUNTIF(A1:A4,">"&2)

I know this thread is a few years old, but I ended up here with a similar problem (how to use arrays, not ranges, with countif).
Although my end goal was a little different (I wanted to find items common to two arrays), I figure the workaround I came up with might be useful for others: I ended up using the "match" function coupled with "isnumber". The formula looked like this:
=isnumber(match({a},{b},0))
this will return an array of true/false which corresponds to the values in {a} that are also in {b}. In case it wasn't clear, {a} and {b} are arrays...

Related

How to "Hlookup an array" with a formula

I'm trying to perform a sumproduct function on two arrays. I'd like to find a way of asking excel to select the first array if the column match a certain value.
In my simplified case if the value is "1" pick the first array and perform sumproduct with the array below A ...something that prevent me from spending the day nesting if (obviously a formula like =SUMPRODUCT(IF(O4=1;F4:F8;IF(O4=2;G4:G8;IF(O4=3;H4:H8;IF(O4=4;I4:I8))));L4:L8) works but in my real case is no use)
=SUMPRODUCT((O4=$F$4:$I$4)*$F$4:$I$8*$L$4:$L$8)

Excel formula: counting array elements with a criteria

I have an array, which is the result of the multiplication of two COUNTIF functions.
The result is {0,2,7,4,0}. I want to count the number of non-zero elements. I tried the following:
=COUNTIF(COUNTIF*COUNTIF,">0") <- here the two inner COUNTIFs are short for the complete formula
It did not work. I then tried the following, which did not work either.
=COUNTIF({0,2,7,4,0},">0")
I resorted to using the following array formula and it worked.
{=SUM(IF(COUNTIF*COUNTIF>0,1,0))}
Two questions:
does COUNTIF only accept cell reference but not constant array?
any solution other than array formula?
Thanks.
The first argument of COUNTIF must be a range, nothing else will do.
You can just enter this as an ordinary formula:
=SUM(--({0,2,7,4,0}>0))
However when the array comes from a calculation, you need to coerce it to process all the elements either
=SUM(INDEX(--(A1:A5>0),0))
or like #Jeeped with Sumproduct
=SUMPRODUCT(--(A1:A5>0))

Excel SUM of SUMIF/SUMIFS with dynamic multiple criteria

I need to pass a multiple criteria list (a constant array) via cell reference rather than hard-typing it into my formula.
So, instead of this:
=SUM(SUMIFS(sum_range,criteria_range,{"red","blue"}))
But I would need to use this:
=SUM(SUMIFS(sum_range,criteria_range,$A1)) where $A1 is {"red","blue"}
I understand that one can use a range of cells to pass an array but I really need my condition to come from a single cell.
It seems that passing a constant array via cell reference only passes the first element to the formula (i.e. only "red" is used as a condition) and all the working examples I could find of this (here or here) are hard-typing the condition into the formula.
Any luck anybody ?
EDIT: I should add that my data set includes blank rows so it is not contiguous and in general, I'm looking for a not too convoluted solution that will work most of the time and with as little restrictions and caveats as possible.
Change the "Array" in A1 to a comma delineated list:
blue,purple
No quotes or {}
Change the SUM to SUMPRODUCT and use this as the criteria:
TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",99)),(ROW(INDEX(AAA:AAA,1):INDEX(AAA:AAA,LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1)*99+1,99))
The $20 should be placed at the max number of choices possible. I just used it here as a placeholder, it can be more without problem but not less or it will skip any more than that.
Based on the formula you provided.
=SUMPRODUCT(SUMIFS(W$12:W$448,$I$12:$I$448,$I474,$J$12:$J$448,$J474,$K$12:$K$448,TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",99)),(ROW(INDEX(AAA:AAA,1):INDEX(AAA:AAA,LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1)*99+1,99))))
With cell A1 containing {"red","blue"} I then setup a named range Condition to which I assigned =EVALUATE($A1) and now I can pass my condition like so:
=SUM(SUMIFS(W$12:W$448,$I$12:$I$448,$I474,$J$12:$J$448,$J474,$K$12:$K$448,Condition))

Excel array sum with multiple conditions based on named range

I have an array formula that looks like this:
{=SUMIF(C11:C23,budgets,F11:F23)}
What I want it to do is sum cells F11:F23 where cells C11:C23 are values within the named range budgets.
Right now budgets has two values: 10361 and 10300 (these are transaction codes). However, the formula seems to only work for the first code 10361 but not for the second one.
I don't want to use SUMIFS because I have multiple worksheets where I would like this array formula to work, and the codes may change so changing them in one place (the named range budgets) will make things easier.
Any ideas? I don't know VBA, so was hoping for a formula solution.
Use SUMPRODUCT with COUNTIF():
=SUMPRODUCT((COUNTIF(budgets,$C$11:$C$23)>0)*$F$11:$F$23)
Or you can use this array formula:
=SUM(SUMIF(C11:C23,budgets,F11:F23))
Being an array it needs to be entered with Ctrl-Shift-Enter when exiting edit mode instead of Enter. If done properly Excel will put {} around the formula.

Skip column in an array

I have a VBA function that returns an array to be displayed in Excel. The array's first two columns contain ID's that don't need to be displayed.
Is there any way to modify the Excel formula to skip the first two columns, without going back to create a VBA helper to strip off the columns?
The formula looks like this, where the brackets let the array be displayed across a span of cells:
{=GetCustomers($a$1)}
The closest thing Excel has to built-in array manipulation is the 'INDEX' function. In your case, if the array returned by your 'GetCustomers' routine is a single row, and if you know how long it is (which I guess you do since you're putting it into the sheet), you can get what you want by doing something like this:
=INDEX(GetCustomers($A$1),{3,4,5})
So say GetCustomers() returned the array {1,2,"a","b","c"}, the above would just give back {"a","b","c"}.
There are various ways to save yourself having to type out your array of indices, though. For example,
=COLUMN(C1:E1)
will return {3,4,5}, and you can use that instead:
=INDEX(GetCustomers($A$1),COLUMN(C1:E1))
This trick doesn't work with a true 2-D array, though. 'INDEX' will return a whole row or column if you pass in a zero in the right place, but I don't know how to make it return a 2-D subset. EDIT: You can do it, but it's cumbersome. Say your array is 2x5, and you want the last three columns. You could use:
=INDEX(GetCustomers($A$1), {1,1,1;2,2,2}, {3,4,5;3,4,5})
(FURTHER EDIT: chris neilsen provides a nice way to compute those arrays in his answer.)
Charles Williams has a link on his website that explains more about using arrays like this here:
http://www.decisionmodels.com/optspeedj.htm
He posted that in response to this question I asked:
Is there any documentation of the behavior of built-in Excel functions called with array arguments?
Personally, I use VBA helper functions for things like this. With the right library routines, you can do something like:
=subseq(GetCustomers($A$1),3,3)
in the 1-D case, or:
=hstack(subseq(asCols(GetCustomers($A$1)),3,3))
in the 2-D case, and it's much more clear.
simplest solution is to just hide the first two columns
another may be to use OFFSET to resize the returned array
syntax is
OFFSET(reference,rows,cols,height,width)
I suggest modifying the 'GetCustomers' function to include an optional Boolean variable to tell the function to return all the columns, or just the single column. That would be the cleanest solution, instead of trying to handle it on the formula side.
Public Function GetCustomers(rng as Range, Optional stripColumns as Boolean = False) as Variant()
If stripColumns Then 'Resize array to meet your needs
Else 'return full sized array
End If
End Function
You can use the INDEX function to extract items from the return array
- formula is in an range starting at cell B2
{=INDEX(getcustomers($A$1),ROW()-ROW($B$2)+1,COLUMN()-COLUMN($B$2)+3)}

Resources