Useful Excel Functions etc

63
CONFIDENTIAL Useful Functions for all Excel Occasions This document is still in draft and includes the responses to FAQs received from CSS in the ASO and GCO regions for 2006. For any further information please contact: [email protected] January 17, 2006 This report is solely for the use of client personnel. No part of it may be circulated, quoted, or reproduced for distribution outside the client organization without prior written approval from McKinsey & Company.

Transcript of Useful Excel Functions etc

Page 1: Useful Excel Functions etc

CONFIDENTIAL

Useful Functions for all Excel Occasions

This document is still in draft and includes the responses to FAQs received from CSS in the ASO and GCO regions for 2006. For any further information please contact:

[email protected]

January 17, 2006

This report is solely for the use of client personnel. No part of it may be circulated, quoted, or reproduced for distribution outside the client organization without prior written approval from McKinsey & Company.

Page 2: Useful Excel Functions etc

Table of Contents

Crucial Functions 5

Match 5

And 6

Index 7

Indirect 7

If 8

Duplicate Entries 9

Testing for duplicate entries 9

Highlighting duplicate entries 9

Tagging duplicate entries 10

Unique Entries 10

Counting unique entries in a Range 10

Extracting unique entries 11

Extracting values common to two lists 12

Extracting values on one list and not another12

Text 12

Returning first word in a String 12

Returning last word in a String 13

Returning All But First Word In A String 13

Returning any word or words in a String 13

Assigning a letter to a numeric value 14

Page 3: Useful Excel Functions etc

Range Names 15

Offset Function – basis of Dynamic Range Names 15

Dynamic Range Names – for Pivot Tables, data validation, etc 16

Separating Text strings – e.g., First and Last Names 17

Address of first minimum in a Range 18

Address of the last minimum in a Range 18

Address of first maximum in a Range 19

Address of the last maximum in a Range 19

Most common String in a Range 19

A Dynamic Range within another Range 20

Numbers 21

Ranking Numbers 21

Summing every Nth value 21

Functions 22

Left Lookups 22

Summing and Counting using multiple criteria 23

Perform two-way table lookups 25

User Defined Functions 26

Example (Easy): Calculate the area of a rectangle 26

Example (Easy): Calculate Fuel Consumption29

Examples of UDFs 30

Find the nearest Saturday 33

Page 3 of 48

Page 4: Useful Excel Functions etc

Remove Spaces 34

Data Validation 38

Validation list 38

Validation formulae 38

Pivot Tables 41

Custom Calculations 41

% of Row 43

% of Total 44

Difference from 44

% Difference from 45

% Of 46

Running Total in 47

Page 4 of 48

Page 5: Useful Excel Functions etc

CRUCIAL FUNCTIONS

Match

=MATCH(lookup_value,lookup_array,match_type)

¶ Lookup_value is the value you use to find the value you want in a table.

Lookup_value is the value you want to match in lookup_array. For example, when you look up someone's number in a telephone book, you are using the person's name as the lookup value, but the telephone number is the value you want.

Lookup_value can be a value (number, text, or logical value) or a cell reference to a number, text, or logical value.

¶ Lookup_array is a contiguous range of cells containing possible lookup values. Lookup_array must be an array or an array reference.

¶ Match_type is the number -1, 0, or 1. Match_type specifies how Microsoft Excel matches lookup_value with values in lookup_array.

If match_type is 1, MATCH finds the largest value that is less than or equal to lookup_value. Lookup_array must be placed in ascending order: ...-2, -1, 0, 1, 2, ..., A-Z, FALSE, TRUE.

If match_type is 0, MATCH finds the first value that is exactly equal to lookup_value. Lookup_array can be in any order.

If match_type is -1, MATCH finds the smallest value that is greater than or equal to lookup_value. Lookup_array must be placed in descending order: TRUE, FALSE, Z-A, ...2, 1, 0, -1, -2, ..., and so on.

If match_type is omitted, it is assumed to be 1.

Note:

¶ MATCH returns the position of the matched value within lookup_array, not the value itself. For example, MATCH("b",{"a","b","c"},0) returns 2, the relative position of "b" within the array {"a","b","c"}.

Page 5 of 48

Page 6: Useful Excel Functions etc

¶ MATCH does not distinguish between uppercase and lowercase letters when matching text values.

¶ If MATCH is unsuccessful in finding a match, it returns the #N/A error value.

¶ If match_type is 0 and lookup_value is text, lookup_value can contain the wildcard characters asterisk (*) and question mark (?). An asterisk matches any sequence of characters; a question mark matches any single character.

Example

And

=AND(logical1,logical2, ...)

Logical1, logical2, ... are 1 to 30 conditions you want to test that can be either TRUE or FALSE.

Note:

¶ The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays or references that contain logical values.

Page 6 of 48

Page 7: Useful Excel Functions etc

¶ If an array or reference argument contains text or empty cells, those values are ignored.

¶ If the specified range contains no logical values, AND returns the #VALUE! error value.

Index

Returns a value or the reference to a value from within a table or range. There are two forms of the INDEX() function: array and reference. The array form always returns a value or an array of values; the reference form always returns a reference.

¶ INDEX(array,row_num,column_num) returns the value of a specified cell or array of cells within array.

¶ INDEX(reference,row_num,column_num,area_num) returns a reference to specified cells within reference.

Indirect

=indirect(ref_text,a1)

¶ Ref_text is a reference to a cell that contains an A1-style reference, an R1C1-style reference, a name defined as a reference, or a reference to a cell as a text string. If ref_text is not a valid cell reference, INDIRECT returns the #REF! error value.

If ref_text refers to another workbook (an external reference), the other workbook must be open. If the source workbook is not open, INDIRECT returns the #REF! error value.

