COUNTIF with multilayered dropdown selections

LWLD

New Member
Joined
Apr 22, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am a complete novice when it comes to Excel, I manage to do most things via Google searches and forums like this.

I have complied a sheet which has data values from 0 - 4 and managed to get a table to count up the number of each value grade (0 - 4). However, I am now trying to get this to allow me to filter these results via dropdown selections. I have included a minisheet to better explain/demonstrate this as I fear I will complicate it too much if I try. The 'Number of Marks at Grade' table is set up to count up the total across each donor value. I'd like to use the filter at the side to narrow the results more.

If anyone can assist i would be grateful, if not, thank you to those to took the time to read this.

Book1.ods
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
665Fired9mmPinchWashed1000000000
67Unwashed0010100000
68LoadWashed0000000001
69Unwashed1101010010
70.45sPinchWashed
71Unwashed
72LoadWashed
73Unwashed
74Unfired9mmPinchWashed1121121010
75Unwashed3434333323
76LoadWashed0101000010
77Unwashed1110012101
78.45sPinchWashed0121321111
79Unwashed3343433333
80LoadWashed1110011110
81Unwashed2211211221DonorNumber of Marks at GradeFilter
826Fired9mmPinchWashed000000000001234TotalCondition
83Unwashed0000000010162431410120Caliber
84LoadWashed010001001027640400120Contact
85Unwashed0011000100350432610120Surface
86.45sPinchWashed4725441826120
87Unwashed5474211164120
88LoadWashed61118100120
89Unwashed729463960120
90Unfired9mmPinchWashed0000000000853432400120
91Unwashed0000000000934571856120
92LoadWashed0000000000102035331319120
93Unwashed0000000001Total48938221460551200
94.45sPinchWashed0000000000%age41%32%18%5%5%
95Unwashed0000000000
96LoadWashed0000000000
97Unwashed0000000200
Gradings_(2)
Cell Formulas
RangeFormula
Y83Y83=COUNTIF(F2:O17,"0")
Z83Z83=COUNTIF(F2:O17,"1")
AA83AA83=COUNTIF(F2:O17,"2")
AB83AB83=COUNTIF(F2:O17,"3")
AC83AC83=COUNTIF(F2:O17,"4")
AD83:AD92AD83=SUM(Y83:AC83)
Y84Y84=COUNTIF(F18:O33,"0")
Z84Z84=COUNTIF(F18:O33,"1")
AA84AA84=COUNTIF(F18:O33,"2")
AB84AB84=COUNTIF(F18:O33,"3")
AC84AC84=COUNTIF(F18:O33,"4")
Y85Y85=COUNTIF(F34:O49,"0")
Z85Z85=COUNTIF(F34:O49,"1")
AA85AA85=COUNTIF(F34:O49,"2")
AB85AB85=COUNTIF(F34:O49,"3")
AC85AC85=COUNTIF(F34:O49,"4")
Y86Y86=COUNTIF(F50:O65,"0")
Z86Z86=COUNTIF(F50:O65,"1")
AA86AA86=COUNTIF(F50:O65,"2")
AB86AB86=COUNTIF(F50:O65,"3")
AC86AC86=COUNTIF(F50:O65,"4")
Y87Y87=COUNTIF(F66:O81,"0")
Z87Z87=COUNTIF(F66:O81,"1")
AA87AA87=COUNTIF(F66:O81,"2")
AB87AB87=COUNTIF(F66:O81,"3")
AC87AC87=COUNTIF(F66:O81,"4")
Y88Y88=COUNTIF(F82:O97,"0")
Z88Z88=COUNTIF(F82:O97,"1")
AA88AA88=COUNTIF(F82:O97,"2")
AB88AB88=COUNTIF(F82:O97,"3")
AC88AC88=COUNTIF(F82:O97,"4")
Y89Y89=COUNTIF(F98:O113,"0")
Z89Z89=COUNTIF(F98:O113,"1")
AA89AA89=COUNTIF(F98:O113,"2")
AB89AB89=COUNTIF(F98:O113,"3")
AC89AC89=COUNTIF(F98:O113,"4")
Y90Y90=COUNTIF(F114:O129,"0")
Z90Z90=COUNTIF(F114:O129,"1")
AA90AA90=COUNTIF(F114:O129,"2")
AB90AB90=COUNTIF(F114:O129,"3")
AC90AC90=COUNTIF(F114:O129,"4")
Y91Y91=COUNTIF(F130:O145,"0")
Z91Z91=COUNTIF(F130:O145,"1")
AA91AA91=COUNTIF(F130:O145,"2")
AB91AB91=COUNTIF(F130:O145,"3")
AC91AC91=COUNTIF(F130:O145,"4")
Y92Y92=COUNTIF(F146:O161,"0")
Z92Z92=COUNTIF(F146:O161,"1")
AA92AA92=COUNTIF(F146:O161,"2")
AB92AB92=COUNTIF(F146:O161,"3")
AC92AC92=COUNTIF(F146:O161,"4")
Y93:AC93Y93=SUM(Y83:Y92)
AD93AD93=IF(SUM(AD83:AD92)=SUM(Y93:AC93),(SUM(AD83:AD92)+SUM(Y93:AC93))/2,0)
Y94Y94=ROUND(Y93/AD93*100,0)&"%"
Z94Z94=ROUND(Z93/AD93*100,0)&"%"
AA94AA94=ROUND(AA93/AD93*100,0)&"%"
AB94AB94=ROUND(AB93/AD93*100,0)&"%"
AC94AC94=ROUND(AC93/AD93*100,0)&"%"
Cells with Data Validation
CellAllowCriteria
F66:O97List0,1,2,3,4
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
@LWLD Welcome.

