Function to count all rows with multiple criteria - excel

I'm trying to use the COUNTIFS statement to count all rows where values in 4 different columns equal "something", while excluding rows where the values in two columns are equal. This is what I have for counting the rows where the 4 columns equal "something" but I can't figure out how to add the last part:
=COUNTIFS(A2:A100,"something",B2:B100,"something",C2:C100,"something",D2:D100,"something", [...])
Now I need to add another statement within this COUNTIFS at the [...] that says something like "exclude all rows where value in J is equal to value in K", but I can't seem to figure out how to do that WITHIN the COUNTIFS statement.

You likely have to move to a SUMPRODUCT function.
=SUMPRODUCT((A2:A100="something")*(B2:B100="something")*(C2:C100="something")*(D2:D100="something")*(J2:J100<>K2:K100))
Avoid full column references in SUMPRODUCT due to the cyclic nature of the calculation.

You could use an Array function for this.
={sum((A1:A1000 = 'Something')*(BB:B1000 = 'Something')*(C1:C1000 = 'Something')*(D1:D1000 = 'Something')*(J1:J1000 = K1=K1000))}
for entering an array function, you need to use Ctrl+Shift+Enter
More info at Excel Array Functions

Related

Is there a way to scan an entire column based on one cell in another column and pull out a value of the corresponding column?

A
B
C
D
4
1
6
5649
3
8
10
9853
5
2
7
1354
I have two worksheets, for example column A in sheet 1 and columns B-D in sheet 2.
What I want to do is to take one value in Column A, and scan both columns B and C and it is between those two values, then display the corresponding value from column D in a new worksheet.
There could be multiple matches for each of the cell in column A and if there is no match, to skip it and not have anything displayed. Is there a way to code this and somehow create a loop to do all of column A? I tried using this formula, but I think it only matches for each row and not how I want it to.
=IF(AND([PQ.xlsx]Sheet1!$A2>=[PQ.xlsx]Sheet2!$B2,[PQ.xlsx]Sheet1!$A2<[PQ.xlsx]Sheet2!$C2),[PQ.xlsx]Sheet2!$D$2,"")
How do I do this?
Thank you.
I'm not positive if I understood exactly what you intended. In this sheet, I have taken each value in A:A and checked to see if it was between any pair of values in B:C, and then returned each value from D:D where that is true. I did keep this all on a single tab for ease of demonstration, but you can easily change the references to match your own layout. I tested in Excel and then transferred to this Google Sheet, but the functions should work the same.
https://docs.google.com/spreadsheets/d/1-RR1UZC8-AVnRoj1h8JLbnXewmzyDQKuKU49Ef-1F1Y/edit#gid=0
=IFERROR(TRANSPOSE(FILTER($D$2:$D$15, ($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15))), "")
So what I have done is FILTEREDed column D on the two conditions that Ax is >= B:B and <= C:C, then TRANSPOSED the result so that it lays out horizontally instead of vertically, and finally wrapped it in an error trap to avoid #CALC where there are no results returned.
I added some random data to test with. Let me know if this is what you were looking at, or if I misunderstood your intent.
SUPPORT FOR EXCEL VERSIONS WITHOUT DYNAMIC ARRAY FUNCTIONS
You can duplicate this effect with array functions in pre-dynamic array versions of Excel. This is an array function, so it has be finished with SHFT+ENTER. Put it in F2, SHFT+ENTER, and then drag it to fill F2:O15:
=IFERROR(INDEX($D$2:$D$15, SMALL(IF(($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15), ROW($A$2:$A$15)-MIN(ROW($A$2:$A$15))+1), COLUMNS($F$2:F2))),"")
reformatted for easier explanation:
=IFERROR(
INDEX(
$D$2:$D$15,
SMALL(
IF(
($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15),
ROW($A$2:$A$15) - MIN(ROW($A$2:$A$15))+1
),
COLUMNS($F$2:F2)
)
),
"")
From the inside out: ROW($A$2:$A$15) creates an array from 2 to 15, and MIN(ROW($A$2:$A$15))+1 scales it so that no matter which row the range starts in it will return the numbers starting from 1, so ROW($A$2:$A$15) - MIN(ROW($A$2:$A$15))+1 returns an array from 1 to 14.
We use this as the second argument in the IF clause, what to return if TRUE. For the first argument, the logical conditions, we take the same two conditions from the original formula: ($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15). As before, this returns an array of true/false values. So the output of the entire IF clause is an array that consists of the row numbers where the conditions are true or FALSE where the conditions aren't met.
Take that array and pass it to SMALL. SMALL takes an array and returns the kth smallest value from the array. You'll use COLUMNS($F$2:F2) to determine k. COLUMNS returns the number of columns in the range, and since the first cell in the range reference is fixed and the second cell is dynamic, the range will expand when you drag the formula. What this will do is give you the 1st, 2nd, ... kth row numbers that contain matches, since FALSE values aren't returned by SMALL (as a matter of fact they generate an error, which is why the whole formula is wrapped in IFERROR).
Finally, we pass the range with the numbers we want to return (D2:D15 in this case) to INDEX along with the row number we got from SMALL, and INDEX will return the value from that row.
So FILTER is a lot simpler to look at, but you can get it done in an older version. This will also work in Google Sheets, and I added a second tab there with this formula, but array formulas work a little different there. Instead of using SHFT+ENTER to indicate an array formula, Sheets just wraps the formula in ARRAY_FORMULA(). Other than that, the two formulas are the same.
Since FALSE values aren't considered, it will skip those.