¶ A1 is a logical value that specifies what type of reference is contained in the cell ref_text.

If a1 is TRUE or omitted, ref_text is interpreted as an A1-style reference.

If a1 is FALSE, ref_text is interpreted as an R1C1-style reference.

Page 7 of 48

Page 8: Useful Excel Functions etc

If

=(logical_test,value_if_true,value_if_false)

The IF statement returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE. Use IF to conduct conditional tests on values and formulas.

¶ Logical_test is any value or expression that can be evaluated to TRUE or FALSE. For example, A10=100; if the value in cell A10 is equal to 100, the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.

¶ Value_if_true is the value that is returned if logical_test is TRUE. For example, if this argument is the text string "Within budget" and the logical_test argument evaluates to TRUE, then the IF function displays the text "Within budget". If logical_test is TRUE and value_if_true is blank, this argument returns zero. Value_if_true can be another formula.

¶ Value_if_false is the value that is returned if logical_test is FALSE. Value_if_false can be another formula.

Note:

¶ Up to seven IF functions can be nested as value_if_true and value_if_false arguments to construct more elaborate tests. See the last of the following examples.

¶ When the value_if_true and value_if_false arguments are evaluated, IF returns the value returned by those statements.

¶ If any of the arguments to IF are arrays, every element of the array is evaluated when the IF statement is carried out.

Page 8 of 48

Page 9: Useful Excel Functions etc

DUPLICATE ENTRIES

Testing for duplicate entries

If you need to determine whether a list in Excel has duplicate entries, use the following formula. It will display "Duplicates" if the list in Range1 has duplicate entries, or "No Duplicates" if the range does not have any duplicates.

=IF(MAX(COUNTIF(Range1,Range1))>1,"Duplicates","No Duplicates")

This is an array formula, so press Ctrl+Shift+Enter rather than just Enter when you first enter the formula, and whenever you edit it.

This formula requires that the complete range contain data. If only the first N cells contain data, and the rest are empty, the formula will return "Duplicates" because it considers the empty cells to be duplicates of themselves.

If you want the formula to look only that the cells that contains data, use a formula like the following:

=IF(MAX(COUNTIF(INDIRECT("A2:A"&(MAX((A2:A500<>"")*ROW(A2:A500)))),INDIRECT("A2:A"&(MAX((A2:A500<>"")*ROW(A2:A500))))))>1,"Duplicates","No Duplicates")

This formula will look only that the cells from A2 down to the last cell that contains data. This is an array formula, so press Ctrl+Shift+Enter.

Highlighting duplicate entries

Use Excel's Conditional Formatting tool to accomplish this. Highlight the entire Range1. Select Format>Conditional Formatting. Change the "Cell Value Is" option to "Formula Is" and enter the following formula in the formula text box:

=IF(COUNTIF(Range1, A5)>1,TRUE,FALSE)

Page 9 of 48

Page 10: Useful Excel Functions etc

Where A5 is the first cell in Range1. Then, click the Format button and select the font or color you want your cell formatted with. Click OK.

Duplicate entries in Range1 will be formatted as you selected.

Tagging duplicate entries

Instead of highlighting duplicates, you may want to "tag" them by placing the word "Duplicate" next to each duplicate entry in the range. In an empty column next to Range1, enter the following formula, and autofill the column with the formula:

=IF(COUNTIF(Range1,XXX)>1,"Duplicate","")

Change XXX to the first cell of Range1. If a cell is duplicated in Range1, the word "Duplicate" will appear next to each cell.

UNIQUE ENTRIES

Counting unique entries in a Range

To count the number of unique entries in the list, use the following array formula:

=SUM(IF(FREQUENCY(IF(LEN(Range1)>0,MATCH(Range1,Range1,0),""),

IF(LEN(Range1)>0,MATCH(Range1,Range1,0),""))>0,1))

This will return the number of unique entries in Range1. It will not count blanks at all.

Page 10 of 48

Page 11: Useful Excel Functions etc

If your data does not have any blanks in the range, use the following Array Formula:

=SUM(1/COUNTIF(A1:A10,A1:A10))

If your data has only numeric values or blank cells, with no text or string values, you can use the formula to count the number of unique values. This will count only the number of unique numeric values, not including text values.

=SUM(N(FREQUENCY(A1:A10,A1:A10)>0))

Extracting unique entries

In some circumstances, it is easier to use Excel's Advance Filtering capabilities to extract the unique values. There is one drawback. If you change the contents of the list, you have to manually recreate the list of filtered data. By using worksheet functions to extract the unique entries, the extracted data dynamic.

You can do this with an array formula.

=IF(COUNTIF($A$1:A1,A1)=1,A1,"")

Enter this formula in the first cell of the range you want to contain the unique entries. Change A1 and $A$1 to the first cell in the range containing the data that you want to extract unique items from. Use autofill down to as many rows as you need to hold the unique entries (i.e., up to as many rows as there are in the original range.)

You can transfer these values to another range of cells and eliminate the blank entries.

Page 11 of 48

Page 12: Useful Excel Functions etc

Extracting values common to two lists

You can extract values that appear in both of two lists. Suppose your lists are in A1:A10 and B1:B10. Enter the following array formula in the first cell of the range which is to contain the common entries:

=IF(COUNTIF($A$1:$A$10,B1)>0,B1,"")

Change B1 and $A$1:$A$10 to the first cells in the ranges from which data that you want to extract common items. Autofill the formula down to as many rows as you need to hold the common entrie.

Extracting values on one list and not another

Suppose there are two lists, in A1:A10 and B1:B10. Enter the following array formula in the first cell of the range that is to contain the entries in B1:B10 that do not occur in A1:A10.