I can see nothing in the above data that enables me to relate to the filters.
You may need to clarify, in order to get a meaningful response.
 
Upvote 0
What does the data that feeds columns A:O look like? If that's a normalized data table, the answer to your question might be as simple as replacing what's currently in A:O with a pivot table and looking at the subtotals it would natively include.
 
Upvote 0
I think Oaktree's idea of normalizing the data and using a pivot table has a lot of merit. But if you are stuck with the layout you have, there might be options. The fact that there are 2 options for each filter makes a binary search possible. Consider:


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1DonorConditionCaliburContactSurfaceDonorNumber of Marks at GradeFilterOptionsHelper
21Fired9mmPinchWashed100000000001234TotalConditionUnfiredFired/Unfired/All1
3Unwashed001010000010558220Caliber.45s9mm/.45s/All1
4LoadWashed0000000001219010020ContactAllPinch/Load/All1
5Unwashed110101001030558220SurfaceUnwashedWashed/Unwashed/All1
6.45sPinchWashed419010020
7Unwashed50558220
8LoadWashed619010020
9Unwashed719010020
10Unfired9mmPinchWashed1121121010819010020
11Unwashed3434333323919010020
12LoadWashed01010000101019010020
13Unwashed1110012101Total1331522246200
14.45sPinchWashed0121321111%age67%8%11%12%3%
15Unwashed3343433333
16LoadWashed1110011110
17Unwashed2211211221
182Fired9mmPinchWashed0000000000
19Unwashed0000000010
20LoadWashed0100010010
21Unwashed0011000100
22.45sPinchWashed
23Unwashed
24LoadWashed
25Unwashed
26Unfired9mmPinchWashed0000000000
27Unwashed0000000000
28LoadWashed0000000000
29Unwashed0000000001
30.45sPinchWashed0000000000
31Unwashed0000000000
32LoadWashed0000000000
33Unwashed0000000200
Sheet7
Cell Formulas
RangeFormula
R3:V12R3=LET(donor,INT((ROW($F$2:$F$161)+14)/16),bin,RIGHT(DEC2BIN(ROW($F$2:$F$161)-2,10),4),cond,LEFT(bin,1),cal,MID(bin,2,1),cont,MID(bin,3,1),surf,RIGHT(bin,1),fil,FILTER($F$2:$O$161,(donor=ROWS($R$3:$R3))*((cond=$AB$2)+($Z$2="All"))*((cal=$AB$3)+($Z$3="All"))*((cont=$AB$4)+($Z$4="All"))*((surf=$AB$5)+($Z$5="All"))),SUMPRODUCT(--(fil&""=R$2&"")))
W3:W13W3=SUM(R3:V3)
R13:V13R13=SUM(R3:R12)
AB2AB2=IF(Z2="Fired","0","1")
AB3AB3=IF(Z3="9mm","0","1")
AB4AB4=IF(Z4="Pinch","0","1")
AB5AB5=IF(Z5="Washed","0","1")
R14:V14R14=ROUND(R13/$W13,2)


