Formula to return count and average for specified column headers

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm looking for two formulas if possible.
The formulas need to look up column headers and return results for rows that meet the variable criteria specified.
Cell K16 returns the count for rows that meet all the criteria given in J8:K15.
Cell K19 returns the average for the cells in the column detailed in K18 for the rows identified in the the criteria given in J8:K15.
Please note that the data B4:H25 will have gaps (data missing) so these rows need to be ignored if they are not complete and the criteria in J8:K15 will vary in number of entries.
I'm really looking for a formula solution (not vba) in this instance if possible.
I cannot see how to attached the file so I have given the screenshot only.
Any help much appreciated.
Capture2.PNG
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I cannot see how to attached the file so I have given the screenshot only.
Not having to manually type out the sample data would be a great help. Have a look at XL2BB as linked in my signature block below.

I also suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

1594114772121.png
 
Upvote 0
Ok, have updated my account details and installed the XL2BB add-in...
Hopefully this is what is required?
Book2.xlsx
ABCDEFGHIJKLM
1
2
3Header1Header2Header3Header4Header5Header6Header7
4ApplesLondon611Male5
5ApplesBristol274
6ApplesBristol653Male7
7PearsBristol353Male7
8PearsBristol431Male5Header1Pears
9ApplesBristol253Male6Header2Bristol
10ApplesBristol647Male2Header6Male
11ApplesBristol243Male3Header4>=3
12ApplesLondon27Male3
13PearsLondon162
14PearsLondon216Female
15PearsLondon672Female6
16PearsLondon112Female7Count:3<< formula result
17ApplesLondon64Female3
18ApplesManchester53Male2Header7
19ApplesManchester4252Average:4.7<< formula result
20PearsManchester271Male6
21ApplesManchester115Female6
22PearsBristol377Male2
23ApplesLondon371Male2
24ApplesLondon75Male7
25ApplesLondon12Female3
26
Data
 
Upvote 0
Thanks for the profile update and XL2BB data. (y)

That is quite a task for a formula - I don't see an easy way! However, you could try these monsters. :eek:
Note that for my formulas to work (hopefully), you need to include an empty column at the right of the table in your range. You will see that I have used across to column I in my ranges.

cjcass 2020-07-07 1.xlsm
ABCDEFGHIJK
1
2
3Header1Header2Header3Header4Header5Header6Header7
4ApplesLondon611Male5
5ApplesBristol274
6ApplesBristol653Male7
7PearsBristol353Male7
8PearsBristol431Male5Header1Pears
9ApplesBristol253Male6Header2Bristol
10ApplesBristol647Male2Header6Male
11ApplesBristol243Male3Header4>=3
12ApplesLondon27Male3
13PearsLondon162
14PearsLondon216Female
15PearsLondon672Female6
16PearsLondon112Female7Count:3
17ApplesLondon64Female3
18ApplesManchester53Male2Header7
19ApplesManchester4252Average:4.6666667
20PearsManchester271Male6
21ApplesManchester115Female6
22PearsBristol377Male2
23ApplesLondon371Male2
24ApplesLondon75Male7
25ApplesLondon12Female3
Sheet1
Cell Formulas
RangeFormula
K16K16=COUNTIFS(INDEX(B4:I25,0,IF(J8="",COLUMNS(B3:I3),MATCH(J8,B3:I3,0))),K8&"",INDEX(B4:I25,0,IF(J9="",COLUMNS(B3:I3),MATCH(J9,B3:I3,0))),K9&"",INDEX(B4:I25,0,IF(J10="",COLUMNS(B3:I3),MATCH(J10,B3:I3,0))),K10&"",INDEX(B4:I25,0,IF(J11="",COLUMNS(B3:I3),MATCH(J11,B3:I3,0))),K11&"",INDEX(B4:I25,0,IF(J12="",COLUMNS(B3:I3),MATCH(J12,B3:I3,0))),K12&"",INDEX(B4:I25,0,IF(J13="",COLUMNS(B3:I3),MATCH(J13,B3:I3,0))),K13&"",INDEX(B4:I25,0,IF(J14="",COLUMNS(B3:I3),MATCH(J14,B3:I3,0))),K14&"",INDEX(B4:I25,0,IF(J15="",COLUMNS(B3:I3),MATCH(J15,B3:I3,0))),K15&"")
K19K19=AVERAGEIFS(INDEX(B4:I25,0,MATCH(K18,B3:I3,0)),INDEX(B4:I25,0,IF(J8="",COLUMNS(B3:I3),MATCH(J8,B3:I3,0))),K8&"",INDEX(B4:I25,0,IF(J9="",COLUMNS(B3:I3),MATCH(J9,B3:I3,0))),K9&"",INDEX(B4:I25,0,IF(J10="",COLUMNS(B3:I3),MATCH(J10,B3:I3,0))),K10&"",INDEX(B4:I25,0,IF(J11="",COLUMNS(B3:I3),MATCH(J11,B3:I3,0))),K11&"",INDEX(B4:I25,0,IF(J12="",COLUMNS(B3:I3),MATCH(J12,B3:I3,0))),K12&"",INDEX(B4:I25,0,IF(J13="",COLUMNS(B3:I3),MATCH(J13,B3:I3,0))),K13&"",INDEX(B4:I25,0,IF(J14="",COLUMNS(B3:I3),MATCH(J14,B3:I3,0))),K14&"",INDEX(B4:I25,0,IF(J15="",COLUMNS(B3:I3),MATCH(J15,B3:I3,0))),K15&"")
 