=IF(COUNTIF($A$1:$A$10,B1)=0,B1,"")

Change B1 and $A$1:$A$10 to the first cells in the ranges from which data that you want to extract items. Use autofill to as many rows as you need to hold the common entries.

You can then transfer these entries to another range of cells and eliminate the blank entries.

TEXT

Returning first word in a String

=LEFT(A1,FIND(" ",A1,1))

Page 12 of 48

Page 13: Useful Excel Functions etc

This will return the word "First".

Returning last word in a String

=RIGHT(A1,LEN(A1)-MAX(ROW(INDIRECT("1:"&LEN(A1))) *(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)=" ")))

This formula is an array formula.

Returning All But First Word In A String

=RIGHT(A1,LEN(A1)-FIND(" ",A1,1))

This will return the words "Second Third Last"

Returning any word or words in a String

To return any single word from a single-spaced string of words, use the following array formula:

=MID(A10,SMALL(IF(MID(" "&A10,ROW(INDIRECT("1:"&LEN(A10)+1)),1)=" ",ROW(INDIRECT("1:"&LEN(A10)+1))),B10),SUM(SMALL(IF(MID(" "&A10&" ",ROW(INDIRECT("1:"&LEN(A10)+2)),1)=" ",ROW(INDIRECT("1:"&LEN(A10)+2))),B10+{0,1})*{-1,1})-1)

Where A10 is the cell containing the text, and B10 is the number of the word you want to get.

This formula can be extended to get any set of words in the string. To get the words from M for N words (e.g., the 5th word for 3, or the 5th, 6th, and 7th words), use the following array formula:

Page 13 of 48

Page 14: Useful Excel Functions etc

=MID(A10,SMALL(IF(MID(" "&A10,ROW(INDIRECT("1:"&LEN(A10)+1)),1)=" ",ROW(INDIRECT("1:"&LEN(A10)+1))),B10),SUM(SMALL(IF(MID(" "&A10&" ",ROW(INDIRECT("1:"&LEN(A10)+2)),1)=" ",ROW(INDIRECT("1:"&LEN(A10)+2))),B10+C10*{0,1})*{-1,1})-1)

Where A10 is the cell containing the text, B10 is the number of the word to get, and C10 is the number of words, starting at B10, to get.

Note: in the above array formulas, the {0,1} and {-1,1} are enclosed in array braces (curly brackets {} ) not parentheses.

Assigning a letter to a numeric value

First create a define name called "Grades" which refers to the array:

={0,"F";60,"D";70,"C";80,"B";90,"A"}

Then, use VLOOKUP to convert the number to the grade:

=VLOOKUP(A1,Grades,2)

where A1 is the cell contains the numeric value. You can add entries to the Grades array for other grades like C- and C+. Just make sure the numeric values in the array are in increasing order.

Page 14 of 48

Page 15: Useful Excel Functions etc

RANGE NAMES

Offset Function – basis of Dynamic Range Names

Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells. The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and the number of columns to be returned.

=OFFSET(reference,rows,cols,height,width)

Reference is the reference from which you want to base the offset. Reference must refer to a cell or range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.

Rows is the number of rows, up or down, that you want the upper-left cell to refer to. Using 5 as the rows argument specifies that the upper-left cell in the reference is five rows below reference. Rows can be positive (which means below the starting reference) or negative (which means above the starting reference).

Cols is the number of columns, to the left or right, that you want the upper-left cell of the result to refer to. Using 5 as the cols argument specifies that the upper-left cell in the reference is five columns to the right of reference. Cols can be positive (which means to the right of the starting reference) or negative (which means to the left of the starting reference).

Height is the height, in number of rows, that you want the returned reference to be. Height must be a positive number.

Width is the width, in number of columns, that you want the returned reference to be. Width must be a positive number.

Remarks

If rows and cols offset reference over the edge of the worksheet, OFFSET returns the #REF! error value.

If height or width is omitted, it is assumed to be the same height or width as reference.

Page 15 of 48

Page 16: Useful Excel Functions etc

OFFSET doesn't actually move any cells or change the selection; it just returns a reference. OFFSET can be used with any function expecting a reference argument. For example, the formula SUM(OFFSET(C2,1,2,3,1)) calculates the total value of a 3-row by 1-column range that is 1 row below and 2 columns to the right of cell C2.

=OFFSET(DynaVlookup!$A$1,0,0,COUNTA(DynaVlookup!$A:$A),COUNTA(DynaVlookup!$1:$1))

=OFFSET(reference,rows,cols,height,width)

Dynamic Range Names – for Pivot Tables, data validation, etc

You can define a name to refer to a range whose size varies depending on its contents. You may want a range name that refers only to the portion of a list of numbers that are not blank - such as only the first N non-blank cells in A2:A20.

Define a name called MyRange, and set the Refers To property to:

=OFFSET(Sheet1!$A$2,0,0,COUNTA($A$2:$A$20),1)

Be sure to use absolute cell references in the formula.

Expand down as many rows as there are numeric entries.

In the Refers to box type: =OFFSET($A$1,0,0,COUNT($A:$A),1)

Expand down as many rows as there are numeric and text entries.

In the Refers to box type: =OFFSET($A$1,0,0,COUNTA($A:$A),1)

Expand down to the last numeric entry

In the Refers to box type: =OFFSET($A$1,0,0,MATCH(1E+306,$A:$A,1),1)

Page 16 of 48

Page 17: Useful Excel Functions etc

If you expect a number larger than 1E+306 (a one with 306 zeros) then change this to a larger number.

Expand down to the last text entry

In the Refers to box type: =OFFSET($A$1,0,0,MATCH("*",$A:$A,-1),1)

Expand down based on another cell value