I assume you'd have a dropdown in cells Z2:Z5, and based on the options selected, the values in R3:V12 would adjust. The formula is pretty complicated though, and requires the helper cells in AB2:AB5.
 
Upvote 0
Thanks for the responses thus far.

@Snakehips Thank you for the warm welcome, and apologies, the data for the dropdown selections mentioned was on another sheet so didn't get listed. The filter selections are as follows:
Condition: All/Fired/Unfired
Caliber: All/9mm/.45s
Contact: All/Pinch/Load
Surface: All/Washed/Unwashed

@Oaktree Thanks for the suggestion, however, a Pivot table is not valid, perhaps because of the layout as @Eric W mentioned.

@Eric W Thanks for your efforts here. To clarify, Donor 1/Grade 0 R3 would only encompass data from F2:O17 as opposed to F2:F161 or F2:O161, sorry for the confusion. You mention two option per filter field, would that not actually be three options as there's the two options plus the sum of those two?
Your assumption about my dropdowns in cells Z2:Z5 is correct, though it is disheartening to hear that the formula to achieve this is complicated. If that is the case, I may have to explore other options. But, I would like to take the time to thank you for all the help you have provided. It warms my heard to find a community do willing to assist novices in the field, it is very much appreciated, and I apologise if anyone feels their time has been wasted.
 
Upvote 0
Consider this (I only showed 2 donors in this mini-sheet, but the other 8 are included in the formulas):

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1DonorConditionCaliburContactSurfaceDonorNumber of Marks at GradeFilterOptionsHelper
21Fired9mmPinchWashed100000000001234TotalConditionFiredFired/Unfired/All0
3Unwashed0010100000113700020Caliber9mm9mm/.45s/All0
4LoadWashed0000000001216400020ContactAllPinch/Load/All?
5Unwashed1101010010313700020SurfaceUnwashedWashed/Unwashed/All1
6.45sPinchWashed41640002000?1
7Unwashed513700020
8LoadWashed616400020
9Unwashed716400020
10Unfired9mmPinchWashed1121121010816400020
11Unwashed3434333323916400020
12LoadWashed01010000101016400020
13Unwashed1110012101Total15149000200
14.45sPinchWashed0121321111%age76%25%0%0%0%
15Unwashed3343433333
16LoadWashed1110011110
17Unwashed2211211221
182Fired9mmPinchWashed0000000000
19Unwashed0000000010
20LoadWashed0100010010
21Unwashed0011000100
22.45sPinchWashed
23Unwashed
24LoadWashed
25Unwashed
26Unfired9mmPinchWashed0000000000
27Unwashed0000000000
28LoadWashed0000000000
29Unwashed0000000001
30.45sPinchWashed0000000000
31Unwashed0000000000
32LoadWashed0000000000
33Unwashed0000000200
Sheet7
Cell Formulas
RangeFormula
R3:V12R3=SUMPRODUCT(($F$2:$O$161&""=R$2&"")*(INT((ROW($F$2:$F$161)+14)/16)=ROWS($R$3:$R3))*ISNUMBER(SEARCH($AB$6,DEC2BIN(MOD(ROW($F$2:$F$161)-2,16),4))))
W3:W13W3=SUM(R3:V3)
R13:V13R13=SUM(R3:R12)
AB2AB2=MID("01?",FIND(LEFT(Z2),"FUA"),1)
AB3AB3=MID("01?",FIND(LEFT(Z3),"9.A"),1)
AB4AB4=MID("01?",FIND(LEFT(Z4),"PLA"),1)
AB5AB5=MID("01?",FIND(LEFT(Z5),"WUA"),1)
AB6AB6=AB2&AB3&AB4&AB5
R14:V14R14=ROUND(R13/$W13,2)


I was playing around with it a bit more, and with a different choice of the helper formula, I managed to significantly shorten the formula. The helper formulas (AB2:AB5) return a 0 for the first option, a 1 for the second option, and a ? for either. Then the AB6 formula concatenates them.


