Using sumproduct to count entries with multiple criteria.

Jaard

New Member
Joined
Apr 17, 2006
Messages
3
I want to count the number of entries which meet multiple criteria, and thought I was on the right track with SUMPRODUCT.

A B C D

5 C GPH/CCU 18 Y
6 C GPH/EC 8 Y
7 H SU/C 25 N
8 F GPH/43 0 N
9 C GPH/31 31 N

the criteria i want met are
A5:A200 = "C"
B5:B200 = anything starting with GPH
C5:C200 >15
D5:D200 ="Y"

I tried sumproduct(($A$5:$A$200="C")*($B$5:$B$200="GPH*")*($C$5:$C$200>15)*($D$5:$D$200="Y"))
without much success so far.
Thanks for any help.
 
As alluded to by Aladin SUMPRODUCT is resource hungry.

That is not exactly what I said (emphasis added)...

"There is no need to include additional function calls (like TRIM) in a formula that is already resource-demanding."

Your generic suggestions are too generic...

If you have any reasonable amount of data, consider other options. Especially a pivot table, depending on what you'e doing.

This would require expanding the source data. If that's going to be done per case, you cannot meet ad hoc needs: "Begins with", "Ends with", "Anywhere," etc.

Filtering might also be suitable.

Is it? Again, if the simple SubTotal formulas are not sufficient, you'll need formulas that are more resource-demanding. Also, It won't be that suitable if the results are going to be submitted to further processing.

There are other ways too.

I suppose this is not a catch-all phrase and you are probably thinking of Data Tables set up with D-functions.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Thanks, Aladin.

Appreciate the point about the additional functions.

Re the filtering, I was thinking of advanced filtering - hence no extension to the data source.

And, not data tables, but query tables. These are great tools and not used by many users. They can be brilliant. Really powerful.

Yes, I was too general in my earlier comments. To the extent that it wasn't helpful, I can see. I'll try to be more specific. I was offering general suggestions about other ways that might be of relevance. So that the OP would be aware there are various ways. Such as, "... a pivot table, depending on what you'e doing." The solution is not restricted to array formulas. They can have their place; sometimes there are more suitable solutions.

It is harder to know what level to pitch a comment and what solution is best for an individual case.

Sometimes too, the OP might be giving a simple example although their real task has many thousands of more requirements for solutions (as in, they want to say apply the answer in 1,000 cells. Can be not efficient for array formulae) or of input data (the example might be 3 columns and 5 rows but the real case is 200 columns and 30,000 rows).

regards,
Fazza
 
Upvote 0
Thinking further, it might be helpful to detail a query table solution.

The file must be saved and the data must be in a list - that is, header titles to each fields. Data is in A5:D200. So in A4:D4 create field headers. I used {"First", "Second", "Third", "Fourth"}

And give the whole list a name. I used "MyData" as the name, and it covers A4:D200 (Shortcut from a cell in the list, select using CTRL-SHIFT-8 then CTRL-F3 to enter the name.)

The next steps require MS Query to be installed, and this is easily done the first time it is needed. On the main menu go Data, Get External Data, New Database Query.

Choose "Excel files" at the first tab, then select the workbook, when the wizard starts, select the data table (can be just the field/s you want or all at once. If there are multiple tables, you can select them here. HINT: this can lead to joined tables a la Access, and results like VLOOKUP without formulas, for example.)

Continuing with the wizard, filter the result set with "First" field equals to "C", "Second" field begins with "GPH", "Third" is greater than 15 and "Fourth" is equal to "Y". These can be all selected at the "Filter Data" tab. Then continue to the "Finish" tab but select "View data or edit query in Microsoft Query" then "Finish".

This will lead to MS Query and a view of the results set. All matching records are showing. The applied criteria/filters are showing.

The SQL can be viewed via either View, SQL or the "SQL" toolbar button.

The SQL for me being,
SELECT MyData.First, MyData.Second, MyData.Third, MyData.Fourth
FROM `H:\Book3`.MyData MyData
WHERE (MyData.First='C') AND (MyData.Second Like 'GPH%') AND (MyData.Third>15) AND (MyData.Fourth='Y')

Because we want a count of the records rather than the result set, I edited the SQL directly in the window to,
SELECT COUNT(*)
FROM `H:\Book3`.MyData MyData
WHERE (MyData.First='C') AND (MyData.Second Like 'GPH%') AND (MyData.Third>15) AND (MyData.Fourth='Y')

And, further to have a better final name,
SELECT COUNT(*) AS [MyAnswer]
FROM `H:\Book3`.MyData MyData
WHERE (MyData.First='C') AND (MyData.Second Like 'GPH%') AND (MyData.Third>15) AND (MyData.Fourth='Y')

The result set is now the required count.

Now either hit the "Return Data" button (shows a door) which is immediately left of the SQL button or via menu File, Return Data to Excel. And put the answer in a worksheet. With header.

Like a pivot table, this doesn't refresh automatically. Refresh by selecting a cell in the table and then (like a PT) Data, Refresh.

To update via code, something like
ActiveSheet.QueryTables(1).Refres BackgroundQuery:=False

Various thoughts. Doesn't accept dynamic ranges (such as with OFFSET, COUNTA). Can be more complex - multiple table joins. Queries closed files. Can feed into a pivot table - so that massive data sources can be filtered before the PT.

This is introductory. You can do lots with Query Tables. Look at the data range property (such as right click when a cell in the query table is selected. You can set refresh times, retain formats on refresh, fill down formulas on RHS, etc)

You can cut & paste the SQL for editing outside the window.

You can manipulate the SQL via VBA. It is only simple text manipulation. Be careful with the punctuation!

You can create query tables via VBA, it is straightforward.

Suggest you fiddle around and read the built in help.

Watch that the file references as given above are hard coded to when you created the query. This can be edited, though.

Gives you the answer, and some much more complex ones, without a formula.

I know I've typed a fair bit. However, it is pretty straightforward to get started due to the Wizard.

HTH
Fazza
 
Upvote 0

Forum statistics

Threads
1,216,267
Messages
6,129,792
Members
449,535
Latest member
Piaskun

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