Put the number 10 in cell B1 first then:

In the Refers to box type: =OFFSET($A$1,0,0,$B$1,1)

Now change the number in cell B1 and the range will change accordingly.

Expand down one row each month

In the Refers to box type: =OFFSET($A$1,0,0,MONTH(TODAY()),1)

Expand down one row each week

In the Refers to box type: =OFFSET($A$1,0,0,WEEKNUM(TODAY()),1)

N.B. Requires the "Analysis Toolpak" to be installed. Tools>Add-ins-Analysis Toolpak

Separating Text strings – e.g., First and Last Names

Suppose you've got a range of data consisting of people's first and last names.

There are several formulas that will break the names apart into first and last names

separately. Cell A2 contains the name "John A Lee".

Page 17 of 48

Page 18: Useful Excel Functions etc

To return the last name, use

=RIGHT(A2,LEN(A2)-FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)-

LEN(SUBSTITUTE(A2," ","")))))

To return the first name, including the middle name (if present), use

=LEFT(A2,FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)-

LEN(SUBSTITUTE(A2," ",""))))-1)

To return the first name, without the middle name (if present), use

=LEFT(B2,FIND(" ",B2,1))

Address of first minimum in a Range

To return the address of the cell containing the first (or only) instance of the minimum of a list, use the following array formula:

=ADDRESS(MIN(IF(NumRange=MIN(NumRange),ROW(NumRange))),COLUMN(NumRange),4)

This function returns B2, the address of the first '1' in the range.

Address of the last minimum in a Range

To return the address of the cell containing the last (or only) instance of the minimum of a list, use the following array formula:

=ADDRESS(MAX(IF(NumRange=MIN(NumRange),ROW(NumRange)*(NumRange<>""))),COLUMN(NumRange),4)

Page 18 of 48

Page 19: Useful Excel Functions etc

This function returns B4, the address of the last '1' in the range.

Address of first maximum in a Range

To return the address of the cell containing the first instance of the maximum of a list, use the following array formula:

=ADDRESS(MIN(IF(NumRange=MAX(NumRange),ROW(NumRange))),COLUMN(NumRange),4)

This function returns B1, the address of the first '5' in the range.

Address of the last maximum in a Range

To return the address of the cell containing the last instance of the maximum of a list, use the following array formula:

=ADDRESS(MAX(IF(NumRange=MAX(NumRange),ROW(NumRange)*(NumRange<>""))),COLUMN(NumRange),4)

This function returns B5, the address of the last '5' in the range.

Most common String in a Range

The following array formula will return the most frequently used entry in a range:

=INDEX(Rng,MATCH(MAX(COUNTIF(Rng,Rng)),COUNTIF(Rng,Rng),0))

Where Rng is the range containing the data.

Page 19 of 48

Page 20: Useful Excel Functions etc

A Dynamic Range within another Range

This dynamic range is slightly more complicated, but is still along the same lines. What this one will enable us to do is nominate a dynamic range from within another range. We have no idea which row the range will start or end on and there is data both above and below the range.

Suppose we have a list of names in column "A" and these names have been sorted in ascending order (A:Z). Suppose that we need to create a dynamic range of all the names that start with the letter "H". To make matters worse, names will be added and deleted from the list from time-to-time. This means our dynamic range must start from the first name beginning with "H" and end with the last name beginning with "H".

Here is how we would do this. As with all the examples we must place the formula in the "Refers to" box of the Insert name dialog.

=OFFSET(INDIRECT(ADDRESS(MATCH("H*",Sheet1!$A$2:$A$1000,0)+1,1)),0,0,COUNTIF(Sheet1!$A$2:$A$1000,"H*"),1)

We start from cell $A$2 to allow for a heading.

The best way to see this range in action is to type some names in column "A" and sort the names in ascending order (A:Z) order. Make sure you have some names that start with the letter "H".

Add the above formula in the "Refers to" box of the Insert name dialog and call it MyRange. Push F5 and type MyRange in the Go to "Reference" box and click OK. Add some more names to the list and again sort in acsending order (A:Z), once again push F5 and type MyRange in the Go to "Reference" box and click OK.

Page 20 of 48

Page 21: Useful Excel Functions etc

NUMBERS

Ranking Numbers

Often, it is useful to be able to return the N highest or lowest values from a range of data. Suppose we have a range of numeric data called RankRng. Create a range next to RankRng (starting in the same row, with the same number of rows) called TopRng.

Also, create a named cell called TopN, and enter into it the number of values you want to return (e.g., 5 for the top 5 values in RankRng). Enter the following formula in the first cell in TopRng, and use Fill Down to fill out the range:

=IF(ROW()-ROW(TopRng)+1>TopN,"",LARGE(RankRng,ROW()-ROW(TopRng)+1))

To return the TopN smallest values of RankRng, use

=IF(ROW()-ROW(TopRng)+1>TopN,"",SMALL(RankRng,ROW()-ROW(TopRng)+1))

The list of numbers returned by these functions will automatically change as you change the contents of RankRng or TopN.

Summing every Nth value

You can easily sum (or average) every Nth cell in a column range. For example, suppose you want to sum every 3rd cell. Suppose your data is in A1:A20, and N = 3 is in D1. The following array formula will sum the values in A3, A6, A9, etc.

=SUM(IF(MOD(ROW($A$1:$A$20),$D$1)=0,$A$1:$A$20,0))

If you want to sum the values in A1, A4, A7, etc., use the following array formula:

Page 21 of 48

Page 22: Useful Excel Functions etc

=SUM(IF(MOD(ROW($A$1:$A$20)-1,$D$1)=0,$A$1:$A$20,0))