Upvote 0
Hi Peter,
Many thanks for that, they are big formulas but I can work with that.
Thanks again for your time and help with this, that's great :)
 
Upvote 0
Just for my understanding - why the extra column in the reference (Col I)..?
 
Upvote 0
Just for my understanding - why the extra column in the reference (Col I)..?
COUNTIFS compares corresponding rows in each column to see if they meet the criteria so in the Header1 column it looks for Pears and then in the Header2 column it looks for Bristol and only counts rows where both criteria are true. This continues for the other criteria.

The issue is that your criteria range J8:K15 has 8 rows so we have to allow for 8 column comparisons. What to do when we come to J12:K12 which is, at the moment, blank? We need to make sure that rows that have been counted considering the first 4 criteria are not now discounted so we nee to manufacture a column & criteria where every row meets the criteria. I have done that by using that extra (empty) column with the criteria that the row in that column is empty.

Hope that makes enough sense. :)
 
Upvote 0
A couple of other options depending on who is using the sheet and your preferences, noting though that INDIRECT is a volatile function. These shorten the formulas somewhat.

1. Turn the data into a formal table, label the blank column X and fill with zeros. You could then hide this column.

cjcass 2020-07-07 1.xlsm
ABCDEFGHIJKL
1
2
3Header1Header2Header3Header4Header5Header6Header7X
4ApplesLondon611Male50
5ApplesBristol2740
6ApplesBristol653Male70
7PearsBristol353Male70
8PearsBristol431Male50Header1Pears
9ApplesBristol253Male60Header2Bristol
10ApplesBristol647Male20Header6Male
11ApplesBristol243Male30Header4>=3
12ApplesLondon27Male30
13PearsLondon1620
14PearsLondon216Female0
15PearsLondon672Female60
16PearsLondon112Female70Count:3
17ApplesLondon64Female30
18ApplesManchester53Male20Header7
19ApplesManchester42520Average:4.6666667
20PearsManchester271Male60
21ApplesManchester115Female60
22PearsBristol377Male20
23ApplesLondon371Male20
24ApplesLondon75Male70
25ApplesLondon12Female30
26
Sheet3
Cell Formulas
RangeFormula
L16L16=COUNTIFS(INDIRECT("Table1["&K8&"]"),L8,INDIRECT("Table1["&IF(K9="","X",K9)&"]"),L9,INDIRECT("Table1["&IF(K10="","X",K10)&"]"),L10,INDIRECT("Table1["&IF(K11="","X",K11)&"]"),L11,INDIRECT("Table1["&IF(K12="","X",K12)&"]"),L12,INDIRECT("Table1["&IF(K13="","X",K13)&"]"),L13,INDIRECT("Table1["&IF(K14="","X",K14)&"]"),L14,INDIRECT("Table1["&IF(K15="","X",K15)&"]"),L15)
L19L19=AVERAGEIFS(INDIRECT("Table1["&L18&"]"),INDIRECT("Table1["&K8&"]"),L8,INDIRECT("Table1["&IF(K9="","X",K9)&"]"),L9,INDIRECT("Table1["&IF(K10="","X",K10)&"]"),L10,INDIRECT("Table1["&IF(K11="","X",K11)&"]"),L11,INDIRECT("Table1["&IF(K12="","X",K12)&"]"),L12,INDIRECT("Table1["&IF(K13="","X",K13)&"]"),L13,INDIRECT("Table1["&IF(K14="","X",K14)&"]"),L14,INDIRECT("Table1["&IF(K15="","X",K15)&"]"),L15)



