count the number of rows if conditions are true in 3 columns - excel-formula

I need to count the number of rows if conditions are true in 3 columns.
Currently I am using the below formula, but I need to sum for almost 500 odd rows...
How can this be done using array formulas...
Any Ideas?
=IF(AND(B4="B1",F4="D",AM4="X"),1,0)

=COUNTIFS($B$1:$B$500,"B1",$F$1:$F$500,"D",$AM$1:$AM$500,"X") will give you the result you want. It uses the structure of Criteria Range 1, Criterion 1, Criteria range 2...etc. You can include up to (IIRC) 127 conditions. Put that formula into a single cell and you'll see the result in that cell. You can also use cell references for the conditions and put your criterion into cells for easier modification or updating.

Related

SUMIF (or something) over a wide range of columns

I've got a massive parts spreadsheet that I'm trying to simplify. Various parts could be included in number of locations, which I would like to add up to a single list. The attached file is just an example using reindeer.
This is doable with using a bunch of SUMIF statements added together, but not practical due to the range of columns I need to include. There's gotta be a better way!?
=SUMPRODUCT(--($D$4:$J$11=$A4),$E$4:$K$11))
SUMPRODUCT can do that. Make sure the second range shifts one column, but has equal count of columns (and rows).
($D$4:$J$11=$A4) results in an array of TRUE's or FALSE's for the value in range $D$4:$J$11 being equal to the value in $A4 (no $ prior to it's row number will increase the row # referenced when dragged down).
Adding -- in front of the array converts the TRUE's and FALSE's to 1's and 0's respectively.
Multiplying that with the range to the right of it will result in 1* the value in $E$4:$K$11 for all TRUE's, which results in it's value, or 0* the value in $E$4:$K$11 for all FALSE's, which results in 0.
Summing the array of values results in the sum of all values where the condition is met in the column left from it.
SUMPRODUCT combines the multiplication of the array and summing the array results to 1 total sum.
You can use simply the SUM:
=SUM((D$4:$D$11=A4)*$E$4:$E$11,($F$4:$F$11=A4)*$G$4:$G$11, etc.)
where in etc you can put any range you want. If you don't use 2021/365 version, you must confirm the formula with CTRL+SHIFT+ENTER.

Counting cells with specific value through non contiguous columns

I need a formula to count the number of cells that contain "0" on non contiguous ranges.
I need to count the number of cells on "Q" columns (A,C,E,G,I,K,M) that contains "0" on row 2 (putting the result on "O2"). As you can see, there may be some "0" on "Comments" columns, so creating one big range is not an option (look at "H2"). The output should be "2", since "C2" and "K2" are the only ones with "0"
The real sheet is actually much longer, so i would appreciate a short approaching, maybe an array formula.
Thank you so much!
You can use SUMPRODUCT to count with multiple criteria and also check the columns of each of the values before summing the count:
=SUMPRODUCT((NOT(ISBLANK(A2:N2)))*(A2:N2=0)*(MOD(COLUMN(A2:N2),2)))
By using the MOD function you can check whether the column number is divisible by 2, if not it will return the remainder of 1. Columns that are divisible by 2 (your comments) will return 0 and therefore will not be counted. To reverse this (if the values you wanted to count ARE on even columns) simply add one to the columns before using the MOD function.
Like this:
=SUMPRODUCT((NOT(ISBLANK(A2:N2)))*(A2:N2=0)*(MOD(COLUMN(A2:N2)+1,2)))
I've also added a check to ensure that blank cells are not counted however you can remove (NOT(ISBLANK(A2:N2)))* if you did want to count blanks.
If the gap between the questions is more than 1 column this formula will not work

Excel: How to Count TRUE in Filtered "Named Ranges"

After doing a long search, I have yet to find an answer. I feel like I am asking something so out of the ordinary, like "How do you count unicorns?"
So here it goes:
How do you in Excel... count the number of TRUE cells in a FILTERED "named range"?
I found one that will work with specifying the Column - Row Index like A2:A100.
=SUMPRODUCT(SUBTOTAL(3,OFFSET(A2:A100,ROW(A2:A100)-MIN(ROW(A2:A100)),,1)), --(A2:A100=TRUE))
That doesn't work for me. Got to be Named Ranges. I can't use a helper column either. I'd rather not use VBA. And... there is one more request for some additional magical powers: My named range is the whole column, headers and all. works with SUBTOTAL anyway.
What version of Excel are you using? In 2013, I am able to make this work properly.
I populated A2:A100 with 50 TRUE and 49 FALSE values and B2:B100 with 99 RAND() values. I then filtered on various number filters in column B and all of the below formulas worked properly, counting only the visible TRUE values in column A.
Example 1: Original Formula
=SUMPRODUCT(SUBTOTAL(3,OFFSET(A2:A100,ROW(A2:A100)-MIN(ROW(A2:A100)),,1)), --(A2:A100=TRUE))
Example 2: Non-Dynamic Exact Named Range
Range named "NonDynamic" and set to:
=Sheet1!$A$2:$A$100
Formula:
=SUMPRODUCT(SUBTOTAL(3,OFFSET(NonDynamic,ROW(NonDynamic)-MIN(ROW(NonDynamic)),,1)), --(NonDynamic=TRUE))
Example 3: Dynamic Named Range
Range named "Dynamic" and set to:
=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!$A:$A)-1)
Formula:
=SUMPRODUCT(SUBTOTAL(3,OFFSET(Dynamic,ROW(Dynamic)-MIN(ROW(Dynamic)),,1)), --(Dynamic=TRUE))
Note: is a dynamic named range an option for you? They are often significantly more efficient than referencing an entire column, especially if there are no blank values in the column between the first and last row of data.
Example 4: Non-Dynamic Named Range of Entire Column
Range named "WholeColumn" and set to:
=Sheet1!$A:$A
Formula:
=SUMPRODUCT(SUBTOTAL(3,OFFSET(WholeColumn,ROW(WholeColumn)-MIN(ROW(WholeColumn)),,1)), --(WholeColumn=TRUE))
All four examples produce the exact same results in Excel 2013. For example, I filtered on column B > 0.5; this left 26 TRUE values visible of 50 total. All four formulas changed from 50 to 26.

