Professional Documents
Culture Documents
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.
Table of Contents
Crucial Functions 5
Match 5
And 6
Index 7
Indirect 7
If 8
Duplicate Entries 9
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
Page 3 of 48
Example (Easy): Calculate Fuel Consumption
29
Examples of UDFs 30
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
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.
1. 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.
2. 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.
3. 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.
4. If match_type is 0, MATCH finds the first value that is
exactly equal to lookup_value. Lookup_array can be in
any order.
5. 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.
6. If match_type is omitted, it is assumed to be 1.
Page 5 of 48
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"}.
¶ 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.
Page 6 of 48
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.
¶ 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.
7. 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.
Page 7 of 48
8. If a1 is TRUE or omitted, ref_text is interpreted as an
A1-style reference.
9. If a1 is FALSE, ref_text is interpreted as an R1C1-style
reference.
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.
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
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:A50
0)))),INDIRECT("A2:A"&(MAX((A2:A500<>"")*ROW(A2:A500))))))>1,"Duplic
ates","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.
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:
Page 9 of 48
=IF(COUNTIF(Range1, A5)>1,TRUE,FALSE)
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.
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
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
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))
=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
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.
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.
Page 12 of 48
TEXT
=LEFT(A1,FIND(" ",A1,1))
This will return the word "First".
=RIGHT(A1,LEN(A1)-MAX(ROW(INDIRECT("1:"&LEN(A1)))
*(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)=" ")))
=RIGHT(A1,LEN(A1)-FIND(" ",A1,1))
This will return the words "Second Third Last"
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.
Page 13 of 48
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:
=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.
First create a define name called "Grades" which refers to the array:
={0,"F";60,"D";70,"C";80,"B";90,"A"}
=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
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
• 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)
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)
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)
Page 16 of 48
If you expect a number larger than 1E+306 (a one with 306 zeros) then change this
to a larger number.
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
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))
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))),COLUM
N(NumRange),4)
This function returns B2, the address of the first '1' in the 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)*(NumRa
nge<>""))),COLUMN(NumRange),4)
Page 18 of 48
This function returns B4, the address of the last '1' in the 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))),COLU
MN(NumRange),4)
This function returns B1, the address of the first '5' in the 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)*(NumRa
nge<>""))),COLUMN(NumRange),4)
This function returns B5, the address of the last '5' in the 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))
Page 19 of 48
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)
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
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))
=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.
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
=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
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.
The example formulas presented in this tip use the simple database table shown
below.
Page 23 of 48
Sum of Sales, where Month<>"Jan"
Another simple use of SUMIF (single criterion):
=SUMIF(A2:A10,"<>Jan",C2:C10)
Page 24 of 48
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))
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
=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.
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.
Open a new workbook and then open the Visual Basic Editor (Tools > Macro >
Visual Basic Editor or ALT+F11).
Page 26 of 48
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
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
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.
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)
Page 28 of 48
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...
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.
Page 29 of 48
but because the fuel comes in liters and there are 4.546 liters in a gallon..
MPG = (MILES THIS FILL - MILES LAST FILL) / LITRES OF FUEL x 4.546
EXAMPLES OF UDFS
Page 30 of 48
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!
Page 31 of 48
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 Day(Date) >= Day(DoB) Then If the today is equal to or past the day of
Age = Year(Date) - Year(DoB) their birthday, then they have had their
birthday (or it is today) so their age is this
year minus their birth year...
Page 32 of 48
Case Is > Month(DoB) If today's month is after (i.e. greater than)
Age = Year(Date) - Year(DoB) the month of the person's date of birth,
they have had their birthday, so their age
is this year minus their birth year.
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.
Page 33 of 48
InputDate + (7 - Weekday(InputDate))
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.
Page 34 of 48
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.
Page 35 of 48
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...
Page 36 of 48
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...
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
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
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))
Page 39 of 48
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
Require Entry in a Specific Column before allowing entry.
=NOT(TRIM(A1)="")
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
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
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
Page 43 of 48
3. Click the Options button, to expand the dialog box
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
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
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
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
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