If your data ranges does not begin in row 1, the formulas are slightly more complicated. Suppose our data is in B3:B22, and N = 3 is in D1. To sum the values in rows 5, 8, 11, etc, use the following array formula:

=SUM(IF(MOD(ROW($B$3:$B$22)-ROW($B$3)+1,$D$1)=0,$B$3:B$22,0))

If you want to sum the values in rows 3, 6, 9, etc, use the following array formula:

=SUM(IF(MOD(ROW($B$3:$B$22)-ROW($B$3),$D$1)=0,$B$3:B$22,0))

FUNCTIONS

Left Lookups

The easiest way do table lookups is with the =VLOOKUP function. However, =VLOOKUP requires that the value returned be to the right of the value you're looking up. If you're looking up a value in column B, you cannot retrieve values in column A. If you need to retrieve a value in a column to the left of the column containing the lookup value, use either of the following formulas:

=INDIRECT(ADDRESS(ROW(Rng)+MATCH(C1,Rng,0)-1,COLUMN(Rng)-ColsToLeft))

or

=INDIRECT(ADDRESS(ROW(Rng)+MATCH(C1,Rng,0)-1,COLUMN(A:A) ))

Where Rng is the range containing the lookup values, and ColsToLeft is the number of columns to the left of Rng that the retrieval values are.

Page 22 of 48

Page 23: Useful Excel Functions etc

In the second syntax, replace "A:A" with the column containing the retrieval data. In both examples, C1 is the value you want to look up.

Summing and Counting using multiple criteria

The example formulas presented in this tip use the simple database table shown below.

Sum of Sales, where Month="Jan"

This is a straightforward use of the SUMIF function (uses a single criterion):

=SUMIF(A2:A10,"Jan",C2:C10)

Count of Sales, where Month="Jan"

This is a straightforward use of the COUNTIF function (single criterion):

=COUNTIF(A2:A10,"Jan")

Page 23 of 48

Page 24: Useful Excel Functions etc

Sum of Sales, where Month<>"Jan"

Another simple use of SUMIF (single criterion):

=SUMIF(A2:A10,"<>Jan",C2:C10)

Sum of Sales where Month="Jan" or "Feb"

For multiple OR criteria in the same field, use multiple SUMIF functions:

=SUMIF(A2:A10,"Jan",C2:C10)+SUMIF(A2:A10,"Feb",C2:C10)

Sum of Sales where Month="Jan" AND Region="North"

For multiple criteria in different fields, the SUMIF function doesn't work. However, you can use an array formula. When you enter this formula, use Ctrl+Shift+Enter:

=SUM((A2:A10="Jan")*(B2:B10="North")*C2:C10)

Sum of Sales where Month="Jan" AND Region<>"North"

Use Ctrl+Shift+Enter:

=SUM((A2:A10="Jan")*(B2:B10<>"North")*C2:C10)

Count of Sales where Month="Jan" AND Region="North"

For multiple criteria in different fields, the COUNTIF function doesn't work. Use Ctrl+Shift+Enter:

=SUM((A2:A10="Jan")*(B2:B10="North"))

Sum of Sales where Month="Jan" AND Sales>= 200

Requires an array formula similar to the previous example. Use Ctrl+Shift+Enter:

=SUM((A2:A10="Jan")*(C2:C10>=200)*(C2:C10))

Page 24 of 48

Page 25: Useful Excel Functions etc

Sum of Sales between 300 and 400

This also requires an array formula. Use Ctrl+Shift+Enter:

=SUM((C2:C10>=300)*(C2:C10<=400)*(C2:C10))

Count of Sales between 300 and 400

This also requires an array formula. Use Ctrl+Shift+Enter:

=SUM((C2:C10>=300)*(C2:C10<=400))

Perform two-way table lookups

The lookup functions in Excel are only appropriate for one-way lookups. If you need to perform a two-way lookup, you'll need more than the standard functions.

The figure below shows a simple example.

The formula in cell H4 looks up the entries in cells H2 and H3 and then returns the corresponding value from the table. The formula in H4 is:

Page 25 of 48

Page 26: Useful Excel Functions etc

=INDEX(A1:E14, MATCH(H2,A1:A14,0), MATCH(H3,A1:E1,0)).

The formula uses the INDEX function, with three arguments:

1. The first is the entire table range (A1:A14).

2. The second uses the MATCH function to return the offset of the desired month in column A.

3. The third argument uses the MATCH function to return the offset of the desired product in row 1.

USER DEFINED FUNCTIONS

A User Defined Function (UDF) is a function that you create yourself with VBA. UDFs are also called "Custom Functions". A UDF can remain in a code module attached to a workbook, in which case it will always be available when that workbook is open. Alternatively you can create your own add-in containing one or more functions that you can install into Excel just like a commercial add-in.

Like any function, the UDF can be as simple or as complex as you want.

Example (Easy): Calculate the area of a rectangle

This is the calculation to be done:

AREA = LENGTH x WIDTH

Open a new workbook and then open the Visual Basic Editor (Tools > Macro > Visual Basic Editor or ALT+F11).

Page 26 of 48

Page 27: Useful Excel Functions etc

You will need a module in which to write your function so choose Insert > Module. Into the empty module type: Function Area and press ENTER.

The Visual Basic Editor completes the line for you and adds an End Function line as if you were creating a subroutine.

Function Area()

End Function

Place your cursor between the brackets after "Area".

We are going to specify the "arguments" that our function will take (an argument is a piece of information needed to do the calculation).

Type Length as double, Width as double and click in the empty line underneath.

Function Area(Length As Double, Width As Double)

End Function

Declaring the data type of the arguments is not obligatory but makes sense. Warning Excel what data type to expect helps your code run more quickly and picks up errors in input. The double data type refers to number (which can be very large) and allows fractions.

