Use of sumif

Example 1: Nested IF Function with Boolean AND

  1. Start Excel and create the following worksheet:
       A1: Dept  B1: Employees C1: Criteria
       A2: A     B2: 2	   C2: A
       A3: B     B3: 4	   C3: B
       A4: C     B4: 3		
       A5: A     B5: 3		
       A6: B     B6: 3		
       A7: C     B7: 2		
       A8: A     B8: 4		
       A9: C     B9: 3		
  2. In cell D1, type the following formula:
  3. Press CTRL+SHIFT+ENTER (or COMMAND+RETURN on the Mac) to enter the formula as an array formula.

    The formula returns 16, the total number of employees in departments A and B.

Example 2: Nested IF Function with Boolean OR

  1. Repeat step 1 from Example 1.
  2. In cell D2, type the following formula:
  3. Press CTRL+SHIFT+ENTER (or COMMAND+RETURN on the Mac) to enter the formula as an array formula.

    The formula once again returns 16.

Example 3: SUMIF() Worksheet Function

  1. Repeat step 1 from Example 1.
  2. In cell D3, type the following formula:
  3. Press CTRL+SHIFT+ENTER (or COMMAND+RETURN on the Mac) to enter the formula as an array formula.

    The formula returns 9, the number of employees who meet criteria A.
  4. Grab the fill handle and fill the formula down to cell D4.

    The formula returns 7, the number of employees who meet criteria B.

XL: How to Use a Logical AND or OR in a SUM+IF Statement in Excel

 
The information in this article applies to:
 

SUMMARY

In Microsoft Excel, when you use the logical functions AND and/or OR inside a SUM+IF statement to test a range for more than one condition, it may not work as expected. A nested IF statement provides this functionality; however, this article discusses a second, easier method that uses the following formulas.

For AND Conditions

   =SUM(IF(Test1*Test2*...*Testn))

For OR Conditions

   =SUM(IF(Test1+Test2+...+Testn))

MORE INFORMATION

Use a SUM+IF statement to count the number of cells in a range that pass a given test or to sum those values in a range for which corresponding values in another (or the same) range meet the specified criteria. This behaves similarly to the DSUM function in Microsoft Excel.

Example

This example counts the number of values in the range A1:A10 that fall between 1 and 10, inclusively.

To accomplish this, you can use the following nested IF statement:

   =SUM(IF(A1:A10>=1,IF(A1:A10<=10,1,0)))

The following method also works and is much easier to read if you are conducting multiple tests:

   =SUM(IF((A1:A10>=1)*(A1:A10<=10),1,0))

The following method counts the number of dates that fall between two given dates:

   =SUM(IF((A1:A10>=DATEVALUE("1/10/99"))*(A1:A10<=DATEVALUE("2/10/99")),1,0))

NOTES:

 

With this method, you are multiplying the results of one logical test by another logical test to return TRUEs and FALSEs to the SUM function. You can equate these to:

   TRUE*TRUE=1
   TRUE*FALSE=0
   FALSE*TRUE=0
   FALSE*FALSE=0

The method shown above counts the number of cells in the range A1:A10 for which both tests evaluate to TRUE. To sum values in corresponding cells (for example, B1:B10), modify the formula as shown below:

   =SUM(IF((A1:A10>=1)*(A1:A10<=10),B1:B10,0))

You can implement an OR in a SUM+IF statement similarly. To do this, modify the formula shown above by replacing the multiplication sign (*) with a plus sign (+). This gives the following generic formula:

   =SUM(IF((Test1)+(Test2)+...+(Testn),1,0))

XL: How to Use INDEX and MATCH Worksheet Functions with Multiple Criteria

 
The information in this article applies to:
For a Microsoft Excel 98 and earlier version of this article, see Q59482.
 

SUMMARY

This article offers several examples that use the INDEX and MATCH worksheet functions in Microsoft Excel to find a value based upon multiple criteria.

MORE INFORMATION

The following examples use the INDEX and MATCH worksheet functions to find a value based on multiple criteria.

Example 1: Data in Columns

Method 1

  1. Start Excel.
  2. Type the following data into a new worksheet:
       A1: Part   B1:  Code   C1:  Price   D1:  Find Part  E1:  Find Code
       A2: x      B2:  11     C2:  5.00    D2:  y          E2:  12
       A3: x      B3:  12     C3:  6.00    D3:  y          E3:  11
       A4: y      B4:  11     C4:  7.00    D4:  x          E4:  12
       A5: y      B5:  12     C5:  8.00    D5:  x          E5:  11
  3. To retrieve the price for part y with code 12 and return the value to cell F2, type the following formula in cell F2:

      =INDEX($C$2:$C$5,MATCH(D2,IF($B$2:$B$5=E2,$A$2:$A$5),0))

  4. Press CTRL+SHIFT+ENTER to enter the formula as an array formula.

    The formula returns the value 8.00.
  5. Select cell F2, grab the fill handle, and then fill down to cell F5 to retrieve the price for each part and code combination.