Tolerant average (ignore #NA, etc.)

I want to calculate the average over a range (B1:B12 or C1:C12 in the figure), excluding:
Cells not being numeric, including Empty strings, Blank cells with no contents, #NA, text, etc. (B1+B8:B12 or C1+C8:C12 here).
Cells for which corresponding cells in a range (A1:A12 here) have values outside an interval ([7,35] here). This would further exclude B2:B3 or C2:C3.
At this point, cells in column A may contain numbers or have no contents.
I think it is not possible to use any built-in AVERAGE-like function. Then, I tried calculating the sum, the count, and divide. I can calculate the count (F2 and F7), but not the sum (F3), when I have #N/A in the range, e.g.
How can I do this?
Notes:
Column G shows the formulas in column F.
I cannot filter and use SUBTOTAL.
B8:C8 contain Blank cells with no contents, B9:C9 contain Empty strings.
I am looking for (non-user defined) formulas, i.e., non-VBA.
From
https://stackoverflow.com/a/30242599/2103990:
Providing you are using Excel 2010 and above the AGGREGATE
function
can be optioned to ignore all errors.
=AGGREGATE(1, 6, A1:A5)
        
You can accomplish this by using array formulas based upon nested IFs to provide at least part of the criteria. When an IF resolves to FALSE it no longer process the TRUE portion of the statement.
   
The array formulas in F2:F3 are,
=SUM(IF(NOT(ISNA(B2:B13)), (A2:A13>=7)*(A2:A13<=35)*(B2:B13<>"")))
=SUM(IF(NOT(ISNA(B2:B13)), IF(B2:B13<>"", (A2:A13>=7)*(A2:A13<=35)*B2:B13)))
The array formulas in F7:F8 are,
=SUM(IF(NOT(ISNA(C2:C13)), (A2:A13>=7)*(A2:A13<=35)*(C2:C13<>"")))
=SUM(IF(NOT(ISNA(C2:C13)), IF(C2:C13<>"", (A2:A13>=7)*(A2:A13<=35)*C2:C13)))
Array formulas need to be finalized with Ctrl+Shift+Enter↵. Once entered correctly, they can be filled down like any other formula if necessary.
Array formulas increase calculation load logarithmically as the range(s) they refer to expand. Try to keep excess blank rows to a minimum and avoid full column references.
You can get the average of your "NA" column values in one fairly simple formula like this:
=AVERAGE(IF(
(
($A$2:$A$13>=$F$2)*
($A$2:$A$13<=$F$3)*
ISNUMBER(B2:B13)
)>0,
B2:B13))
entered as an array formula using CtrlShiftEnter↵.
I find this to be a very clear way of writing it, because all your conditions are lined up next to each other. They're "and'ed" using the mathematical operator *; this of course converts TRUE and FALSE values to 1's and 0's, respectively, so when the and'ing is done, I convert them back to TRUE/FALSE using >0. Note that instead of hard-coding your thresholds 7 and 35 (hard-coding literals is usually considered bad practice), I put them in cells.
Same logic for your sum and your count; just replace AVERAGE with SUM and COUNT, respectively:
=SUM(IF((($A$2:$A$13>=$F$2)*($A$2:$A$13<=$F$3)*ISNUMBER(B2:B13))>0,B2:B13))
=COUNT(IF((($A$2:$A$13>=$F$2)*($A$2:$A$13<=$F$3)*ISNUMBER(B2:B13))>0,B2:B13))
though a more succinct formula can also be used for the count:
=SUM(($A$2:$A$13>=$F$2)*($A$2:$A$13<=$F$3)*ISNUMBER(B2:B13))
The same formulas can be used to average/sum/count your "blank" column. Here I just drag-copied them one column to the right (column G), which means that all instances of B2:B13 became C2:C13.

Excel: Find all values in a list that match another list as part of a COUNTIFS Statement

I'm struggling with integrating a condition into my COUNTIFS statement. I have about 5 conditions which I've been able to easily work in, but I can't figure out the last one. The criteria range would be A1:A40000, and the criteria would count the number that match any value in a list of 30 text strings on Sheet 2, Cells A1:A40. Is this possible? I can get the result without the other conditions. Unfortunately, I do not have the flexibility to add a column next to A1:A40000 that checks to see if it is in the list.
Edit: Clarification per request.
Simplified version of what I'm doing. I need to count the number items (column A) that meet several conditions depending on the column in the entire data set. So, I need to find the number of items that have a value of "1" in column B - AND - a value of "YES" in column "C" - AND - a value of "OLD" in column "D" - AND - (the part I'm struggling with) column "E" must contain any one of the values that's in a completely separate range (call it Z1:Z40). The formula for the first 3 conditions would be:
=COUNTIFS(B:B,1, C:C,"YES", D:D,"OLD")
The final criteria in bold would be something like:
=COUNTIFS(B:B,1, C:C,"YES", D:D,"OLD", **E:E,isnumber(match(E:E,Z1:Z40,0))**)
But that does not work...
You can simply use the range as a criteria. If you do that then your COUNTIFS function will return an array (one value each for each value in Z1:Z40) so you need a function to sum that array - I use SUMPRODUCT because it doesn't require array entry
=SUMPRODUCT(COUNTIFS(B:B,1,C:C,"yes",D:D,"old",E:E,Z1:Z40))
That approach has some limitations - you can only use two "multi-item" criteria in one COUNTIFS function (and if you do one must be a column, the other a row, or you need to use TRANSPOSE to make it that way), and items in Z1:Z40 should not be repeated (or you may get double counting).
To overcome either of those limitations you can use SUMPRODUCT in place of COUNTIFS - with ISNUMBER(MATCH for the multi-item criterion. If you use SUMPRODUCT like that then it's better to restrict the ranges for efficiency reasons, e.g.
=SUMPRODUCT((B2:B100=1)*(C2:C100="yes")*(D2:D100="old")*ISNUMBER(MATCH(E2:E100,Z1:Z40,0)))
You can add as many ISNUMBER(MATCH criteria as you want and Z1:Z40 can be any single row/column range
Let's say all your headers are in row 1 and the real data starts in row 2.
I would add a column on the end and put in the formula
=IF(AND(B2=1, C2="YES", D2="OLD", COUNTIF($Z$1:$Z$40,E2)),"YES","NO")
Then copy that down and any row where Column F was "YES" is a row that met all the criteria.
There is also a way to use wildcards
=countifs(A1:D1;"*yes")
which counts all cell which contain 'yes'

Resources