Countifs variable crietera, avoid hardcodes?

Will85

Board Regular
Joined
Apr 26, 2012
Messages
240
Office Version
  1. 365
Platform
  1. Windows
Sheet1 Column A has values throughout, the values are "Apples", "Oranges", "Cookies", "Apples"
Sheet 2 has a table, A1 = Apples, B1 = Healthy, A2 = Oranges, B2 = Healthy, A3 = Cookies, B3 = Unhealthy, A4 = Apples, B4 = Healthy
Sheet 3 A1 = Healthy, B1 = Unhealthy


A2 & B2 = I need a formula to count the number of times a Healthy or Unhealthy value appeared in Sheet1 Column A

I dont want hardcodes, I might change the value in Sheet2 Column B, so I need it to be dynamic. I think I want a countif array formula, but I am struggling.
 
Did you try the formula I suggested?
Countif Array Formula v2.PNG


Evaluates to zero
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Provided you are okay with the old data being rescored with every change. does this work:
mr excel questions 29.xlsm
ABCDEFG
1DataLegendHealthyUnhealthy
2CookiesApplesHealthy75
3ApplesCandyUnhealthy
4CookiesCookiesUnhealthy
5OrangesOrangesHealthy
6PearsPearsHealthy
7ApplesWatermelonHealthy
8Cookies
9Pears
10Watermelon
11Candy
12Candy
13Apples
WillB85
Cell Formulas
RangeFormula
F2:G2F2=SUM((--(TRANSPOSE($A$2:$A$13)=($C$2:$C$7)))*($D$2:$D$7=F1))
 
Upvote 0
Provided you are okay with the old data being rescored with every change. does this work:
mr excel questions 29.xlsm
ABCDEFG
1DataLegendHealthyUnhealthy
2CookiesApplesHealthy75
3ApplesCandyUnhealthy
4CookiesCookiesUnhealthy
5OrangesOrangesHealthy
6PearsPearsHealthy
7ApplesWatermelonHealthy
8Cookies
9Pears
10Watermelon
11Candy
12Candy
13Apples
WillB85
Cell Formulas
RangeFormula
F2:G2F2=SUM((--(TRANSPOSE($A$2:$A$13)=($C$2:$C$7)))*($D$2:$D$7=F1))
My data range will likely max out at A1:A300, too many calculations.
 
Upvote 0
My data range will likely max out at A1:A300, too many calculations.
im not sure what you mean by that. Arrays are powerful operations and faster than than other lookup functions
 
Upvote 0
This is 300 rows with 24 different foods:

mr excel questions 29.xlsm
ABCDEFG
1DataLegendHealthyUnhealthy
2CookiesCookiesHealthy158142
3ApplesApplesUnhealthy
4CookiesOrangesHealthy
5OrangesPearsUnhealthy
6PearsWatermelonUnhealthy
7ApplesCandyHealthy
8CookiesSRFUnhealthy
9PearsPCKHealthy
10WatermelonCQWHealthy
11CandyHDOHealthy
12CandyONYHealthy
13ApplesJXRUnhealthy
14SRFSIJUnhealthy
15PCKJGGHealthy
16CQWJMHUnhealthy
17HDORXVUnhealthy
18ONYFDPHealthy
19JXRXSQUnhealthy
20SIJOWIHealthy
21JGGTLGUnhealthy
22JMHNLEUnhealthy
23RXVPicklesHealthy
24FDPTomatoesHealthy
25XSQJelly BeansHealthy
26OWI
27TLG
28NLE
29Pickles
30Tomatoes
31Jelly Beans
32Cookies
33Apples
34Cookies
35Oranges
36Pears
37Apples
38Cookies
39Pears
40Watermelon
41Candy
42Candy
43Apples
44SRF
45PCK
46CQW
47HDO
48ONY
49JXR
50SIJ
51JGG
52JMH
53RXV
54FDP
55XSQ
56OWI
57TLG
58NLE
59Pickles
60Tomatoes
61Cookies
62Apples
63Cookies
64Oranges
65Pears
66Apples
67Cookies
68Pears
69Watermelon
70Candy
71Candy
72Apples
73SRF
74PCK
75CQW
76HDO
77ONY
78JXR
79SIJ
80JGG
81JMH
82RXV
83FDP
84XSQ
85OWI
86TLG
87NLE
88Pickles
89Tomatoes
90Cookies
91Apples
92Cookies
93Oranges
94Pears
95Apples
96Cookies
97Pears
98Watermelon
99Candy
100Candy
101Apples
102SRF
103PCK
104CQW
105HDO
106ONY
107JXR
108SIJ
109JGG
110JMH
111RXV
112FDP
113XSQ
114OWI
115TLG
116NLE
117Pickles
118Tomatoes
119Jelly Beans
120Cookies
121Apples
122Cookies
123Oranges
124Pears
125Apples
126Cookies
127Pears
128Watermelon
129Candy
130Candy
131Apples
132SRF
133PCK
134CQW
135HDO
136ONY
137JXR
138SIJ
139JGG
140JMH
141RXV
142FDP
143XSQ
144OWI
145TLG
146NLE
147Pickles
148Tomatoes
149Jelly Beans
150Cookies
151Apples
152Cookies
153Oranges
154Pears
155Apples
156Cookies
157Pears
158Watermelon
159Candy
160Candy
161Apples
162SRF
163PCK
164CQW
165HDO
166ONY
167JXR
168SIJ
169JGG
170JMH
171RXV
172FDP
173XSQ
174OWI
175TLG
176NLE
177Pickles
178Tomatoes
179Jelly Beans
180Cookies
181Apples
182Cookies
183Oranges
184Pears
185Apples
186Cookies
187Pears
188Watermelon
189Candy
190Candy
191Apples
192SRF
193PCK
194CQW
195HDO
196ONY
197JXR
198SIJ
199JGG
200JMH
201RXV
202FDP
203XSQ
204OWI
205TLG
206NLE
207Pickles
208Tomatoes
209Jelly Beans
210Cookies
211Apples
212Cookies
213Oranges
214Pears
215Apples
216Cookies
217Pears
218Watermelon
219Candy
220Candy
221Apples
222SRF
223PCK
224CQW
225HDO
226ONY
227JXR
228SIJ
229JGG
230JMH
231RXV
232FDP
233XSQ
234OWI
235TLG
236NLE
237Pickles
238Tomatoes
239Jelly Beans
240Cookies
241Apples
242Cookies
243Oranges
244Pears
245Apples
246Cookies
247Pears
248Watermelon
249Candy
250Candy
251Apples
252SRF
253PCK
254CQW
255HDO
256ONY
257JXR
258SIJ
259JGG
260JMH
261RXV
262FDP
263XSQ
264OWI
265TLG
266NLE
267Pickles
268Tomatoes
269Jelly Beans
270Cookies
271Apples
272Cookies
273Oranges
274Pears
275Apples
276Cookies
277Pears
278Watermelon
279Candy
280Candy
281Apples
282SRF
283PCK
284CQW
285HDO
286ONY
287JXR
288SIJ
289JGG
290JMH
291RXV
292FDP
293XSQ
294OWI
295TLG
296NLE
297Pickles
298Tomatoes
299Jelly Beans
300TLG
301NLE
WillB85
Cell Formulas
RangeFormula
F2:G2F2=SUM((--(TRANSPOSE($A$2:$A$301)=($C$2:$C$25)))*($D$2:$D$25=F1))
 
Upvote 0
This is 300 rows with 24 different foods:

mr excel questions 29.xlsm
ABCDEFG
1DataLegendHealthyUnhealthy
2CookiesCookiesHealthy158142
3ApplesApplesUnhealthy
4CookiesOrangesHealthy
5OrangesPearsUnhealthy
6PearsWatermelonUnhealthy
7ApplesCandyHealthy
8CookiesSRFUnhealthy
9PearsPCKHealthy
10WatermelonCQWHealthy
11CandyHDOHealthy
12CandyONYHealthy
13ApplesJXRUnhealthy
14SRFSIJUnhealthy
15PCKJGGHealthy
16CQWJMHUnhealthy
17HDORXVUnhealthy
18ONYFDPHealthy
19JXRXSQUnhealthy
20SIJOWIHealthy
21JGGTLGUnhealthy
22JMHNLEUnhealthy
23RXVPicklesHealthy
24FDPTomatoesHealthy
25XSQJelly BeansHealthy
26OWI
27TLG
28NLE
29Pickles
30Tomatoes
31Jelly Beans
32Cookies
33Apples
34Cookies
35Oranges
36Pears
37Apples
38Cookies
39Pears
40Watermelon
41Candy
42Candy
43Apples
44SRF
45PCK
46CQW
47HDO
48ONY
49JXR
50SIJ
51JGG
52JMH
53RXV
54FDP
55XSQ
56OWI
57TLG
58NLE
59Pickles
60Tomatoes
61Cookies
62Apples
63Cookies
64Oranges
65Pears
66Apples
67Cookies
68Pears
69Watermelon
70Candy
71Candy
72Apples
73SRF
74PCK
75CQW
76HDO
77ONY
78JXR
79SIJ
80JGG
81JMH
82RXV
83FDP
84XSQ
85OWI
86TLG
87NLE
88Pickles
89Tomatoes
90Cookies
91Apples
92Cookies
93Oranges
94Pears
95Apples
96Cookies
97Pears
98Watermelon
99Candy
100Candy
101Apples
102SRF
103PCK
104CQW
105HDO
106ONY
107JXR
108SIJ
109JGG
110JMH
111RXV
112FDP
113XSQ
114OWI
115TLG
116NLE
117Pickles
118Tomatoes
119Jelly Beans
120Cookies
121Apples
122Cookies
123Oranges
124Pears
125Apples
126Cookies
127Pears
128Watermelon
129Candy
130Candy
131Apples
132SRF
133PCK
134CQW
135HDO
136ONY
137JXR
138SIJ
139JGG
140JMH
141RXV
142FDP
143XSQ
144OWI
145TLG
146NLE
147Pickles
148Tomatoes
149Jelly Beans
150Cookies
151Apples
152Cookies
153Oranges
154Pears
155Apples
156Cookies
157Pears
158Watermelon
159Candy
160Candy
161Apples
162SRF
163PCK
164CQW
165HDO
166ONY
167JXR
168SIJ
169JGG
170JMH
171RXV
172FDP
173XSQ
174OWI
175TLG
176NLE
177Pickles
178Tomatoes
179Jelly Beans
180Cookies
181Apples
182Cookies
183Oranges
184Pears
185Apples
186Cookies
187Pears
188Watermelon
189Candy
190Candy
191Apples
192SRF
193PCK
194CQW
195HDO
196ONY
197JXR
198SIJ
199JGG
200JMH
201RXV
202FDP
203XSQ
204OWI
205TLG
206NLE
207Pickles
208Tomatoes
209Jelly Beans
210Cookies
211Apples
212Cookies
213Oranges
214Pears
215Apples
216Cookies
217Pears
218Watermelon
219Candy
220Candy
221Apples
222SRF
223PCK
224CQW
225HDO
226ONY
227JXR
228SIJ
229JGG
230JMH
231RXV
232FDP
233XSQ
234OWI
235TLG
236NLE
237Pickles
238Tomatoes
239Jelly Beans
240Cookies
241Apples
242Cookies
243Oranges
244Pears
245Apples
246Cookies
247Pears
248Watermelon
249Candy
250Candy
251Apples
252SRF
253PCK
254CQW
255HDO
256ONY
257JXR
258SIJ
259JGG
260JMH
261RXV
262FDP
263XSQ
264OWI
265TLG
266NLE
267Pickles
268Tomatoes
269Jelly Beans
270Cookies
271Apples
272Cookies
273Oranges
274Pears
275Apples
276Cookies
277Pears
278Watermelon
279Candy
280Candy
281Apples
282SRF
283PCK
284CQW
285HDO
286ONY
287JXR
288SIJ
289JGG
290JMH
291RXV
292FDP
293XSQ
294OWI
295TLG
296NLE
297Pickles
298Tomatoes
299Jelly Beans
300TLG
301NLE
WillB85
Cell Formulas
RangeFormula
F2:G2F2=SUM((--(TRANSPOSE($A$2:$A$301)=($C$2:$C$25)))*($D$2:$D$25=F1))
Ill recheck, I got a low resource error message. Does the Range have to be fixed? It cant be just column A:A?
 