Excel Match function as array formula serching multiple colums

I've got some problem with this function. I've Tried to find position some cell on the list consisted of two columns. My formula MATCH(A1;D1:E10;0) gives me an #N/D error, but when I change formula for each separate column like e.g. MATCH(A1;D1:D10;0) it works. Is three possibility to do this simultanously for both colums. Maybe some array formula?
Match will not work with 2 dimensional ranges.
You can use AGGREGATE to return the correct ROW:
=AGGREGATE(15,6,ROW(D1:E10)/(D1:E10 = A1),1)

Trying to specify multiple wildcards in a countifs function for Excel

I am working on a data sheet that has almost 300,000 rows by about 40 columns.
I have a countifs function to count the number of rows that have an entry ranging from "A1" through "A5" for each letter A-G in a particular column.
I have broken out analysis on separate sheets to pull data for each row for each separate letter A-G using countifs(range,"other data","F?") (I know its simplified).
I need to create a new sheet that excludes any row with an A value in it.
I tried countifs(range,"other data", range,{"B?","C?","D?","E?","F?","G?"}) and it only returns the count for the outside values (B and G), how do I get Excel to count all of those other values as well? I would like to keep this format because to create the sheets for B-G, I just used the find and replace to replace "A?" with "B?" and so on for the other sheets.
I would like to just replace "B?" with whatever works to count the number of rows that have B-G in that particular column.
You countifs formula, with an array constant for criteria, returns an array of values. But what you want is the SUM of that array. So:
sum(countifs(range,"other data", range,{"B?","C?","D?","E?","F?","G?"}))
Without the sum function, you will only see the value of the first element of that array.
I have a feeling this is the wrong answer, but I'll say it anyway. Why can't you use
=COUNTIFS(Range,"<>A?")
Or are there other possible values that you want to exclude?
In which case you should be able to use this for A
=COUNTIFS(Range,">=B1",Range,"<=G5")
and for B1-B5
=COUNTIFS(Range,">=A1",Range,"<=A5")+COUNTIFS(Range,">=C1",Range,"<=G5")
which can be modified for C, D, E and F
and this for G
=COUNTIFS(Range,">=G1",Range,"<=G5")

SUMIF with OR criteria

How to use SUMIF formula in Excel cell that must sum over a given range and instead of finding for a single value, it should find multiple values?
For finding a single value, I use:
=SUMIF(A4:A100;"1";B4:B100)
Now I need to sum over if the column A holds 1 or 2, like:
=SUMIF(A4:A100;"1" OR "2";B4:B100)
The cell A1 will hold the criteria as a text, here it would be 1;2.
It should return same as
=SUMIF(A4:A100;"1";B4:B100) + SUMIF(A4:A100;"2";B4:B100)
but I need a formula that can take any number of criteria (1,2,3,... or more).
What's the syntax? I'm not able to use VBA here.
To sum for 1 or 2 try this version
=SUM(SUMIF(A4:A100;{1;2};B4:B100))
SUMIF will return an "array" of two results so you need SUM to sum that array for the total for 1 and 2
You can add as many numbers as you like e,g,
=SUM(SUMIF(A4:A100;{1;2;3;4};B4:B100))
or with numbers listed in a range like Z1:Z10
=SUMPRODUCT(SUMIF(A4:A100;Z1:Z10;B4:B100))
I don't think there is a way to do OR within a single statement like this. You can use SUMIFS for multiple conditions where all need to be true, but in this case you would just need to add together multiple SUMIF statements:
=SUMIF(A4:A100,"1",B4:B100)+SUMIF(A4:A100,"2",B4:B100)
Since "1" and "2" are mutually exclusive:
=SUMIF(A4:A100,"1",B4:B100)+SUMIF(A4:A100,"2",B4:B100)
i think you should define a range, let's say keys where you keep all values for which you want to sum. so in this range you keep 1 and 2 and can modyfy it whenever you want. then you add a flag column with formula IFERROR(IF(MATCH(A4,keys,0)>0,1,0),0) - now you have column in which 1 is for the values you want to sum.
this works with multiple text evaluation
=sumif(M4:M206,"Sat",O4:O206)+sumif(M4:M206,"Sun",O4:O206) // add here more + + +

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