Functions in Excel

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

IF Function

Function Description
The Excel IF function tests a supplied condition and returns one result if the condition
evaluates to TRUE, and another result if the condition evaluates to FALSE.

The syntax of the function is:

IF( logical_test, value_if_true, value_if_false )


where the arguments are as follows:

logical_test - The condition that is to be tested and evaluated as either TRUE or


FALSE.

value_if_true - The result that is to be returned if the


supplied logical_test evaluates to TRUE.

value_if_false - The result that is to be returned if the


supplied logical_test evaluates to FALSE.

Note that the If function will still work if the logical_test returns a numeric value. In this
case, any non-zero value is treated as TRUE and zero is treated as FALSE.

Excel If Function Examples


If Function Example 1
The following spreadsheet shows two simple uses of the Excel If function. In these
examples, the logical_test checks whether the corresponding value in column A is less
than zero and returns:
• The text string "negative" if the value in column A is less 0
or
• The text string "positive" otherwise (i.e. if the value in column A is greater than or
equal to 0).
A B C
1 5 =IF( A1<0, "negative", "positive" ) - returns "positive"
2 -2 =IF( A2<0, "negative", "positive" ) - returns "negative"

SUMIFS Function
SUMIF Function
Function Description
The Excel Sumif function finds the values in a supplied array, that satisfy a given
criteria, and returns the sum of the corresponding values in a second supplied array.

The syntax of the function is:

SUMIF( range, criteria, [sum_range] )


where the function arguments are:

Range - An array of values (or range of cells containing values) to be tested


against the supplied criteria.

Criteria - The condition to be tested against each of the values in the


supplied range.

[sum_range] - An optional array of numeric values (or cells containing numeric


values), which are to be added together, if the
corresponding range entry satisfies the supplied criteria.
If the [sum_range] argument is omitted, the values from
the range argument are summed instead.

Excel Sumif Function Examples


Example 1
The following spreadsheet shows three examples of the Excel Sumif function used with
text based critia.

For each call to the Excel Sumif function, the range argument (to be tested against
the criteria) is either the cell range A2-A9 or the cell range B2-B9, and
the [sum_range] argument (containing the values to be summed) is the cell range C2-
C9.
Formulas: Results:
A B C A B C
1 Month Team Sales 1 Month Team Sales
2 Jan North 1 $36,693 2 Jan North 1 $36,693
3 Jan North 2 $22,100 3 Jan North 2 $22,100
4 Jan South 1 $53,321 4 Jan South 1 $53,321
5 Jan South 2 $34,440 5 Jan South 2 $34,440
6 Feb North 1 $29,889 6 Feb North 1 $29,889
7 Feb North 2 $50,090 7 Feb North 2 $50,090
8 Feb South 1 $32,080 8 Feb South 1 $32,080
9 Feb South 2 $45,500 9 Feb South 2 $45,500
10 10
11 =SUMIF( A2:A9, "Feb", C2:C9 ) 11 $157,559 - sum of cells C6-C9
12 =SUMIF( B2:B9, "North 1", C2:C9 ) 12 $66,582 - sum of cells C2 & C6
13 =SUMIF( B2:B9, "North*", C2:C9 ) 13 $138,772 - sum of cells C2, C3, C6 & C7
Note that, in the example above:

• The function in cell A13 uses the wildcard * and so finds cells in the range B2-B9 that
begin with the text string "North". This is satisfied by the values "North 1" and "North
2".

• In all three examples, the text based criteria (including the wildcard) are encased in
quotes.

Example 2
The following example shows the Excel Sumif function using critia based on numeric
values.

Formulas: Results:
A B C A B C
1 1 200 1 1 200
2 2 45 2 2 45
3 1 550 3 1 550
4 2 450 4 2 450
5 1 20 5 1 20
6 6
7 =SUMIF( A1:A5, 1, B1:B5 ) 7 770 - sum of cells B1, B3 & B5
8 =SUMIF(B1:B5, ">100" ) 8 1200 - sum of cells B1, B3 & B4
Note that, in the above spreadsheet:

• The criteria in cell A8 is an expression, and so, is enclosed in quotes.


• The [sum_range] argument is omitted from the function in cell A8 and so the values
in the range array (i.e. cells B2-B6) are summed.