Upvote 0
Ill recheck, I got a low resource error message. Does the Range have to be fixed? It cant be just column A:A?
Setting range to A:A may take more resources since the formula is constructing an array. My doing that you are creating a milllion x a million array. I can see why.
What you can do is make Data and Legends tables which will automatically expand formulas.
Unfortunately, xl2bb doesn't copy table definitions. but they are easy to create, you just need to remember to rename them from the default name (change Table1 to Data, and Table2 to Legend). I'll do that locally and then send the mini worksheet. hold on.
 
Upvote 0
Here it is with tables. You can test how the range expands by adding a record in the immediate cell below each table.


mr excel questions 29.xlsm
ABCDEFG
1DataFoodRatingHealthyUnhealthy
2CookiesCookiesHealthy158142
3ApplesApplesUnhealthy
4CookiesOrangesHealthy
5OrangesPearsUnhealthy
6PearsWatermelonUnhealthy
7ApplesCandyHealthy
8CookiesSRFUnhealthy
9PearsPCKHealthy
10WatermelonCQWHealthy
11CandyHDOHealthy
12CandyONYHealthy
13ApplesJXRUnhealthy
14SRFSIJUnhealthy
15PCKJGGHealthy
16CQWJMHUnhealthy
17HDORXVUnhealthy
18ONYFDPHealthy
19JXRXSQUnhealthy
20SIJOWIHealthy
21JGGTLGUnhealthy
22JMHNLEUnhealthy
23RXVPicklesHealthy
24FDPTomatoesHealthy
25XSQJelly BeansHealthy
26OWI
27TLG
28NLE
29Pickles
30Tomatoes
31Jelly Beans
32Cookies
33Apples
34Cookies
35Oranges
36Pears
37Apples
38Cookies
39Pears
40Watermelon
41Candy
42Candy
43Apples
44SRF
45PCK
46CQW
47HDO
48ONY
49JXR
50SIJ
51JGG
52JMH
53RXV
54FDP
55XSQ
56OWI
57TLG
58NLE
59Pickles
60Tomatoes
61Cookies
62Apples
63Cookies
64Oranges
65Pears
66Apples
67Cookies
68Pears
69Watermelon
70Candy
71Candy
72Apples
73SRF
74PCK
75CQW
76HDO
77ONY
78JXR
79SIJ
80JGG
81JMH
82RXV
83FDP
84XSQ
85OWI
86TLG
87NLE
88Pickles
89Tomatoes
90Cookies
91Apples
92Cookies
93Oranges
94Pears
95Apples
96Cookies
97Pears
98Watermelon
99Candy
100Candy
101Apples
102SRF
103PCK
104CQW
105HDO
106ONY
107JXR
108SIJ
109JGG
110JMH
111RXV
112FDP
113XSQ
114OWI
115TLG
116NLE
117Pickles
118Tomatoes
119Jelly Beans
120Cookies
121Apples
122Cookies
123Oranges
124Pears
125Apples
126Cookies
127Pears
128Watermelon
129Candy
130Candy
131Apples
132SRF
133PCK
134CQW
135HDO
136ONY
137JXR
138SIJ
139JGG
140JMH
141RXV
142FDP
143XSQ
144OWI
145TLG
146NLE
147Pickles
148Tomatoes
149Jelly Beans
150Cookies
151Apples
152Cookies
153Oranges
154Pears
155Apples
156Cookies
157Pears
158Watermelon
159Candy
160Candy
161Apples
162SRF
163PCK
164CQW
165HDO
166ONY
167JXR
168SIJ
169JGG
170JMH
171RXV
172FDP
173XSQ
174OWI
175TLG
176NLE
177Pickles
178Tomatoes
179Jelly Beans
180Cookies
181Apples
182Cookies
183Oranges
184Pears
185Apples
186Cookies
187Pears
188Watermelon
189Candy
190Candy
191Apples
192SRF
193PCK
194CQW
195HDO
196ONY
197JXR
198SIJ
199JGG
200JMH
201RXV
202FDP
203XSQ
204OWI
205TLG
206NLE
207Pickles
208Tomatoes
209Jelly Beans
210Cookies
211Apples
212Cookies
213Oranges
214Pears
215Apples
216Cookies
217Pears
218Watermelon
219Candy
220Candy
221Apples
222SRF
223PCK
224CQW
225HDO
226ONY
227JXR
228SIJ
229JGG
230JMH
231RXV
232FDP
233XSQ
234OWI
235TLG
236NLE
237Pickles
238Tomatoes
239Jelly Beans
240Cookies
241Apples
242Cookies
243Oranges
244Pears
245Apples
246Cookies
247Pears
248Watermelon
249Candy
250Candy
251Apples
252SRF
253PCK
254CQW
255HDO
256ONY
257JXR
258SIJ
259JGG
260JMH
261RXV
262FDP
263XSQ
264OWI
265TLG
266NLE
267Pickles
268Tomatoes
269Jelly Beans
270Cookies
271Apples
272Cookies
273Oranges
274Pears
275Apples
276Cookies
277Pears
278Watermelon
279Candy
280Candy
281Apples
282SRF
283PCK
284CQW
285HDO
286ONY
287JXR
288SIJ
289JGG
290JMH
291RXV
292FDP
293XSQ
294OWI
295TLG
296NLE
297Pickles
298Tomatoes
299Jelly Beans
300TLG
301NLE
WillB85
Cell Formulas
RangeFormula
F2:G2F2=SUM((--(TRANSPOSE(Data[Data])=(Legend[Food])))*(Legend[Rating]=F1))
 
