Excel COUNTIFS: Formula doesn't work - excel

I have a question regarding the formula "=COUNTIFS". I have a table with several columns, one is "Project name" and the others are "category 1" and "category2".
The single "=COUNTIF" works fine for me, see below (I am filtering for the word "Preferential" in two columns on the sheet "Entries":
=COUNTIF(Entries!E2:F1099, "Preferential")
The double one "=COUNTIFS" does not work though. Can you please tell me what I do wrong? This is what I am using:
=COUNTIFS(Entries!E2:F1099, "Preferential", Entries!A2:A1099, "Selling GPI")
I always just get the error "#VALUE!".
Thank you very much!

From the documentation found here:
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.
In your case, the criteria 1 range has 2 columns, while the criteria 2 range has only one.
You can change the formula by :
=COUNTIFS(Entries!E2:E1099, "Preferential", Entries!F2:F1099, "Preferential", Entries!A2:A1099, "Selling GPI")
It should solve your problem. [1]
[1] or maybe not, the second formula implies that "Preferential" should be in both columns E and F
Edit: Corrected formula: If the objective is to count the rows where either E or F are "Preferential" and A is "Selling GPI":
=COUNTIFS(Entries!E2:E1099,"Preferential",Entries!A2:A1099,"Selling GPI")+COUNTIFS(Entries!F2:F1099,"Preferential",Entries!A2:A1099,"Selling GPI")-COUNTIFS(Entries!E2:E1099,"Preferential",Entries!F2:F1099,"Preferential",Entries!A2:A1099,"Selling GPI")
The first COUNTIFS will count rows for columns E and A, the second for F and A, an the third will remove the rows counted twice.
If the objective is to count occurrences of "Preferential" in columns E or F and of "Selling GPI" in column A, you can use:
=COUNTIF(Entries!E2:F1099,"Selling GPI")+COUNTIF(Entries!A2:A1099,"Selling GPI")

Related

Sum.ifs for few single cells in a row

Im trying to make sum.ifs formula to sum values in one row based on criteria.
In Criteria for Z I want to sum ALL Zs in table and same for D criteria.
Can I use sort of sum.ifs(A1:C1:E1;B1:D1:F1;G1) function? Unfortunately A1:C1:E1 trys to take range, not single cells and I get error...
What about this: (ugly but working)
=SUM(IF(A3:E3="Z",OFFSET(A3:E3,0,1),0))
It just goes over all cells (even the ones with the values are checked if they equal "Z", but as this is not the case (as they are values), they are skipped).
Edit (better formulated than the first one):
Next proposal:
=SUM(IF($A3:$E3=F$2,OFFSET($A3:$E3,0,1),0))
The dollarsigns are meant for fixing rows and/or columns:
$A3:$E3 : we will always be working with columns A to E, but the rows may change.
F$2 : the criteria are always on row 2, but some are in column F ("Z")
and some in column G ("D").
Place this in G3
=SUMIF(A3:E3,{"=Z","=D"},B3:F3)
The range A3:E3 is checked for the criteria of "=Z" and "=D" in turn, and the range over B3:F3 is then summed.
If you have Office 365 you could use: =SUM(FILTER(B3:F3,COUNTIF(G2:H2,A3:E3)>0))
Not sure if you wanted all summed at once or per letter. To have sum per letter use:
=SUM(FILTER(B3:F3,COUNTIF(G2:H2,A3:E3)>0)) and drag to the right

Excel SUMPRODUCT and dynamic text conditions

I am trying to do a summation of rows with certain dynamic conditions. I have rows like:
A can be only one value, K can have multiple OR-values. In the end M is to be summed.
I have tried to use SUMPRODUCT() which works for column A but not for K. What I am looking for is something like:
=SUMPRODUCT(--(!$A$2:$A$20000="AA")*--(!$K$2:$K$20000="AA" OR "BB")*$M$2:$M$20000)
I know I can do ="AA" and then ="BB" but I need "AA" and "BB" to be dynamic based on other cells. And the number of arguments is different. I tried {"AA";"BB"} but I know this will not work as the match then needs to be in the same row.
Can it at all be achieved?
Thanks a lot!
=SUMPRODUCT(($A$2:$A$20000="AA")*(($K$2:$K$20000="AA")+($K$2:$K$20000="BB"))*$M$2:$M$20000)
Note that:
Since you are multiplying/adding arrays, there's no need to include the double unary's
I don't know why you have a ! in your example formula.
To return an OR array of TRUE;FALSE, we add.
Your comments still do not provide a clear explanation of what you are making dynamic.
But to create a dynamic OR for column K, including testing for column A and summing column M, you can do the following:
For column K, let us assume that your possible OR's are entered separately in the range F2:F10
=SUMPRODUCT(MMULT(--($K$2:$K$20000=TRANSPOSE($F$2:$F$10)),--(ROW($F$2:$F$10)>0))*($A$2:$A$20000="AAA")*$M$2:$M$20000)
The matrix multiplication will produce a single column of 19,999 entries which will be a 1 for matches of any of the OR's and 0 if it does not match.
See How to do a row-wise sum in an array formula in Excel?
for information about the MMULT function in this application.
In the above formula, "blanks" in the OR range (F2:F10) will also match blank entries in column K. So it is conceivable that if there is a blank in K and F and a AAA in col A and a value in column M that a wrong result might be returned.
To avoid that possibility, we can use a dynamic formula to size column F where we are entering our OR values:
=INDEX($F$2:$F$10,1):INDEX($F$2:$F$10,COUNTA($F$2:$F$10))
will return only the values in col F that are not blank (assuming no blanks within the column)
So:
=SUMPRODUCT(MMULT(--($K$2:$K$20000=TRANSPOSE(INDEX($F$2:$F$10,1):INDEX($F$2:$F$10,COUNTA($F$2:$F$10)))),--(ROW(INDEX($F$2:$F$10,1):INDEX($F$2:$F$10,COUNTA($F$2:$F$10)))>0))*($A$2:$A$20000="AAA")*$M$2:$M$20000)
Given this data:
the last formula will return a value of 5 (sum of M2,M3,M7)
Use SUMIFS with SUMPRODUCT wrapper:
=SUMPRODUCT(SUMIFS($M$2:$M$20000,$A$2:$A$20000,"AA",$K$2:$K$20000,{"AA","BB"}))

VBA : distinct values in a column and their associated distinct values

I'm trying to do something similar in vba which I have an idea of only in python for loops. Can someone teach me how to do this in vba, either in function or module macro please :
For each distinct values in column A4:A30, there should be no more than 9 distinct values in column C4:C30. If true, return 'OK' in cell A1. if false, return 'Error' in cell A1'
e.g As in the picture, Sam should not have more than 9 distinct fruits. Same goes to Mary
Update :
I have tried the filterxml method and unfortunately didn't seem work for me : [1] https://i.stack.imgur.com/cbmTs.png
Solution for excel with filter/unique formulas
Easiest way to achieve it in Excel365 is: add extra column which counts unique values (Fruits) for each Key (Names) and find maximum value in this column
Start with formula that find each non-blank which fits the key.
=FILTER($C$4:$C$30,($A$4:$A$30=A4)*($C$4:$C$30<>""))
Then delete duplicates:
=UNIQUE(FILTER($C$4:$C$30,($A$4:$A$30=A4)*($C$4:$C$30<>"")))
Then check how many cells we have in filtered data without blanks and duplicates:
=COUNTA(UNIQUE(FILTER($C$4:$C$30,($A$4:$A$30=A4)*($C$4:$C$30<>""))))
Then expand our new-column (column B in my case) formula to each row in our Keys.
And finally add formula to A1 which checks maximum counter:
=IF(MAX($B$4:$B$30)<10,"OK","Error - to many velues")
*There is a little typo, it should be "Error - to many values" =)
Below how the worksheet looks in my testfile
Solution for older versions of excel
I've checked if i am able to make it works without these formulas and it is possible:
We need to start with counting if there is for key-value above current row
=COUNTIFS($A$4:A4,A4,$C$4:C4,C4)
In case we have duplicates above, they should be already counted so we skip them:
=IF(COUNTIFS($A$4:A4,A4,$C$4:C4,C4)>1,"",1)
Now we have colum with "1" or blanks. In that case we need to count each non-empty cell above which correct key (name) and add 1 so instead "" and "1" we will have "" or 1, 2, 3, 4, ...
=IF(COUNTIFS($A$4:A4,A4,$C$4:C4,C4)>1,"",COUNTIFS($A$3:A3,A4,$F$3:F3,">0")+1)
Edit
I have added one extra IF to skip keys if value is blank:
=IF(C4="","",IF(COUNTIFS($A$4:A4,A4,$C$4:C4,C4)>1,"",COUNTIFS($A$3:A3,A4,$B$3:B3,">0")+1))
Cells Formula in A1 is the same
=IF(MAX($B$4:$B$30)<10,"OK","Error - to many values")
Quick Note:
Some formulas have range which starts on 3rd row instead of 4th; Its intended because we are counting cells above and at first row of data we need to have choose something above. This code assumes that you don't have numbers (on column B) or names (on column A) in row 3;
Below I am attaching screen with example; This screen have additional columns (D-F) which isn't required, its only do display how final formula was created.