SUMIFS Function
Function Description
The Excel Sumifs function finds values in one or more supplied arrays, that satisfy a set
of criteria, and returns the sum of the corresponding values in a further supplied array.

The function is new in Excel 2007, and so is not available in earlier versions of Excel.

The syntax of the Sumifs function is:

SUMIFS( sum_range, criteria_range1, criteria1, [criteria_range2, criteria2], ... )


Where the function arguments are:

sum_range - An array of numeric values (or a range of cells containing


numbers) which are to be added together if the criteria are
satisfied.

criteria_range1 - Arrays of values (or ranges of cells containing values) to be


[criteria_range2], ... tested against the respective criteria1, criteria2, ...
(The supplied criteria_range arrays must all have the same
length as the sum_range).

criteria1, - The conditions to be tested against the values


[criteria2], ... in criteria_range1, [criteria_range2], ...

Excel Sumifs Function Examples


The spreadsheet below shows the quarterly sales figures for 3 sales representatives.

The Sumifs function can be used to find total sales figures for any combination of
quarter, area and sales rep.

This is shown in the examples below.

A B C D
1 Quarter Area Sales Rep. Sales
2 1 North Jeff $223,000
3 1 North Chris $125,000
4 1 South Carol $456,000
5 2 North Jeff $322,000
6 2 North Chris $340,000
7 2 South Carol $198,000
8 3 North Jeff $310,000
9 3 North Chris $250,000
10 3 South Carol $460,000
11 4 North Jeff $261,000
12 4 North Chris $389,000
13 4 South Carol $305,000
Example 1
To find the sum of sales in the North area during quarter 1:

=SUMIFS( D2:D13, A2:A13, 1, B2:B13, "North" )

which gives the result $348,000.


In this example, the Excel Sumifs function identifies rows where:
• The value in column A is equal to 1
and
• The entry in column B is equal to "North"

and calculates the sum of the corresponding values from column D.

I.e. this formula finds the sum of the values $223,000 and $125,000 (from cells D2 and
D3).

Example 2
Again, using the data spreadsheet above, we can also use the Sumifs function to find
the total sales for "Jeff", during quarters 3 and 4:

=SUMIFS( D2:D13, A2:A13, ">2", C2:C13, "Jeff" )

This formula returns the result $571,000.


In this example, the Excel Sumifs function identifies rows where:

• The value in column A is greater than 2


and
• The entry in column C is equal to "Jeff"

and calculates the sum of the corresponding values in column D.

I.e. this formula finds the sum of the values $310,000 and $261,000 (from cells D8 and
D11).
COUNTIF Function
Function Description
The Excel Countif function returns the number of cells within a supplied range, that
satisfy a given criteria.

The syntax of the function is:

COUNTIF( range, criteria )


where the function arguments are as follows:

range - The range of cells that should be tested against the supplied criteria and
counted if the criteria is satisfied.

criteria - A user-defined condition that is tested against each of the cells in the
supplied range.

Excel Countif Function Examples


Countif Function Example 1
A B C D
1 Sunday 07-Sep-2008 0 TRUE
2 Monday 08-Sep-2008 2.1 TRUE
3 Wednesday 10-Sep-2008 2 TRUE
4 Thursday 11-Sep-2008 3 FALSE
5 Wednesday 17-Sep-2008 2.5 FALSE
6 Tuesday 23-Sep-2008 3 FALSE
7 Wednesday 24-Sep-2008 6 FALSE
8 Sunday 05-Oct-2008 4 FALSE
9 Saturday 11-Oct-2008 0 FALSE
The following example shows the Excel Countif function used to count cells containing
text strings, numeric values, dates or logical values within the data spreadsheet on the
right.
The functions and the results are shown in the spreadsheets below.

Formulas: Results:
A A
11 =COUNTIF( A1:A9, "Wednesday" ) 11 3
12 =COUNTIF( A1:A9, "<>Wednesday" ) 12 6
13 =COUNTIF( B1:B9, ">01/10/2008" ) 13 2
14 =COUNTIF( C1:C9, 0 ) 14 2
15 =COUNTIF( C1:C9, ">=3" ) 15 4
16 =COUNTIF( D1:D9, TRUE ) 16 3