Upvote 0
This is 300 rows with 24 different foods:

mr excel questions 29.xlsm
ABCDEFG
1DataLegendHealthyUnhealthy
2CookiesCookiesHealthy158142
3ApplesApplesUnhealthy
4CookiesOrangesHealthy
5OrangesPearsUnhealthy
6PearsWatermelonUnhealthy
7ApplesCandyHealthy
8CookiesSRFUnhealthy
9PearsPCKHealthy
10WatermelonCQWHealthy
11CandyHDOHealthy
12CandyONYHealthy
13ApplesJXRUnhealthy
14SRFSIJUnhealthy
15PCKJGGHealthy
16CQWJMHUnhealthy
17HDORXVUnhealthy
18ONYFDPHealthy
19JXRXSQUnhealthy
20SIJOWIHealthy
21JGGTLGUnhealthy
22JMHNLEUnhealthy
23RXVPicklesHealthy
24FDPTomatoesHealthy
25XSQJelly BeansHealthy
26OWI
27TLG
28NLE
29Pickles
30Tomatoes
31Jelly Beans
32Cookies
33Apples
34Cookies
35Oranges
36Pears
37Apples
38Cookies
39Pears
40Watermelon
41Candy
42Candy
43Apples
44SRF
45PCK
46CQW
47HDO
48ONY
49JXR
50SIJ
51JGG
52JMH
53RXV
54FDP
55XSQ
56OWI
57TLG
58NLE
59Pickles
60Tomatoes
61Cookies
62Apples
63Cookies
64Oranges
65Pears
66Apples
67Cookies
68Pears
69Watermelon
70Candy
71Candy
72Apples
73SRF
74PCK
75CQW
76HDO
77ONY
78JXR
79SIJ
80JGG
81JMH
82RXV
83FDP
84XSQ
85OWI
86TLG
87NLE
88Pickles
89Tomatoes
90Cookies
91Apples
92Cookies
93Oranges
94Pears
95Apples
96Cookies
97Pears
98Watermelon
99Candy
100Candy
101Apples
102SRF
103PCK
104CQW
105HDO
106ONY
107JXR
108SIJ
109JGG
110JMH
111RXV
112FDP
113XSQ
114OWI
115TLG
116NLE
117Pickles
118Tomatoes
119Jelly Beans
120Cookies
121Apples
122Cookies
123Oranges
124Pears
125Apples
126Cookies
127Pears
128Watermelon
129Candy
130Candy
131Apples
132SRF
133PCK
134CQW
135HDO
136ONY
137JXR
138SIJ
139JGG
140JMH
141RXV
142FDP
143XSQ
144OWI
145TLG
146NLE
147Pickles
148Tomatoes
149Jelly Beans
150Cookies
151Apples
152Cookies
153Oranges
154Pears
155Apples
156Cookies
157Pears
158Watermelon
159Candy
160Candy
161Apples
162SRF
163PCK
164CQW
165HDO
166ONY
167JXR
168SIJ
169JGG
170JMH
171RXV
172FDP
173XSQ
174OWI
175TLG
176NLE
177Pickles
178Tomatoes
179Jelly Beans
180Cookies
181Apples
182Cookies
183Oranges
184Pears
185Apples
186Cookies
187Pears
188Watermelon
189Candy
190Candy
191Apples
192SRF
193PCK
194CQW
195HDO
196ONY
197JXR
198SIJ
199JGG
200JMH
201RXV
202FDP
203XSQ
204OWI
205TLG
206NLE
207Pickles
208Tomatoes
209Jelly Beans
210Cookies
211Apples
212Cookies
213Oranges
214Pears
215Apples
216Cookies
217Pears
218Watermelon
219Candy
220Candy
221Apples
222SRF
223PCK
224CQW
225HDO
226ONY
227JXR
228SIJ
229JGG
230JMH
231RXV
232FDP
233XSQ
234OWI
235TLG
236NLE
237Pickles
238Tomatoes
239Jelly Beans
240Cookies
241Apples
242Cookies
243Oranges
244Pears
245Apples
246Cookies
247Pears
248Watermelon
249Candy
250Candy
251Apples
252SRF
253PCK
254CQW
255HDO
256ONY
257JXR
258SIJ
259JGG
260JMH
261RXV
262FDP
263XSQ
264OWI
265TLG
266NLE
267Pickles
268Tomatoes
269Jelly Beans
270Cookies
271Apples
272Cookies
273Oranges
274Pears
275Apples
276Cookies
277Pears
278Watermelon
279Candy
280Candy
281Apples
282SRF
283PCK
284CQW
285HDO
286ONY
287JXR
288SIJ
289JGG
290JMH
291RXV
292FDP
293XSQ
294OWI
295TLG
296NLE
297Pickles
298Tomatoes
299Jelly Beans
300TLG
301NLE
WillB85
Cell Formulas
RangeFormula
F2:G2F2=SUM((--(TRANSPOSE($A$2:$A$301)=($C$2:$C$25)))*($D$2:$D$25=F1))
I got it to work by limiting the ranges, previously I was referencing entire columns and getting resource errors, thank you. Luckily I think I know my max ranges and can keep those as hardcodes without issue.

How about this curveball then.

Countif Array Formula v3.PNG
 
Upvote 0
I got it to work by limiting the ranges, previously I was referencing entire columns and getting resource errors, thank you. Luckily I think I know my max ranges and can keep those as hardcodes without issue.

How about this curveball then.

View attachment 90516
Using the table definition will be easier to understand debug and most resource efficient. But, you have a solution that works so that is good.
As far as the month, it would just be one more parenthetical comparison statement in the formula. I'll post a formula in a moment. But again, this brings me back to my historical data integrity questions I made earlier. By changing your Legend you are messing up prior reports that use the same legend source. but if you want to see the updated health statuses looking back then this works. Hold on for an updated formula for months included.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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