Counting Names Based on Two Criteria

by Allen Wyatt
(last updated December 28, 2013)

9

Rod has a Christmas card list in Excel. (Actually, this question could apply to any greeting card list.) Each name has a unique numeric ID associated with it and is color coded based on whether the name is family, friend, school friend, or work friend. He also has a True/False indicator in a column which indicates whether the person is to receive a card this year. Rod would like to count how many cards fit two criteria: the "gets card" indicator is True and the ID is 50 or below. Alternatively, he'd like a way to count the number of cards where the "gets card" indicator is True and the color code is red.

If you only have to get such sublists periodically, the easiest and most flexible solution might be to use the filtering capabilities of Excel and then use the SUBTOTAL function to determine the count. You can filter to display rows based upon almost any criteria you want. When the list represents what you want, then you can use a formula such as the following:

=SUBTOTAL(3,B2:B100)

This variation on the SUBTOTAL function returns a count of all displayed rows in the specified range. The key here is that the only rows displayed are those that meet your filtering criteria, so the formula returns the count you want.

There are also formulas that will return the desired information. The COUNTIFS function will look at multiple criteria and return a count based upon the result of the comparisons. For instance, the following formula will provide the number of list members where the "gets card" indicator is true and the ID is 50 or below:

=COUNTIFS(A2:A100,"<50",B2:B100,TRUE)

The formula only looks at list members in A2:A100, and it assumes that B2:B100 contains the True/False values serving as the "gets card" indicator.

You should remember that the COUNTIFS function was introduced beginning with Excel 2007. If you are creating a workbook that will also be used by those with an older version of Excel, then you may want to use an alternative function to return the count:

=SUMPRODUCT((A2:A100<50)*B2:B100*1)

If you want to take cell color into account in your formulas, then it becomes a bit trickier. You can devise a user-defined function (a macro) that returns cell color and then use that it in formula. (Other issues of ExcelTips have discussed such functions.) An easier way which avoids the macro is to add a column to your data that contains a numeric value indicating which group (family, friend, school friend, or work friend) the indivual belongs to. Then you can include the column value in your formulas.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12867) applies to Microsoft Excel 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Making Use of Extra Labels

Got extra labels left over after printing a mail merge? Here's what you can do to put those labels to good use.

Discover More

Number Formatting Shortcuts

Keyboard shortcuts can save time and make developing a workbook much easier. Here's how to apply the most common of Excel's ...

Discover More

Jumping to a Specific Cell Using a Hyperlink

Excel allows you to define hyperlinks in your worksheets, and these can target specific cells on other worksheets. Here are ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Extracting a State and a ZIP Code

Excel is often used to process or edit data in some way. For example, you may have a bunch of addresses from which you need ...

Discover More

Filling References to Another Workbook

When you create references to cells in other workbooks, Excel, by default, makes the references absolute. This makes it ...

Discover More

Compiling a List of Students in a Course

Need to pull just a limited amount of information from a large list? Here are a few approaches you might be able to use with ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 2?

2015-01-20 03:48:51

Tom

What about a good old fashioned Pivot table?


2014-01-07 16:44:44

Rod Hutson

Thanks for the comments, everyone!! Appreciated! You've given me several ways to look at the problem.

I had considered just using the filtering method to get a sub-total, but I wanted something "static" that when I added a new name, the sub-totals automatically updated properly.

I could have sworn I looked at COUNTIFS before asking for help - but if I did, I clearly didn't understand how it could be used.

Same thing with COUNT(IF... I didn't realize I could use the product of two column tests as a logical AND of multiple row cell states. That's a big help for more than this specific problem.

Anyway, now my list of mailed Christmas cards can be easily trimmed down, and the ones that go by email can take up the slack without fear of duplication.

Thanks again!!
Rod


2013-12-31 07:12:02

Shreepad S M Gandhi

Thanks Luc Horré for the info.


2013-12-31 06:22:06

Michael (Micky) Avidan

@Luc Horré,
Similar formula - using SUMPRODUCT instead of SUM - will eliminate the need to remember the CSE combination.
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2013-12-30 10:57:50

Luc Horré

@Shreepad,
If you're familiar with array functions then I prefer to use the SUM array function.

=SUM((B2:B5="red")*(D2:D5<=50)) (Reminder : Use Ctrl+Shift+Enter)

For me this looks less complex and more readible then the "=COUNT(IF(..." construction where you also need a blank cell range.


2013-12-30 08:35:39

Jennifer Thomas

@ Shreepad S M Gandhi:

Very nice solution, and easy to understand for novice users too! Thanks for your contribution.


2013-12-30 02:11:07

Shreepad S M Gandhi

You may directly use the =count(if()) array function. Suppose you have the following:

Col. A = Names of persons (Friend A, Friend B, Friend C etc.)
Col. B = Color codes (red, green etc.)
Col. C = Type (family friend, Work friend etc.)
Col. D = ID (34, 18, 27 etc.)
Col. E = Gets card (TRUE and FALSE)

Suppose you wish to return a count based on red color and ID less than 50, key in this formula

=COUNT(IF((B2:B5="red")*(D2:D5<=50),I12:I15))

Since this is an array formula, press Ctrl+Shift+Enter so that the formula gets enclosed within a pair of braces { }.

You may add more conditions with the operator ‘*’. This means, if you wish to base your output on one more condition say Gets card (TRUE, FALSE), you may achieve so by the formula

=COUNT(IF((B2:B5="red")*(D2:D5<=50)*(E2:E5=TRUE),I2:I5))

Note : Always keep blank the cell range I12:I15. Else the formula may return incorrect values.


2013-12-29 09:55:19

Michael (Micky) Avidan

@Chuck,
Function_num: 103 and 3 will count visible cells either after Filtering or Hiding rows,
where, Function_num: 3 WILL NOT count visible cells as a result of Hiding.
From the above I would suggest to use 103 instead of 3.
As for "looking" at the status bar - this is some sort of gimmick because it does not present the result in a dedicated cell.
I wiil be happy to learn of a way to Auto-copy the status-bar's information into cell(s).
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2013-12-28 05:48:43

Chuck

Or, ... do it the easy way - just look at the status bar.
Immediately after using autofilter, Excel will automatically display the number of items selected, in the bottom left portion of the screen, in the status bar.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.