TECHNOLOGY EXCEL...While COUNTIF is great at using a sin-gle criterion, there’s no easy way to...

2
A reader sent in a question: “I need to count how many cells in a column fall between 10 and 19.999. I can use COUNTIF to find the records less than 10 or to find the records less than 20. But how can I combine two conditions in a single COUNTIF?” Since Excel 97, the COUNTIF and SUMIF commands have provided a way to count or sum records that match a single criterion. For example, in Figure 1, the formula =COUNTIF($A2:$A200,C2) is counting the records that match the <10 criterion stored in cell C2. While COUNTIF is great at using a sin- gle criterion, there’s no easy way to com- bine two criteria in a single COUNTIF function. Before Excel 2007, you might try using SUMPRODUCT or DCOUNT or even subtracting one COUNTIF from another: =COUNTIF(A2:A200,“<20”)– COUNTIF(A2:A200,“<10”). All of those options require arcane functions that few people ever encounter. With Excel 2007, Microsoft intro- duced plural versions of these functions, adding SUMIFS, COUNTIFS, and even AVERAGEIF and AVERAGEIFS. The plural COUNTIFS can count records that match from one to 255 criteria, making it easy to solve this problem. COUNTIFS requires pairs of argu- ments. Each pair consists of a criteria range and criteria. In the current exam- ple, the criteria range will be A2:A200. The first criterion will be >=10. The sec- ond criterion will be <20. The formula is =COUNTIFS(A2:A200,“>=10”,A2:A200, “<20”), as shown in Figure 2. While it wasn’t stated in the reader’s question, I suspect that he also needs to count the number of records that fall into other ranges, such as 20-30, 30-40, and so on. Figure 3 shows three different methods for creating such an analysis. One solution is to extend the COUNTIFS formula. The table in C4:D8 uses the COUNTIFS function with two criteria. The advantage is that you can decide if a range includes the upper or lower limit. The current formula of =COUNTIFS($A$2:$A$200,“>=”&C4, $A$2:$A$200,“<”&C5) includes values equal to the lower limit but excludes val- ues equal to the upper limit. If you want- ed to change the rule, you could replace the greater than or equal to sign (>=) with a greater than sign (>) and the less than sign (<) with the less than or equal to (<=) sign. The second solution is the FREQUEN- CY function. To use this function, you set up a range of values that hold the upper limit for each range. In Figure 3, the range C12:C16 holds the upper limit of each range. The 31 returned in cell 54 STRATEGIC FINANCE I February 2012 TECHNOLOGY EXCEL Counting Values within a Range By Bill Jelen Figure 1 Figure 2

Transcript of TECHNOLOGY EXCEL...While COUNTIF is great at using a sin-gle criterion, there’s no easy way to...

Page 1: TECHNOLOGY EXCEL...While COUNTIF is great at using a sin-gle criterion, there’s no easy way to com-bine two criteria in a single COUNTIF function. Before Excel 2007, you might try

A reader sent in a question: “I need to

count how many cells in a column fall

between 10 and 19.999. I can use

COUNTIF to find the records less than 10

or to find the records less than 20. But

how can I combine two conditions in a

single COUNTIF?”

Since Excel 97, the COUNTIF and

SUMIF commands have provided a way

to count or sum records that match a

single criterion. For example, in Figure 1,

the formula =COUNTIF($A2:$A200,C2)

is counting the records that match the

<10 criterion stored in cell C2.

While COUNTIF is great at using a sin-

gle criterion, there’s no easy way to com-

bine two criteria in a single COUNTIF

function. Before Excel 2007, you might

try using SUMPRODUCT or DCOUNT or

even subtracting one COUNTIF from

another: =COUNTIF(A2:A200,“<20”)–

COUNTIF(A2:A200,“<10”). All of those

options require arcane functions that

few people ever encounter.

With Excel 2007, Microsoft intro-

duced plural versions of these functions,

adding SUMIFS, COUNTIFS, and even