COUNTIFS function
Function Description
The Excel Countifs function returns the number of rows within a table, that satisfy a set
of given criteria.

The function is new in Excel 2007, and so is not available in earlier versions of Excel.

The syntax of the Countifs function is:

COUNTIFS( criteria_range1, criteria1, [criteria_range2, criteria2], ... )


Where the function arguments are as follows:

criteria_range1 - Arrays of values (or ranges of cells containing values) to be


[criteria_range2], ... tested against the respective criteria1, [criteria2], ...
(The supplied criteria_range arrays must all have the same
length).

criteria1, - The conditions to be tested against the values


[criteria2], ... in criteria_range1, [criteria_range2], ...

Excel Countifs Function Examples


The spreadsheet below shows a set of exam results for a class of students.

The Countifs function can be used to find the number of rows in the data set that satisfy
conditions relating to the subject, score and gender.

This is shown in the examples below.

A B C D
1 Name Gender Subject Score
2 Anne Female Maths 63%
3 Anne Female English 78%
4 Anne Female Science 39%
5 Carl Male Maths 55%
6 Carl Male English 71%
7 Carl Male Science 51%
8 Kath Female Maths 78%
9 Kath Female English 81%
10 Kath Female Science 49%
11 Tony Male Maths 35%
12 Tony Male English 69%
13 Tony Male Science 65%

Example 1
If we want to know how many female test scores were greater than 60%, we could use
the following formula:

=COUNTIFS( B2:B13, "Female", D2:D13, ">60%" )


which gives the result 4.
In this example, the formula has counted the number of rows where:

• The entry in column B is equal to "Female"


and
• The entry in column D is greater than "60%".

Example 2
If we want to know how many science tests scores were less than 50%, we could use
the formula:

=COUNTIFS( C2:C13, "Science", D2:D13, "<50%" )


which gives the result 2.
HLOOKUP
Function Description
The Excel Hlookup function 'looks up' a given value in the top row of a data array (or
table), and returns the corresponding value from another row of the array.

The syntax of the function is:

HLOOKUP( lookup_value, table_array, row_index_num, [range_lookup] )


Where the function arguments are as follows:

lookup_value - The value that you want to search for, in the first row of the
supplied data array.

table_array - The data array or table, containing the data to be searched in


the top row, and the return values in any other row.

row_index_num - The row number, within the supplied table_array, that you want
the corresponding value to be returned from.

[range_lookup] - An optional logical argument, which can be set to TRUE or


FALSE, meaning:

TRUE - if the function cannot find an exact match to the


supplied lookup_value, it should use the closest
match below the supplied value.
Note: If [range_lookup] is set to TRUE, the top
row of the table_array must be in ascending
order.
FALSE - if the function cannot find an exact match to the
supplied lookup_value, it should return an error.

Hlookup Function Examples


Hlookup Example 1 - Exact Match Lookup
Cells A2-F6 of the spreadsheet below, show the exam scores for 5 students in 4
different subjects. If you want to look up a specific score (e.g. Biology) for one of the
students (e.g. Ed), this can be done using the Hlookup function, as shown in cell B10 of
the spreadsheet.

Formulas: Results:
In the above example, the Hlookup function searches through the top row of
the table_array (the range A2-F2), to find a match for the lookup_value (the name "Ed").
When the name "Ed" is found, the function returns the corresponding value from the 5th
row of the table_array.

This is illustrated in the spreadsheet on the right. The function finds the name 'Ed' in the
top row of the table_array and then returns the value '61%' from the 5th row of
the table_array.
If we change the name in cell A10 of the spreadsheet from 'Ed' to 'Cara', the Hlookup
function would automatically recalculate the function to display the exam result for Cara.

Note that although the top row of the table_array is in ascending order in this example,
this is not essential, because the [range_lookup] argument is set to FALSE (requiring an
exact match).

Hlookup Example 2 - Closest Match Lookup


Cells A1-F3 of the following spreadsheet show body types relating to body mass index
(BMI), for the ranges 0-18.4, 18.5-24.9, 25.0-29.9 and over 30.