Page 27 of 48

Page 28: Useful Excel Functions etc

The calculation

In the empty line first press the TAB key to indent your code (making it easier to read) and type Area = Length * Width.

Function Area(Length As Double, Width As Double)

Area = Length * Width

End Function

You can test your function right away. Switch to Excel and enter figures for Length and Width in separate cells. In a third cell enter your function as if it were one of the built-in ones. In this example cell A1 contains the length (17) and cell B1 the width (6.5). In C1 I typed =area(A1,B1) and the new function calculated the area (110.5)

Sometimes, a function's arguments can be optional. In this example we could make the Width argument optional. Supposing the rectangle happens to be a square with Length and Width equal. To save the user having to enter two arguments we could let them enter just the Length and have the function use that value twice (i.e. multiply Length x Length). So the function knows when it can do this we must include an IF Statement to help it decide.

Page 28 of 48

Page 29: Useful Excel Functions etc

Change the code to look like this...

Function Area(Length As Double, Optional Width As Variant)

If IsMissing(Width) Then

Area = Length * Length

Else

Area = Length * Width

End If

End Function

Note that the data type for Width has been changed to Variant to allow for null values. The function now allows the user to enter just one argument e.g. =area(A1). The IF Statement in the function checks to see if the Width argument has been supplied and calculates accordingly...

Example (Easy): Calculate Fuel Consumption

I like to keep a check on my car's fuel consumption so when I buy fuel I make a note of the mileage and how much fuel it takes to fill the tank.

In the Australia fuel is sold in liters. The car's odometer records distance in miles. And because I'm from the US, I only understand MPG (miles per gallon).

The calculation is the number of miles the car has traveled since the last fill-up divided by the number of gallons of fuel used.

MPG = (MILES THIS FILL - MILES LAST FILL) / GALLONS OF FUEL

but because the fuel comes in liters and there are 4.546 liters in a gallon..

Page 29 of 48

Page 30: Useful Excel Functions etc

MPG = (MILES THIS FILL - MILES LAST FILL) / LITRES OF FUEL x 4.546

Here is how the function looks…

Function MPG(StartMiles As Integer, FinishMiles As Integer, Litres As Single)

MPG = (FinishMiles - StartMiles) / Litres * 4.546

End Function

and here's how it looks on the worksheet...

EXAMPLES OF UDFS

Work out a person’s age

Function Age(DoB As Date)

Age = Int((Date - DoB) / 365.25)

End Function

Page 30 of 48

Page 31: Useful Excel Functions etc

The calculation used in the example above is very accurate, but not completely accurate. It works on the principle that there is an average of 365.25 days in a year (usually 365 but 366 every fourth year) so dividing a person's age in days by 365.25 should give their age in years.

This works fine most of the time but it can (rarely) throw up an error. If the person in question has their birthday today and were born on a year that is a multiple of 4 years ago, the calculation will be a year out. A small possibility, but if we're going to do it we might as well do it right!

Function Age(DoB As Date)

If DoB = 0 Then

Age = "No Birthdate"

Else

Select Case Month(Date)

Case Is < Month(DoB)

Age = Year(Date) - Year(DoB) - 1

Case Is = Month(DoB)

If Day(Date) >= Day(DoB) Then

Age = Year(Date) - Year(DoB)

Else

Age = Year(Date) - Year(DoB) - 1

End If

Case Is > Month(DoB)

Age = Year(Date) - Year(DoB)

End Select

End If

End Function

How the code works...

Function Age(DoB As Date) Gives a name to the function and declares that a single argument is needed, which must be a date. 

If DoB = 0 Then An IF statement to determine whether

Page 31 of 48

Page 32: Useful Excel Functions etc

Age = "No Birthdate" there is a value in the data cell. The value of an empty cell is read as zero. If that is true then the function returns the text "No Birthdate". 

ElseSelect Case Month(Date) 

If the data cell is not empty, consider the month that it is today... 

Case Is < Month(DoB)Age = Year(Date) - Year(DoB) - 1

If today's month is before (i.e. less than) the month of the person's date of birth, they have not had their birthday, so their age is this year minus their birth year minus 1. 

Case Is = Month(DoB) If today's month is the same as the month of the person's date of birth we need to know whether or not they have had their birthday yet so... 

If Day(Date) >= Day(DoB) ThenAge = Year(Date) - Year(DoB)

If the today is equal to or past the day of their birthday, then they have had their birthday (or it is today) so their age is this year minus their birth year... 

ElseAge = Year(Date) - Year(DoB) - 1End If

...otherwise, they have not had their birthday so their age is this year minus their birth year minus 1. 

Case Is > Month(DoB)Age = Year(Date) - Year(DoB)

If today's month is after (i.e. greater than) the month of the person's date of birth, they have had their birthday, so their age is this year minus their birth year. 

End SelectEnd If

Closes the CASE statement, the IF

Page 32 of 48

Page 33: Useful Excel Functions etc

End Function statement and the Function.

This calculation is complex but you only have to type it once. When you have created your function all you ever have to type is its name.

Find the nearest Saturday

Public Function FindSaturday(InputDate As Date)

'Returns the date of the first Saturday following the Inputdate

FindSaturday=FormatDateTime(InputDate+(7-Weekday(InputDate)))

End Function

How does it work?

The function takes a single argument: InputDate. This is the date for which you want to find the following Saturday. It makes use of two VBA functions:

1. Weekday which returns a number from 1 to 7 depending on the day of the week of the supplied date (1=Sunday, 2=Monday etc.)

2. FormatDateTime which displays a date serial in your chosen date format (the default being General Date).

