Loading

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.

Learn more about Allen...

ExcelTips FAQ

Ask an Excel Question

Make a Comment

Free Business Forms

Free Calendars

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:

- 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.)
- In cell J1, enter the word
**Location**. - In cell K1, enter the word
**Cost**. - In cell J2, enter
**X**. - In cell K2, enter
**>0**. You have now entered your criteria table in cells J1:K2. - Select cells J1:K2.
- Choose Name from the Insert menu, then choose Define. Excel displays the Define Name dialog box. (See Figure 1.)
- Enter the name Criteria, then click OK.
- In the cell where you want a count of records meeting your criteria, enter the following:

** Figure 1.** The Define New Name dialog box.

=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.

*Related Tips:*

**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!

Very useful tip. Thanks

@bex75,

Try the suggested formulas in the attached picture:

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

Michael (Micky) Avidan

“Microsoft®” MVP – Excel (2009-2014)

ISRAEL

Try the suggested formulas in the attached picture:

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

Michael (Micky) Avidan

“Microsoft®” MVP – Excel (2009-2014)

ISRAEL

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.

Many thanks.