2. As above but if you are happy to ensure unwanted criteria rows are marked as such then the formulas can be shortened some more. You can mark the unwanted criteria column 1 cells with whatever text you want, so long as the heading of the "spare column" is the same.

cjcass 2020-07-07 1.xlsm
ABCDEFGHIJKL
1
2
3Header1Header2Header3Header4Header5Header6Header7N/A
4ApplesLondon611Male50
5ApplesBristol2740
6ApplesBristol653Male70
7PearsBristol353Male70
8PearsBristol431Male50Header1Pears
9ApplesBristol253Male60Header2Bristol
10ApplesBristol647Male20Header6Male
11ApplesBristol243Male30Header4>=3
12ApplesLondon27Male30N/A
13PearsLondon1620N/A
14PearsLondon216Female0N/A
15PearsLondon672Female60N/A
16PearsLondon112Female70Count:3
17ApplesLondon64Female30
18ApplesManchester53Male20Header7
19ApplesManchester42520Average:4.6666667
20PearsManchester271Male60
21ApplesManchester115Female60
22PearsBristol377Male20
23ApplesLondon371Male20
24ApplesLondon75Male70
25ApplesLondon12Female30
26
Sheet4
Cell Formulas
RangeFormula
L16L16=COUNTIFS(INDIRECT("Table2["&K8&"]"),L8,INDIRECT("Table2["&K9&"]"),L9,INDIRECT("Table2["&K10&"]"),L10,INDIRECT("Table2["&K11&"]"),L11,INDIRECT("Table2["&K12&"]"),L12,INDIRECT("Table2["&K13&"]"),L13,INDIRECT("Table2["&K14&"]"),L14,INDIRECT("Table2["&K15&"]"),L15)
L19L19=AVERAGEIFS(INDIRECT("Table2["&L18&"]"),INDIRECT("Table2["&K8&"]"),L8,INDIRECT("Table2["&K9&"]"),L9,INDIRECT("Table2["&K10&"]"),L10,INDIRECT("Table2["&K11&"]"),L11,INDIRECT("Table2["&K12&"]"),L12,INDIRECT("Table2["&K13&"]"),L13,INDIRECT("Table2["&K14&"]"),L14,INDIRECT("Table2["&K15&"]"),L15)
 
Upvote 0
You can play with a Pivot Table, it can give you other advantages. For example, you could filter by several elements: Bristol and London.

In Report Filter you filter by Header1, Header2 and Header6
In Row Label you filter by Header4
In values you count Header1 and Averge of Header7

Check the following image with the filters, in the Total row you have the results.

1594167175038.png
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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