=SUMPRODUCT(($F$2:$O$161&""=R$2&"")*(INT((ROW($F$2:$F$161)+14)/16)=ROWS($R$3:$R3))*ISNUMBER(SEARCH($AB$6,DEC2BIN(MOD(ROW($F$2:$F$161)-2,16),4))))

As far as the main formula, the part in red checks to see which cells in your range (F2:O161, enough for 10 donors) match the grade marks (R2:V2). The part in blue excludes everything except the donor you want (G3:G12). The part in green takes the row numbers (F2:F161), converts them to binary, takes the last 4 binary digits, and compares that with the AB6 filter, which should match the filters you select in Z2:Z5. The ? works as a wildcard, matching 0 or 1.

Still pretty complicated, but not too bad. This version also does not require any of the newer functions. Hope this works for you.
 
Upvote 0
Morning,

Thanks for getting back to me @Eric W . I've tried the formula and am getting "value is not available to the formula or function" message. I changed cell values to suit my particular sheet, but nothing. I am a bit confused by the green G2:G12 as I don't see it in the formula, but also because it is only part of Donor 1's number 2 bullet/casing allocation. Each Donor (cell A values) has 10 bullets/casings for each filter condition (rows 2:17 for donor 1). F2:F161 would just be looking at a donor's first casing, or am I grasping this wrong?

The filter part is working great, though, instead of displaying 0/1/? as it does in your cell AB6 mine displays the text equivalents.

Thanks again
 
Upvote 0
I'm going to have to explain a bit more about how the formula works. The hardest part about your layout is the use of merged cells. A2:A17, B2:B9, C6:C9, etc. Merged cells are really hard for formulas to handle. For the A2:A17 merged cell, in a formula A2 would have 1 in it, and A3:A17 would be empty. There are complicated, calculation-intensive ways around that, but the usual rule of thumb is to avoid them whenever possible. So your sheet would look something like:

Book1
ABCDEFGHIJKLMNO
1DonorConditionCaliburContactSurface
21Fired9mmPinchWashed1000000000
31Fired9mmPinchUnwashed0010100000
41Fired9mmLoadWashed0000000001
51Fired9mmLoadUnwashed1101010010
61Fired.45sPinchWashed
71Fired.45sPinchUnwashed
81Fired.45sLoadWashed
91Fired.45sLoadUnwashed
101Unfired9mmPinchWashed1121121010
111Unfired9mmPinchUnwashed3434333323
121Unfired9mmLoadWashed0101000010
131Unfired9mmLoadUnwashed1110012101
141Unfired.45sPinchWashed0121321111
151Unfired.45sPinchUnwashed3343433333
161Unfired.45sLoadWashed1110011110
171Unfired.45sLoadUnwashed2211211221
Sheet11


Not as nice to look at perhaps, but easier to write formulas for. If you format some of the cells to hide the contents, maybe this:

Book1
ABCDEF
1DonorConditionCaliburContactSurface
21Fired9mmPinchWashed1
3Unwashed0
4LoadWashed0
5Unwashed1
6.45sPinchWashed
7Unwashed
8LoadWashed
9Unwashed
10Unfired9mmPinchWashed1
11Unwashed3
12LoadWashed0
13Unwashed1
14.45sPinchWashed0
15Unwashed3
16LoadWashed1
17Unwashed2
Sheet11


The other values are still there, just hidden.

But I realized that your layout follows a particular pattern. The donor takes up 16 rows, the Condition toggles between Fired and Unfired every 8 rows, Calibur toggles between 9mm and .45s every 4 rows, Contact toggles between Pinch and Load every 2 rows, and Surface toggles between Washed and Unwashed every row. If you understand binary counting, that's exactly what's happening. If not, see here:

Book1
ABCDEP
1DonorConditionCaliburContactSurfaceFilter Match
21Fired9mmPinchWashed0000
3Unwashed0001
4LoadWashed0010
5Unwashed0011
6.45sPinchWashed0100
7Unwashed0101
8LoadWashed0110
9Unwashed0111
10Unfired9mmPinchWashed1000
11Unwashed1001
12LoadWashed1010
13Unwashed1011
14.45sPinchWashed1100
15Unwashed1101
16LoadWashed1110
17Unwashed1111
182Fired9mmPinchWashed0000
19Unwashed0001
20LoadWashed0010
21Unwashed0011
22.45sPinchWashed0100
23Unwashed0101
24LoadWashed0110
25Unwashed0111
26Unfired9mmPinchWashed1000
27Unwashed1001
28LoadWashed1010
29Unwashed1011
30.45sPinchWashed1100
31Unwashed1101
32LoadWashed1110
33Unwashed1111
Sheet7
Cell Formulas
RangeFormula
P2:P33P2=DEC2BIN(MOD(ROW()-2,16),4)


The formula in P2 takes the row number and subtracts 2 so that it starts at 0. Then we convert it to binary (the DEC2BIN function), and just take the last 4 digits. Now each of the 4 digits in the P2 formula corresponds to one of your conditions. The Condition corresponds to the first digit. If the first digit is 0, that means Fired. If the first digit is 1, that means Unfired. For the second digit, if it's a 0, that means 9mm, if it's a 1 that means .45s. The other 2 work the same way. So what I'm doing is instead of looking for Fired, or Load, or whatever in the titles, I'm looking at the position of the row on the sheet to decide which of the conditions are being used on a given line. So when I created the filter in AB6, it has to be a combination of 1s and 0s and ?s. A filter of 00?1 means Fired, 9mm, Unwashed, and Contact=Any, or rows 3 and 5, 19 and 21, 35 and 23, etc.

I mentioned that the binary counting has to start at 0. The latest formula assumed that the data started on row 2, and I hardcoded a 2 in the formula in 2 places, but if you start on a different row, it would cause problems. This version should take care of that:

Book1
QRSTUVWXYZAAAB
1DonorNumber of Marks at GradeFilterOptionsHelper
201234TotalConditionFiredFired/Unfired/All0
3113700020Caliber9mm9mm/.45s/All0
4216400020ContactAllPinch/Load/All?
5313700020SurfaceUnwashedWashed/Unwashed/All1
641640002000?1
7513700020
8616400020
9716400020
10816400020
11916400020
121016400020
13Total15149000200
14%age76%25%0%0%0%
Sheet7
Cell Formulas
RangeFormula
R3:V12R3=SUMPRODUCT(($F$2:$O$161&""=R$2&"")*(INT((ROW($F$2:$F$161)+16-ROW($F$2))/16)=ROWS($R$3:$R3))*ISNUMBER(SEARCH($AB$6,DEC2BIN(MOD(ROW($F$2:$F$161)-ROW($F$2),16),4))))
W3:W13W3=SUM(R3:V3)
R13:V13R13=SUM(R3:R12)
AB2AB2=MID("01?",FIND(LEFT(Z2),"FUA"),1)
AB3AB3=MID("01?",FIND(LEFT(Z3),"9.A"),1)
AB4AB4=MID("01?",FIND(LEFT(Z4),"PLA"),1)
AB5AB5=MID("01?",FIND(LEFT(Z5),"WUA"),1)
AB6AB6=AB2&AB3&AB4&AB5
R14:V14R14=ROUND(R13/$W13,2)


The way the donor is selected is similar, although a bit easier. We just take the starting row, adjust it to 0, then add 16. Then divide by 16 and drop any fraction. This is also just done by position, not by looking for the name. So the order of the names in column A must be the same order as the names in column Q, just spread out more.

Anyway, a bit long winded, but I hope this makes it clearer, and that you can adjust it to your sheet.
 
Upvote 0
Solution
@Eric W I cannot thank you enough for taking the time to explain it in such a though, and relatable manner.

I have the formula working now so that it doesn't throw out an error, however, the values are particularly low. Is this possibly due to the SUMPRODUCT function adding the values rather than counting them?

Thanks
 
Upvote 0
No, the SUMPRODUCT just counts the matching values. I had to use a trick (using the &"") to avoid counting empty spaces as 0s though. The best thing to do is to test it out. Pick a donor, set a few filters, and manually count the result and see if it matches. Try a few different filters. If you find one that doesn't match, use the XL2BB to show it, and explain where it goes wrong.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,910
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top