Use of sumif
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
=SUM(IF((A2:A9="A")+(A2:A9="B"),B2:B9,0))
=SUM(IF(A2:A9={"A","B"},B2:B9,0))
=SUMIF(A2:A9,C2:C3,B2:B9)
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.
=SUM(IF(Test1*Test2*...*Testn))
=SUM(IF(Test1+Test2+...+Testn))
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.
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))
This article offers several examples that use the INDEX and MATCH worksheet functions in Microsoft Excel to find a value based upon multiple criteria.
The following examples use the INDEX and MATCH worksheet functions to find a value based on multiple criteria.
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
=INDEX($C$2:$C$5,MATCH(D2,IF($B$2:$B$5=E2,$A$2:$A$5),0))
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:
=INDEX($C$2:$C$5,MATCH(D2&E2,$A$2:$A$5&$B$2:$B$5,0))
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
=INDEX($B$3:$E$3,MATCH(B4,IF($B$2:$E$2=B5,$B$1:$E$1),0))
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:
=INDEX($B$3:$E$3,MATCH(B4&B5,$B$1:$E$1&$B$2:$E$2,0))
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.
The syntax of these functions are defined as follows.
=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.
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(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.
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.