Method 2

A second method yields the same results but uses concatenation instead. The following sample formula may be better for matching data against more than two criteria because it does not require nested IF statements. This method is identical to Method 1 except that you replace the formula in step 3 with the following formula:

Example 2: Data Arranged in Rows

Method 1

  1. Start Excel.
  2. Type the following data into a new worksheet:
       A1: Part        B1: x      C1: x     D1: y       E1: y
       A2: Code        B2: 11     C2: 12    D2: 11      E2: 12
       A3: Price       B3: 5.00   C3: 6.00  D3: 7.00    E3: 8.00
       A4: Find Part   B4: y      C4: y     D4: x       E4: x
       A5: Find Code   B5: 12     C5: 11    D5: 12      E5: 11
  3. To retrieve the price for part y with code 12 and return the value to cell B6, type the following formula in cell B6:

      =INDEX($B$3:$E$3,MATCH(B4,IF($B$2:$E$2=B5,$B$1:$E$1),0))

  4. Press CTRL+SHIFT+ENTER to enter the formula as an array formula.

    The formula returns the value 8.00.
  5. Select cell B6, grab the fill handle, and then fill right to cell E6 to retrieve the price for each part and code combination.

Method 2

A second method yields the same results but uses concatenation instead. The following sample formula may be better for matching data against more than two criteria because it does not require nested IF statements. This method is identical to Method 1 (under Example 2) except that you replace the formula in step 3 with the following formula:

XL: How to Use VLOOKUP or HLOOKUP to Find an Exact Match

 
The information in this article applies to:
 

SUMMARY

The VLOOKUP and HLOOKUP functions contain an argument called range_lookup that allows you to find an exact match to your lookup value without sorting the lookup table.

NOTE: It is not necessary to sort the lookup table if you use the range_lookup argument correctly.

MORE INFORMATION

The syntax of these functions are defined as follows.

VLOOKUP Function

   =VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)

where:

   Argument     Definition of argument
   ---------------------------------------------------------------------

   lookup_value The value to be found in the first column of the array.

   table_array  The table of information in which data is looked up.

   col_index    The column number in the table_array for which the
                matching value should be returned.

   range_lookup It is a logical value that specifies whether
                you want to find an exact match or an approximate match.
                If TRUE or omitted, an approximate match is returned; in
                other words, if an exact match is not found, the next
                largest value that is less than the lookup_value is
                returned. If FALSE, VLOOKUP finds an exact match. If an
                exact match is not found, the #N/A error value is returned.

NOTE: If range_lookup is TRUE or omitted (for an approximate match), the values in the first column of table_array must be sorted in ascending order. If range_lookup is FALSE (for an exact match), the table_array does not need to be sorted.

Example That Uses FALSE as the Range_lookup Argument

The following list contains some fruits and their respective colors. Notice that the first column is not sorted:

   A1: Fruit    B1: Color  
   A2: Kiwi     B2: Green 
   A3: Banana   B3: Yellow 
   A4: Grape    B4: Purple  
   A5: Apple    B5: Red   

The following formula finds the color (Red) that corresponds to the fruit Apple. You can type the formula in any cell on the worksheet:

=VLOOKUP("Apple",A2:B5,2,FALSE)

Notice that if you change the range_lookup argument to TRUE, Excel returns the #N/A error, because the first column is not sorted.

HLOOKUP Function

=HLOOKUP(lookup_value,table_array,row_index_num,range_lookup)

where:

   Argument     Definition of argument
   ---------------------------------------------------------------------

   lookup_value The value to be found in the first column of the array.

   table_array  The table of information in which data is looked up.

   row_index    The row number in the table_array for which the
                matching value should be returned.

   range_lookup It is a logical value that specifies whether
                you want to find an exact match or an approximate match.
                If TRUE or omitted, an approximate match is returned; in
                other words, if an exact match is not found, the next
                largest value that is less than the lookup_value is
                returned. If FALSE, VLOOKUP finds an exact match.
                If an exact match is not found, the #N/A error value is
                returned.

NOTE: If range_lookup is TRUE or omitted (for an approximate match), the values in the first row of table_array must be sorted in ascending order. If range_lookup is FALSE (for an exact match), the table_array does not need to be sorted.

Example That Uses FALSE as the Range_lookup Argument

The following list contains some fruits and their respective colors. Notice that the first column is not sorted:

   A1: Fruit    B1: Color
   A2: Kiwi     B2: Green
   A3: Banana   B3: Yellow
   A4: Grape    B4: Purple
   A5: Apple    B5: Red

The following formula finds the Color column, and returns the third item (-1) for the heading Yellow. You can type the formula in any cell on the worksheet:

=HLOOKUP("Color",A1:B5,3,FALSE)

Notice that if you change the range_lookup argument to TRUE, Excel returns the #N/A error, because the first column is not sorted.