The first job is to find out how many days there are between the InputDate and the following Saturday. The Weekday function finds out what day number the InputDate is. Let’s say it's a Tuesday (i.e. day 3). Saturday is day 7 so if we subtract Tuesday from Saturday (7-3) we get an answer of 4. So our InputDate is 4 days before the following Saturday. All we have to do is add 4 to our InputDate to get the date of the following Saturday.

This is accomplished by the calculation:

InputDate + (7 - Weekday(InputDate))

Used in a worksheet o it wouldn't be perfect because the result would be returned as a number, the date serial. To solve this the calculation is wrapped inside the FormatDateTime function. This makes sure that the result is returned ready formatted as a date.

Page 33 of 48

Page 34: Useful Excel Functions etc

Remove Spaces

The function can be modified to remove any chosen character from a string of text, such as the hyphen (-) or it can be used it to tidy up data like telephone numbers, postal codes, ISBN numbers and other kinds of data that can be typed in a variety of ways.

Here's the code:

Public Function RemoveSpaces(strInput As String)

' Removes all spaces from a string of text

Test:

If InStr(strInput, " ") = 0 Then

RemoveSpaces = strInput

Else

strInput = Left(strInput, InStr(strInput, " ") - 1) _

& Right(strInput, Len(strInput) - InStr(strInput, " "))

GoTo Test

End If

End Function

Page 34 of 48

Page 35: Useful Excel Functions etc

How does it work?

This function accepts a single argument, strInput, the string of text from which any spaces have to be removed. At the core of the function is the InStr() function. The InStr() function takes two arguments: the first being the string of text to be examined, and the character to be located. It then returns the position of the first occurrence of that character.

¶ The first line of the function is a label Test: marking the start of the procedure. This is a reference point for the function to return to so that it can run again if necessary.

¶ Next comes an If Statement which uses the InStr() function to test the string to see if there are any spaces. If the function returns a zero, then the string does not contain any spaces and the function finishes, leaving strInput as it was.

¶ If the InStr function returns anything other than zero, it means that a space has been found. So the Else part of the If Statement removes it with the aid of the text functions Len(), Left() and Right().

¶ The Len() function returns the number of characters in a string (including any spaces).

The Left() function returns a given number of characters from the left side of a string.

The Right() function returns a given number of characters from the right side of a string.

How do you use it?

This function can be used in Excel to remove spaces from a string.

Page 35 of 48

Page 36: Useful Excel Functions etc

Accessing Custom Functions

If a workbook has a VBA code module attached to it that contains custom functions, those functions can be accessed within the same workbook as in the examples above. You use the function name as if it were one of Excel's built-in functions.

You can also find the functions listed in the Function Wizard (sometimes called the Paste Function tool). Use the wizard to insert a function in the normal way (Insert > Function).

Scroll down the list of function categories to find User Defined and select it to see a list of available UDFs...

    

You can see that the user defined functions lack any description other than the unhelpful "No help available" message, but you can add a short description...

Page 36 of 48

Page 37: Useful Excel Functions etc

1. Make sure you are in the workbook that contains the functions.

2. Go to Tools > Macro > Macros. You won't see your functions listed here but Excel knows about them!

3. In the Macro Name box at the top of the dialog, type the name of the function, then click the dialog's Options button. If the button is greyed out either you've spelled the function name wrong, or you are in the wrong workbook, or it doesn't exist! This opens another dialog into which you can enter a short description of the function.