Count of values which appear more than once in a column

In my excel column I have values as such:
ID
a
a
a
b
c
c
d
e
I would like to return the count of ids which occur twice or more. In this case answer is 2 (a,c).
Constraints:
No helper cols or one at most(There are a ton of other filters to be added to the countifs which are not relevant to the question,adding helpers would mean 12+ extra columns, one for each month)
2.No VBA ( UDF is ok)
3.Formula result in single cell.
The current formula which I have tried:
=COUNTIFS(F13:F22,COUNTIF(F13:F22,">=2"))
gives me 0.
Thanks in advance.
Hmm with no specific order of values, try:
=SUM(IF(COUNTIF(A2:A9,A2:A9)>1,1/COUNTIF(A2:A9,A2:A9),0))
Enter as array through CtrlShiftEnter
Another variant would be:
=SUMPRODUCT((COUNTIF(A2:A9,A2:A9)>1)/COUNTIF(A2:A9,A2:A9))
With the advantage you won't have to enter as array.
Would you choose to add criteria I believe that the second formula is a bit more userfriendly adding them in, like so (edited your sample data a little to show):
=SUMPRODUCT((B2:B9=1)*(C2:C9="x")*(COUNTIF(A2:A9,A2:A9)>1)/COUNTIF(A2:A9,A2:A9))