Cell C6 shows the user's current BMI, which is 23.5, and cell C7 shows the Hlookup
function that is used to look up the body type that relates to this BMI.
The Hlookup function in the above spreadsheet returns the result "Normal Weight",
which is the correct body type for a BMI of 23.5.

Note that, in this example, the [range_lookup] argument is set to TRUE, to tell that
function that, if it cannot find an exact match to the supplied lookup_value, it should use
the closest match below this value. Therefore,
• For all BMIs up to and including 18.4 the function would return "Underweight";
• For all BMIs between 18.5 and 24.9, the function would return "Normal Weight";

VLOOKUP function
Function Description
The Excel VLOOKUP function 'looks up' a given value in the left-hand column of a data
array (or table), and returns the corresponding value from another column of the array.

The syntax of the function is:

VLOOKUP( lookup_value, table_array, col_index_num, [range_lookup] )


where the arguments are as follows:

lookup_value - The value that you want to look up.

table_array - The data array or table, containing the search values in the left
hand columns and the return values in another column.

col_index_num - The column number, within the supplied table_array, that you
want to return a value from.

[range_lookup] - An optional logical argument, that describes what the function


should return in the event that it does not find an exact match to
the lookup_value.
The [range_lookup] can be set to TRUE or FALSE, meaning:
TRUE - If the function cannot find an exact match to the
supplied lookup_value, it should use the closest
match below the supplied value.
Note: if this option is used, the left-hand column of
the table_array must be in ascending order.
FALSE - If the function cannot find an exact match to the
supplied lookup_value, it should return an error.

Vlookup Examples
Example 1 - Vlookup Requiring an Exact Match
Columns A and B of the spreadsheet below list an inventory of grocery items and their
prices. In cell E2 of the spreadsheet, the Vlookup function is used to look up the price of
an item from the inventory.
A B C D E
Cost Current
1 Item Description ($) Item: Current Item Cost ($)
Tinned =VLOOKUP( D2, A:B, 2, - returns the value
2 Tomatoes $0.90 Cornflakes FALSE ) $3.50
3 Tinned Tuna $1.50
4 Cornflakes $3.50
Shortcake
5 Biscuits $1.00
6 Toothpaste $4.10
Tinned Baked
7 Beans $0.99
White Sliced
8 Bread $0.80
. .
. .
9 . .

The above Vlookup function returns the price for "Cornflakes", which is $3.50.
In this example:
• The lookup_value is the text string "Cornflakes", which is located in cell D2;
• The table_array is defined as columns A-B of the spreadsheet;
• The col_index_num is set to 2, to denote that the value returned should be taken from
column 2 of the table_array;
• The [range_lookup] argument is set to FALSE, to indicate that we only want a result to
be returned if an exact match to the lookup_value is found.
Note that in this example, as the [range_lookup] argument is set to FALSE (requiring an
exact match), it is not necessary to order the table_array by its first column.

Example 2 - Vlookup Requiring the Closest Match


Columns A-C of the spreadsheet below list the grades that are assigned to examination
marks lying within the ranges 0-44%, 45%-54%, etc.

Cell F2 shows the score of 52% that was achieved by the student "Anne" in an
examination. The Vlookup function in cell G2 looks up this score in column A of the
spreadsheet and returns the associated grade from column C.

Note that, in this example, if the exact score of 52% is not found in column A, we want,
instead, to use the nearest value below this score.
A B C D E F G
Lowe Uppe Grad Nam Scor
1r r e e e Grade
=VLOOKUP( F2, A2:C7, 3, - returns the value
2 0% 44% F Anne 52% TRUE ) "E"
3 45% 54% E
4 55% 64% D
5 65% 74% C
6 75% 84% B
100
7 85% %A

The above Vlookup function returns the grade for the score 52%, which is E.
In this example:
The lookup_value is the value 52%, which is located in cell F2;
The table_array is the range A2-C7 of the spreadsheet;
The col_index_num is set to 3, to denote that the returned value should be taken
from column 3 of the table_array;
The [range_lookup] argument is set to TRUE, to indicate that, if an exact match to
the lookup_value is not found, we want the closest value below the lookup_value to
be returned instead.
(Note that, in this case, the first column of the table_array (i.e. cells A2-A7) must be
in ascending order).

You might also like