4. Click OK to save the description and (here's the confusing bit) click Cancel to close the Macro dialog box.

5. Save the workbook containing the function. Next time you go to the Function Wizard your UDF will have a description...

Like macros, user defined functions can be used in any other workbook as long as the workbook containing them is open. However it is not good practice to do this. Entering the function in a different workbook is not simple. You have to add its host workbook's name to the function name. This isn't difficult if you rely on the Function Wizard, but clumsy to write out manually. The Function Wizard shows the full names of any UDFs in other workbooks...

Page 37 of 48

Page 38: Useful Excel Functions etc

If you open the workbook in which you used the function at a time when the workbook containing the function is closed, you will see an error message in the cell in which you used the function. Excel has forgotten about it! Open the function's host workbook, recalculate, and all is fine again. Fortunately there is a better way.

If you want to write User Defined Functions for use in more than one workbook the best method is to create an Excel Add-In. Please refer to the author/distributor of this manual.

DATA VALIDATION

Validation list

You can place specific values in a Named Range and use that list for acceptable values.

Data >Validation > Settings Tab > Allow: List Source: =NamedRange, > OK.

Validation formulae

When doing Data Validation you select a range, the formulas will be applied to the entire range based on the formula in the active cell, which would normally be the first cell in the range. A1 will be used for these examples.

Data > Validation > Custom > Formula is:

Page 38 of 48

Page 39: Useful Excel Functions etc

Basic entries

Any value - range, whole number - Range, Decimal- Range, List, Date - Range, Time -Range, Text length - Range

Disallow spaces

=LEN(A1)=LEN(SUBSTITUTE(A1," ",""))

=ISERROR(FIND(" ",A1))

Disallow leading or trailing spaces, also disallows consecutive internal spaces

=LEN(A1)=LEN(TRIM(A1))

Disallow ONLY CAPS

=NOT(EXACT(A1,UPPER(A1)))

Allow only those found in a list

=NOT(ISERROR(MATCH(C1,$A$1:$A$5,0)))

Allow only the negative amount of another cell

=ROUND(A1-A2,2)

Either of two choices

=OR(A1=1,A1=2)

Values 1 through 500, or 700 through 799

=OR(AND(A1>=1,A1<=500),AND(A1>=700,A1<=799))

Page 39 of 48

Page 40: Useful Excel Functions etc

Unique value in a Range

For example, suppose your worksheet will contain a list of ID numbers for employees, in cells A1:A50, and you want to prevent the user from entering duplicate numbers in the list.

Select the range A1:A50. Go to the Data menu, and select the Validation menu item.

=COUNTIF($A$1:$A$50,A1)=1

Select the "Error Alert" tab, and enter the message you want to display when the user enters a duplicate value. Press OK.

You must use absolute references in the range, and relative references in the criteria, as shown in the example.

Remember, that Data Validation does not apply to changes made when the user pastes data in to the range.

Page 40 of 48

Page 41: Useful Excel Functions etc

Require Entry in a Specific Column before allowing entry.

=NOT(TRIM(A1)="")

Choose from one of three lists based on value in A1

=Choose(A1,$A$3:$A$5,$B$3:$B$5,$C$3:$C$5) =Indirect($c$1)     where c1: =If(A1=1,"$A$3:$A$5","$B$3:$B$5","$C$3:$C$5") =list1         where list1 refers to defined name formula: =IF(A1=1,$A$3:$A$5,$B$3:$B$5,$C$3:$C$5)

Limit the Total

Prevent entry of a value that will cause a range to exceed a set total. In this example, the total budget cannot exceed $3500. The budget amounts are in cells C3:C7.

Select cells C3:C7. Choose Data >Data Validation. Choose Allow: Custom. For the formula, use SUM to total the values in the range $C$3:$C$7. The result must be less than or equal to $3500:

=SUM($C$3:$C$7)<=350

Weekday (Mon-Fri only)

Prevent entry of dates that fall on Saturday or Sunday. The WEEKDAY function returns the number for the date entered, and values of 1 (Sunday) and 7 (Saturday) are not allowed.

=AND(WEEKDAY(B2)<>1,WEEKDAY(B2)<>7)

PIVOT TABLES

Custom Calculations

In a Pivot Table, you can summarize the data by using the values in other cells in the data area.

Page 41 of 48

Page 42: Useful Excel Functions etc

For example, you can show each Region's total as a percentage of the national total. Or, calculate the difference between the sales totals for the current year, and the sales totals for the previous year.

% of Column

In this example, the pivot table has Region in the Row area, and Total in the Data area. A custom calculation will be added, to show the percentage for each region's sales, compared to the national total.

From the Pivot Table field list, drag another copy of the Total field to the Data area. If the data fields are arranged vertically, you can change them to a horizontal layout, by following the instructions here.

1. Right-click the heading cell for the new column, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. %Sales

3. Click the Options button, to expand the dialog box

4. From the Show data as dropdown list, select % of column

Page 42 of 48

Page 43: Useful Excel Functions etc

5. Click the OK button

% of Row

In this example, the pivot table has Item in the Row area, Region in the Column area, and Total in the Data area. The total will be changed to a custom calculation, to show the percentage for each region's sales of an item, compared to the item total.

1. Right-click one of the cells in the Data area, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. %Sales

3. Click the Options button, to expand the dialog box

Page 43 of 48

Page 44: Useful Excel Functions etc

4. From the Show data as dropdown list, select % of row

5. Click the OK button

% of Total

In this example, the pivot table has Item in the Row area, Region in the Column area, and Total in the Data area. The total will be changed to a custom calculation, to show the percentage for each region's sales of an item, compared to the Sales Grand Total for all Items.

1. Right-click one of the cells in the Data area, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. %Sales

3. Click the Options button, to expand the dialog box

4. From the Show data as dropdown list, select % of Total

5. Click the OK button

Difference from

In this example, the pivot table has Region in the Row area, and Total in the Data area. Date is in the Column area, grouped by Year.

Page 44 of 48

Page 45: Useful Excel Functions etc

The total will be changed to a custom calculation, to compare the current year's sales for each region, to previous year's sales, in dollars.

1. Right-click one of the cells in the Data area, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. Change

3. Click the Options button, to expand the dialog box

4. From the Show data as dropdown list, select Difference From

5. From the Base field list, choose Date

6. From the Base item list, choose (previous)

7. Click the OK button

% Difference from

In this example, the pivot table has Item in the Row area, and Total in the Data area. Date is in the Column area, grouped by Year.

Page 45 of 48

Page 46: Useful Excel Functions etc

The total will be changed to a custom calculation, to compare the current year's sales for each Item, to previous year's sales, as a percentage.

1. Right-click one of the cells in the Data area, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. %Change

3. Click the Options button, to expand the dialog box

4. From the Show data as dropdown list, select % Difference From

5. From the Base field list, choose Date

6. From the Base item list, choose (previous)

7. Click the OK button

% Of

In this example, the pivot table has Item in the Row area, Region in the Column area, and Total in the Data area.

The total will be changed to a custom calculation, to compare each Region's sales to Ontario's sales, as a percentage.

Page 46 of 48

Page 47: Useful Excel Functions etc

1. Right-click one of the cells in the Data area, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. %Ontario

3. Click the Options button, to expand the dialog box

4. From the Show data as dropdown list, select % Of

5. From the Base field list, choose Region

6. From the Base item list, choose Ontario

7. Click the OK button

Running Total in

In this example, the pivot table has Region in the Column area, and Total in the Data area. Date is in the Column area, grouped by Year and Quarter.

The total will be changed to a custom calculation, to calculate a running total of sales for each Region, over each Year.

Page 47 of 48

Page 48: Useful Excel Functions etc

1. Right-click one of the cells in the Data area, and select Field Settings...

2. In the Field Settings dialog box, type a name for the field, e.g. Sales

3. Click the Options button, to expand the dialog box

4. From the Show data as dropdown list, select Running Total in

5. From the Base field list, choose Date

6. Click the OK button

Page 48 of 48