How to filter the correct line in sheet instead of IF-statement (Excel)

In my excel sheet i have the following function:
=IF(AD51="European";
IF(P51<50.000.000; 0,05%;
IF(P51<75.000.000; 0,045%;
IF(P51<100.000.000; 0,04%;
IF(P51<125.000.000; 0,037%;
IF(P51<150.000.000; 0,035%;
IF(P51<200.000.000; 0,034%;
IF(P51<250.000.000; 0,033%;
IF(P51<300.000.000; 0,032%;
IF(P51<400.000.000; 0,031%;
IF(P51<500.000.000; 0,03%;0,025)
)
)
)
)
)
)
)
)
);"American")
Well, I need to replace this in an extra sheet in order to avoid this kind of IF function in a cell.
Here the table:
My question is, how to get the result in the marked cells below:
I need to check if the ID is existing in CalcSheet (Screenshot1) and then to check in which rank the Volume value is categorized in MasterSheet (Screenshot2). Finished the table have to return the %-value of the CalcSheet.
EDIT: I tried it with VLOOKUP(A2;Ranking;3) but I get always the
same procent => 0,03. For your information, Ranking is the range of
the Screenshot1 that I named it.
Complex Lookups in Excel
Solution to your problem using SUMIFS
Updated MasterSheet
CalcSheet with Formula
Formula in CalcSheet!C2:
=SUMIFS(MasterSheet!$D$2:$D$11,MasterSheet!$A$2:$A$11,A2,MasterSheet!$E$2:$E$11,E2,MasterSheet!$C$2:$C$11,">"&B2,MasterSheet!$B$2:$B$11,"<="&B2)
Explanation
VLOOKUP is quite limited in its ability to pick up values. This neat trick with SUMIFS allows fairly complex lookup based on multiple conditions. The trick here is to first ensure that you select one and only cell using conditions and then sum over that one cell.
In this case, we sum over column D in MasterSheet. The conditions are:
MasterSheet Column A matches CalcSheet Column A
MasterSheet Column E matches CalcSheet Column E
MasterSheet Column C is greater than CalcSheet Column B
MasterSheet Column B is less or equal to CalcSheet Column B
Please mark this as answer if this solves your problem.
Update in response to OP's comment
Like the above 4 conditions. You can choose up-to 127 ranges and corresponding conditions in Sumifs in Excel 2016. Check this link in Office Documentation.

Resources