# Counting with Two Criteria

by Allen Wyatt
(last updated December 22, 2017)

John has a worksheet that contains records used in a cost-tracking system. Record numbers are entered in column A, locations in column B, and costs in column C. Not all records have a cost value entered in column C. John wants to determine a count of records "with location X and cost <> 0".

Your first impulse may be to use one of the worksheet functions designed for counting, such as CountIf. The only problem is that CountIf doesn't permit two conditions to be checked in calculating a solution. There are, however, a couple of solutions you can use, without the need of adding additional columns or intermediate calculations.

The first (and perhaps simplest) solution is to use the SUMPRODUCT worksheet function. This function allows you to count or sum data from a column, row, or array with as many criteria as you want. The basic syntax is as follows:

```=SUMPRODUCT( (CONDITION1) * (CONDITION2) * (CONDITION3) * (DATACELLS) )
```

In this particular instance, you could put the formula together like this:

```=SUMPRODUCT((B2:B101="X")*(C2:C101>0))
```

What this does is provide two different conditions that are checked. First, the cells in column B are checked to see if they equal "X", then the corresponding cells in column C are checked to see if they are equal to 0. Both conditions return either True (1) or False (0). These results are then multiplied by each other, resulting in either 1 or 0. The SUMPRODUCT function then adds them together, resulting in a cumulative count.

Another solution is to create an array formula that will do the calculation for you. Array formulas are different than regular formulas, in that they work on a number of cells, iterating through them to produce a result. Consider the following formula:

```=(B2="X")*(C2>0)
```

This returns a single value, either 1 or 0. The formula uses the same basic logic described in the earlier explanation of the SUMPRODUCT solution. The two logical comparisons return 1 or 0, which are multiplied by each other, resulting in 1 or 0 as an answer. Now, consider the following formula:

```=SUM((B2:B101="X")*(C2:C101>0))
```

This now looks very much like the earlier SUMPRODUCT formula, but it will not work properly as a straight formula. This is because SUM is not designed to work in an iterative fashion on an range of cells. If you enter this formula as an array formula (press Shift+Ctrl+Enter to enter it), then Excel understands you want to work through each of the ranges, in turn, to figure the final sum, which is a count of records that meet the stated criteria.

The different ways you can use array formulas is quite a broad topic. For more information on how array formulas work, see other issues of WordTips, or refer to the following Web site:

```http://www.cpearson.com/excel/ArrayFormulas.aspx
```

A third option is to use the database worksheet functions to return a count. Using these, you set up a "criteria table" in your worksheet, and then the function uses the criteria to analyze the records. The following steps assume that the column labels for the three columns are RecNum, Location, and Cost:

1. Find a few empty cells, either on the same worksheet as your records or on a different worksheet. (For the sake of this example, I assume you are using columns J and K.)
2. In cell J1, enter the word Location.
3. In cell K1, enter the word Cost.
4. In cell J2, enter X.
5. In cell K2, enter >0. You have now entered your criteria table in cells J1:K2.
6. Select cells J1:K2.
7. Choose Name from the Insert menu, then choose Define. Excel displays the Define Name dialog box. (See Figure 1.)
8. Figure 1. The Define New Name dialog box.

9. Enter the name Criteria, then click OK.
10. In the cell where you want a count of records meeting your criteria, enter the following:
```=DCOUNT(B1:C101,2,Criteria)
```

Notice that the first argument used with DCOUNT is the second and third columns of your records list. This argument also includes the column labels, which are necessary so that DCOUNT can locate the proper criteria matches from the criteria table (third argument).

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2815) applies to Microsoft Excel 97, 2000, 2002, and 2003.

##### 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

Generating a Web Page

Want your worksheets to be available to others on the Internet? Excel provides a way you can save your data in HTML format, ...

Discover More

Using an En Dash, by Default

In publishing, it is common practice to indicate a range of numbers by using an en dash between the first number and the ...

Discover More

Inserting Tomorrow's Date

You can use a couple of different worksheet functions to enter today's date in a cell. What if you want to calculate ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

Using the FORECAST Function

Excel provides a handy worksheet function that allows you to forecast values based upon a set of known values. This function, ...

Discover More

An Average that Excludes Zero Values

Excel allows you to use functions and formulas to analyze your data. One way you can analyze your data is to use the AVERAGE ...

Discover More

Median of Selected Numbers

Need to find a median value in a series of values? It's easy with the MEDIAN function. What isn't as easy is to derive the ...

Discover More
##### Subscribe

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

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 eight more than 2?

2017-07-06 22:09:31

Victor

4.00 4.00 19.52401733
4.00 8.00 19.16038513
4.00 12.00 19.59837341
4.00 16.00 18.73403931
4.00 20.00 10
4.00 24.00 10
8.00 32.00 10
4.00 36.00 18.15522766
4.00 40.00 18.45908356
4.00 44.00 17.80453491
12.00 56.00 9
4.00 60.00 18.67340851
4.00 64.00 18.34897614
4.00 68.00 10
4.00 72.00 10
4.00 76.00 10
4.00 80.00 10
4.00 84.00 10
4.00 88.00 10
4.00 92.00 10
4.00 96.00 10
4.00 100.00 18.9540329
4.00 104.00 10
4.00 108.00 18.21143341
4.00 112.00 17.97983551

I have three column, time, running total of time (secs) and pressure measurement (Pascals). I need to find out how many times and for how long did the pressure drop to 10Pa or below.
Eg: for the first drop below 10, total time it was below was 4+4+8 = 16 secs, second time was 12 seconds, third time was 4+4+4+4+4+4+4+4=32Pa.

Just wondering if there is a function that can give me an output in column d of the time in each instance that value in column c dropped below 10 Pa.

Many thanks

2017-06-28 06:09:48

ChrisC

Question's a bit old but below picture doesn't answer the EITHER question accurately. This gives the total greater than 0 in EACH column which summed would give you 15 rows which is not correct as there are only 12 rows to start with. In the picture supplied, to get the total rows where either column A or column B value is greater than 0 you would need to use:

=Count(A1:A12)-Countifs(A1:A12,"<=0",B1:B12,"<=0")

In the case of the image provided below that would give you 11 rows where either the entry in column A or B is greater than 0.

2014-09-22 01:37:29

Sree Kumar

Very useful tip. Thanks

2013-08-29 08:52:08

Michael (Micky) Avidan

@bex75,
Try the suggested formulas in the attached picture:

http://tinypic.com/r/2lx8cuq/5

Michael (Micky) Avidan
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL

2013-08-28 09:48:35

bex75

What about if I have 2 columns of calculations and I want a count of how many rows contain a value in EITHER column (or both) that is greater than 0?
Many thanks.

##### This Site

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