AVERAGEIF and AVERAGEIFS. The plural

COUNTIFS can count records that match

from one to 255 criteria, making it easy

to solve this problem.

COUNTIFS requires pairs of argu-

ments. Each pair consists of a criteria

range and criteria. In the current exam-

ple, the criteria range will be A2:A200.

The first criterion will be >=10. The sec-

ond criterion will be <20. The formula is

=COUNTIFS(A2:A200,“>=10”,A2:A200,

“<20”), as shown in Figure 2.

While it wasn’t stated in the reader’s

question, I suspect that

he also needs to count

the number of records

that fall into other

ranges, such as 20-30,

30-40, and so on. Figure

3 shows three different

methods for creating such

an analysis.

One solution is to

extend the COUNTIFS formula. The table

in C4:D8 uses the COUNTIFS function

with two criteria. The advantage is that

you can decide if a range includes the

upper or lower limit. The current formula

of =COUNTIFS($A$2:$A$200,“>=”&C4,

$A$2:$A$200,“<”&C5) includes values

equal to the lower limit but excludes val-

ues equal to the upper limit. If you want-

ed to change the rule, you could replace

the greater than or equal to sign (>=) with

a greater than sign (>) and the less than

sign (<) with the less than or equal to (<=)

sign.

The second solution is the FREQUEN-

CY function. To use this function, you

set up a range of values that hold the

upper limit for each range. In Figure 3,

the range C12:C16 holds the upper limit

of each range. The 31 returned in cell

54 S T R AT E G IC F I N A N C E I Fe b r u a r y 2 0 1 2

TECHNOLOGY

EXCELCounting Values within a Range

By Bill Jelen

Figure 1

Figure 2

Page 2: TECHNOLOGY EXCEL...While COUNTIF is great at using a sin-gle criterion, there’s no easy way to com-bine two criteria in a single COUNTIF function. Before Excel 2007, you might try

D12 means that 31 values are less than

or equal to 10. The formula in D12:D17

is a single formula that returns an

array of values. To enter this formula,

you have to select D12:D17. Type

=FREQUENCY(A2:A200,C12:C16), but

don’t press Enter. Instead, hold down

Ctrl+Shift while pressing Enter. This tells

Excel that this formula is an array formu-

la. Excel displays the braces (also known

as curly brackets) around the formula in

the formula bar to alert you that this

isn’t a regular formula. Note that the for-

mula needs to extend one row beyond

the last range. This cell will return any-

thing greater than the final range value.

Both of the methods above will auto-

matically recalculate when the original

data changes. If your data is static, then

a pivot table could be the easiest way to

generate the analysis. To create the pivot

table shown in C19:D25, follow these

steps:

1. Select one cell in the original data.

2. Choose Insert, PivotTable.

3. In the Create PivotTable dialog,

choose Existing Worksheet and set

the location to be cell C19. Click OK.

4. Drag the Value field to the Row

Labels.

5. Select the first value in the new pivot

table.

6. From the PivotTable Tools Options

ribbon tab, select Group Field.

7. In the Grouping dialog, specify

Starting at: 0, Ending at: 50, and

By: 10. Click OK.

8. Drag the Value field to the Values

drop zone. Excel will automatically

choose to Count instead of Summing.

Note that this step has to be done

after you’ve grouped the row field in

steps 5-7. If you try to add anything

to the Values drop zone before

grouping, Excel will sum the results.

9. On the PivotTable Tools Design tab,

open the Report Layout dropdown

and choose Show in Tabular

Form.

While the pivot table is the least con-

fusing method, the results won’t update

automatically if the underlying data

changes. You’ll have to right-click the

pivot table and choose Refresh to update

the results. SF

Bill Jelen is the host of MrExcel.com and

the author of 34 books about Microsoft

Excel, including Learn Excel 2007-2010

from MrExcel. Send questions for future

columns to [email protected].

Fe b r u a r y 2 0 1 2 I S T R AT E G IC F I N A N C E 55

Figure 3