Handbook On Demographic Analysis Using Excel Application
Handbook On Demographic Analysis Using Excel Application
DEMOGRAPHIC
ANALYSIS USING
EXCEL APPLICATION
PREPARED BY
LAI SIOW LI
POPULATION STUDIES UNIT
FACULTY OF ECONOMICS AND ADMINISTRATION
UNIVERSITY OF MALAYA
2014
1
Table of Contents
Preface................................................................................................................................ iii
1. CHAPTER ONE ............................................................................................................. 1
Introduction to Excel Functions .......................................................................................... 1
1.1 Create chart with labels ............................................................................................. 1
1.2 HLOOKUP function ................................................................................................. 7
1.3 Form controls .......................................................................................................... 10
1.4 INDEX function ...................................................................................................... 13
1.5 Median age, MATCH and INDEX function, TREND function ............................. 15
1.5.1 Method 1: Conventional formula ..................................................................... 15
1.5.2 Method 2: Interpolation .................................................................................... 17
1.5.3 Method 3: TREND function ............................................................................. 23
1.6 Line graph on bar chart and ogive ........................................................................... 24
1.7 VLOOKUP function ............................................................................................... 28
2. CHAPTER TWO .......................................................................................................... 30
Life Tables ........................................................................................................................ 30
2.1 Construction of life table ......................................................................................... 30
2.1.1 Complete life table............................................................................................ 32
2.1.2 Abridged life table ............................................................................................ 34
2.2 MATCH and INDEX functions and life table ........................................................ 36
3. CHAPTER THREE ...................................................................................................... 40
Linear Regression Analysis .............................................................................................. 40
3.1 Linear regression model .......................................................................................... 40
3.1.1 Method 1: Excel data analysis tools ................................................................. 40
3.1.2 Method 2: INTERCEPT and SLOPE functions ............................................... 42
3.2 Closing life table by extrapolation of qx.................................................................. 43
4. CHAPTER FOUR ......................................................................................................... 48
Measures of Fertility and Stable Population ..................................................................... 48
4.1 Computation of fertility statistics ............................................................................ 48
4.2 Stable population ..................................................................................................... 55
i
5. CHAPTER FIVE .......................................................................................................... 58
Working with Large Datasets/Lists .................................................................................. 58
5.1 COUNT function ..................................................................................................... 58
5.2 COUNTA function .................................................................................................. 59
5.3 LARGE function ..................................................................................................... 60
5.4 SMALL function ..................................................................................................... 61
5.5 Sorting a list ............................................................................................................ 62
5.6 Using filters to analyze a list ................................................................................... 63
5.6.1 AutoFilter (for simple criteria) ......................................................................... 63
5.6.2 Advanced Filter (for complex criteria) ............................................................. 64
6. CHAPTER SIX ............................................................................................................. 68
Introduction to Visual Basic ............................................................................................. 68
6.1 Objects, properties, methods and events ................................................................. 68
6.2 Recording a macro .................................................................................................. 74
6.2.1 Absolute references .......................................................................................... 74
6.2.2 Relative references ........................................................................................... 77
6.3 Creating decision-making code ............................................................................... 80
6.4 OFFSET and CELLS properties ............................................................................. 84
6.4.1 OFFSET property ............................................................................................. 84
6.4.2 CELLS property ............................................................................................... 85
6.5 Loops ....................................................................................................................... 86
7. CHAPTER SEVEN ...................................................................................................... 96
Population Projections ...................................................................................................... 96
7.1 Female population projection .................................................................................. 98
7.2 Extracting data and recording macro .................................................................... 102
7.3 Pivot table.............................................................................................................. 106
7.4 Male population projection ................................................................................... 107
ii
Preface
This handbook is aimed at providing an introduction to the basic and advanced
knowledge on Excel spreadsheet techniques such as lookup functions, data management,
pivot tables, macros and Visual Basic command to analyze demographic data.
The contents of this handbook can also be used, to some extent, as a training guide for
introductory courses in the application of different Excel techniques in solving typical
tasks of demographic analysis.
Specifically, users of this handbook are expected to strengthen their basic analytical skills
on the following topics:
(a) Introduction to Excel, making charts and advanced use of functions (lookup
functions);
(b) Construction of abridged and complete life tables;
(c) Regression analysis. Using regression approach to close life table;
(d) Computation of fertility statistics and stable populations;
(e) Working with large datasets/lists;
(f) Introduction to Visual Basic and macro language in Excel; and
(g) Population projections and advanced use of macro technique.
Examples in this handbook are demonstrated using Microsoft Excel 2007. Data are
obtained from the United Nations Database.
Definitions of the functions in Excel are based on that of Microsoft Office. However, the
opinions and conclusions expressed in this handbook are solely those of the author and
should not be construed as representing the policy of any agency.
Regards,
Lai Siow Li
Population Studies Unit
Faculty of Economics and Administration
University of Malaya
iii
1. CHAPTER ONE
Introduction to Excel Functions
1. Copy age (also represents the row index number) and any year of data (e.g. year
2006) to a new cell.
Take note: For data with five year age group, row index number also starts with 0,
and then continues by 1, 2, 3, etc.
1
2. Put cursor on any of the cell (e.g. cell Q15). Press F11 to create the chart. Add in
axis titles.
3. To add in axis titles, go back to Sheet1, type in titles for "x-axis", "y-axis" and
"chart title". For the chart title, use syntax below:
="Population of "&A1&","&A2&","&Q6
Sign "&" is used to join the words together.
2
Go back to Chart1 Click on chart title, press "=" sign Go back to Sheet1
Click on cell V3, press "Enter" Repeat again for x-axis and y-axis title
3
4. Add in total population (in million) with 2 decimal places into chart title by using
TEXT function. The syntax for TEXT function:
=TEXT(value,format_text)
where
(a) value is a numeric value, a formula that evaluates to a numeric value, or a
reference to a cell containing a numeric value.
(b) format_text is the numeric format as a text string enclosed in quotation marks.
TEXT function converts a numeric value to text and allows users specify the
display formatting by using special format strings.
4
5
5. ISNUMBER function verifies whether a value is a number, and returns TRUE
(for number) or FALSE (for text). The syntax for ISNUMBER function:
=ISNUMBER(value)
6
1.2 HLOOKUP function
HLOOKUP function is used when comparison values are located in a row across the top
of a table of data, and you want to look down a specified number of rows. The syntax for
HLOOKUP function:
=HLOOKUP(lookup_value,table_array,row_index_num,range_lookup)
where
(a) lookup_value is the value/reference/text string to be found in the first row of the table.
(b) table_array is a table of information in which data is looked up. The values in the first
row of table_array can be text, numbers, or logical values.
(c) row_index_num is the row number in table_array from which the matching value
will be returned. A row_index_num of 1 returns the first row value in table_array, a
row_index_num of 2 returns the second row value in table_array, and so on.
(d) range_lookup is a logical value that specifies whether you want HLOOKUP to find
an exact match or an approximate match. If TRUE, an approximate match is returned;
if an exact match is not found, the next largest value that is less than lookup_value is
returned. If FALSE, HLOOKUP will find an exact match. If one is not found, the
error value #N/A is returned.
7
Task:
Search for any year, and then returns the population by age in the same column from the
year you specify by using HLOOKUP function. For example, you want to change year
2006 to year 2016, and the population by age change accordingly by using HLOOKUP
function.
Solution:
Steps:
1. In cell Q6, type in function =TEXT(2016,"0")
This is because the reference cell must follow the format of the table of data.
8
2. In cell Q7, type in function:
=HLOOKUP($Q$6,$C$6:$M$107,P7+2,FALSE)
Then, drag down.
Note: Dollar sign ($) is used to fix the cell position (absolute cell references) and is
created by pressing F4.
9
1.3 Form controls
With multiple years of data, you can add a list box control to a worksheet linking it to a
cell, and returns a numeric value for the current position of the selected item in the
control.
Steps:
1. Display Developer tab. Go to Office Button Excel Options Show Developer tab
in the Ribbon OK
10
2. Copy the first row of data (year), and paste (transpose option) in a new cell.
3. Go to Developer Insert Form Controls choose List Box. When the cursor
became "+" sign, make a box at any space.
11
4. Click on the list box, and then right click Format Control Input range: list of
items (e.g. AB6:AB16); Link cell: any blank cell (e.g. V10) OK.
When you click on any year in the list box, the blank cell in V10 will appear the number
of items/position that you select.
12
1.4 INDEX function
13
Task:
Data must change accordingly when you select any year in the list box (let say year 2010).
Solution:
In cell Q6, use INDEX function:
=INDEX(AB6:AB16,V10,1)
14
1.5 Median age, MATCH and INDEX function, TREND function
Steps:
1. Find cumulative population.
2. The conventional formula for computing the median age from grouped data:
N
fx
Md l Md ( 2 )i
f Md
where
N
(a) lMd = the lower limit of the class containing the middle, or th item
2
(b) N = the sum of all the frequencies
(c) f x = the sum of the frequencies in all the classes preceding the class
N
containing the th item
2
N
(d) fMd = frequency of the class containing the th item
2
N
(e) i = size of the class interval containing the th item
2
15
N
In this case, the th person falls in the age group 45-46. The solution is:
2
16
1.5.2 Method 2: Interpolation
Generally, cumulative frequency represents the sum of all previous frequencies up to the
current point, where the first cumulative frequency is the value of first frequency and the
last cumulative frequency is the total frequency. However, in this case, cumulative starts
at 0 because the position returned from the MATCH function (will be discussed later)
always start with 1.
Steps:
1. Find cumulative population from age 0 to age n+1. Cumulative population at age
n+1 is equivalent to the total population.
17
2. Find x (median population).
totalpopul ation
x
2
y
A 1
g y
e y
0
x0 x x1
Population
18
3. Use MATCH and INDEX functions to find y0, y1, x0 and x1.
MATCH function is used to search for a specified item in a range of cells, and then
returns the relative position of that item in the range. In this section, MATCH
function is used to provide a value for the row_num argument of the INDEX function.
= MATCH(lookup_value,lookup_array,match_type)
where
(a) lookup_value is the value that you want to match in lookup_array.
(b) lookup_array is the range of cells being searched.
(c) match_type is the argument used to specify how Excel matches lookup_value with
values in lookup_array. The default value for this argument is 1 (TRUE). In this case,
you must use 1 or TRUE because you cannot find the exact match for the median
position and have to look for the position of the next largest value that is less than the
lookup_value returned. Details on match type and its behavior is shown below:
Match_type Behavior
19
20
21
y1 y 0
Median age: y y 0 ( x x0 )
x1 x0
22
1.5.3 Method 3: TREND function
Based on x and y-values computed in Method 2, use TREND function to find median age.
TREND function returns y-values along a linear trend line (based on the method of least
squares):
=TREND(known_y's,known_x's,new_x's,const)
where
(a) known_y's is the set of y-values that already know in the relationship y = a+ bx.
(b) known_x's is an optional set of x-values that already know in the relationship y = a+
bx.
(c) new_x's are new x-values for which you want TREND to return corresponding y-
values.
(d) const is a logical value specifying whether to force the constant b to equal 0. If const
is TRUE or omitted, b is calculated normally (as used in our case). If const is FALSE,
b is set equal to 0 (zero), and the m-values are adjusted so that y = mx.
23
1.6 Line graph on bar chart and ogive
Line graph can be plotted on bar chart by creating a secondary-axis. This type of graph
shows two y-axis along with a x-axis and thus more information can be presented on a
graph.
An ogive is a cumulative frequency or percentage graph that shows the number of data
values lie above or below a particular value in a data.
Task:
Plot one graph that shows the percent and cumulative percent of population.
Solution:
Steps:
1. Find percent and cumulative percent of population.
24
2. Go to Insert Column Charts 2-D Column Clustered column
3. Click on any cumulative % bar (red bar) Right click Format Data Series Series
Options Secondary axis
25
4. Click on any cumulative % bar (red bar) Go to Insert Line Charts 2-D Line
Line
26
5. Go to Layout Axis Titles
27
1.7 VLOOKUP function
VLOOKUP function is used instead of HLOOKUP when comparison values are located
in a column to the left of the data that you want to find:
=VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
where
(a) lookup_value is the value to search in the first column of the table array.
(b) table_array is a table of information in which data is looked up. The values in the first
column of table_array can be text, numbers, or logical values.
(c) col_index_num is the column number in table_array from which the matching value
must be returned. A col_index_num of 1 returns the first column value in table_array,
a col_index_num of 2 returns the second column value in table_array, and so on.
(d) range_lookup is a logical value that specifies whether you want VLOOKUP to find
an exact match or an approximate match. If TRUE or omitted, an exact or
approximate match is returned; if an exact match is not found, the next largest value
that is less than lookup_value is returned. If FALSE, VLOOKUP will only find an
exact match. If exact match is not found, the error value #N/A is returned.
28
Task:
Search for any age group, and then returns the population by time (year) in the same row
from the age group you specify by using VLOOKUP function. For example, you want to
change age group 5 (5-9) to 10 (10-14), and the population by year change accordingly
by using VLOOKUP function.
Solution:
Steps:
1. In column P, assign column index number for the data.
2. In cell R6, type in function:
=VLOOKUP($R$5,$B$6:$M$27,P6+2,FALSE)
Then, drag down.
29
2. CHAPTER TWO
Life Tables
The course of mortality throughout the life cycle may be described by a life table. A life
table consists of several life table functions, all of which are mathematically related and
may be generally derived when the value of one of them is known. There are two types of
life tables:
1. Calendar year or period life table. This type of life table is one in which the
mortality rates used relate to a specified time interval and the cohort is therefore
hypothetical. It gives a cross-sectional view of the mortality and survival experience
of a population during a current year. It is entirely dependent on the age-specific
death rates prevailing in the year for which it is constructed.
2. Generation or cohort life table. This type of life table traces the experience of an
actual birth cohort and the mortality rates contained in the table are then spread over a
prolonged period, usually about 100 years.
The conventional life table conceptually traces a cohort of newborn babies through their
entire life under the assumption that they are subject to the current observed schedule of
age-specific mortality rates (The John Hopkins University and Stan Becker, 2008). As
age increases, the number of survivors of the original group declines.
This section explains the construction of complete and abridged life tables. A complete
life table is usually one in which the values of the life table functions are given in single
years of age. An abridged life table is one in which most functions are given only for
certain pivotal ages, frequently spaced at five or ten year intervals after infancy;
intermediate values for the functions are usually obtained by dome form of interpolation.
Suppose that of li individuals alive at exact age xi, di die between age xi and xi+1, and each
of di individuals live on average a fraction ai of the interval (xi, xi+1).
30
Life table major columns:
1. x = Exact age x
2. n = Width of interval
3. lx = Number of people left alive at age x and x + n
4. ndx = Number of people dying between ages x and x + n
5. nqx = Probability of dying between ages x and x + n. Note that the probability of
dying for the last age group is equal to 1.
6. npx = Probability of surviving from age x to age x + n
7. nLx = Person-years lived between ages x and x + n
8. Tx = Person-years lived above age x
9. ex = Expectation of life at age x
10. nMx = Age-specific death rate between ages x and x + n
11. nax = Average person-years lived between ages x and x + n for persons dying in the
interval (proportional distribution of deaths in interval)
31
2.1.1 Complete life table
A complete life table is usually one in which the values of the life table functions are
given in single years of age.
Dx
1. M x
Px
OR
dx
Mx
(l x d x ) a x d x
where
Dx is the number of deaths in the population at age x
Px is the number of person-years lived in the population at age x
Mx
2. q x
1 (1 a x ) M x
OR
dx
qx
lx
Set qx at 1 for the last age group.
3. d x l x q x , x = 0,1,…,w
OR
d x l x l x 1
4. l x1 l x d x , x = 0,1,…,w-1
OR
l x 1 (1 q x )l x
5. Lx (l x d x ) a x d x , x = 0,1,…,w-1
OR
dx
Lx
Mx
lw
Lw
Mw
32
6. Tx Lx Tx 1 , x = 0,1,…,w
Tw Lw
Tx
7. e x , x = 0,1,…,w
lx
33
2.1.2 Abridged life table
An abridged life table is one in which most functions are given only for certain pivotal
ages, frequently spaced at five or ten year intervals after infancy; intermediate values for
the functions are usually obtained by some form of interpolation.
Dx
1. M x
Px
OR
dx
Mx
n x (l x d x ) a x n x d x
where
Dx is the number of deaths in the population at age x
Px is the number of person-years lived in the population at age x
nx M x
2. q x
1 (1 a x )n x M x
OR
dx
qx
lx
Set qx at 1 for the last age group.
3. d x l x q x , x = 0,1,…,w
OR
d x l x l x 1
4. l x1 l x d x , x = 0,1,…,w-1
OR
l x 1 (1 q x )l x
5. Lx n x (l x d x ) n x a x d x , x = 0,1,…,w-1
OR
dx
Lx
Mx
lw
Lw
Mw
34
6. Tx Lx Tx 1 , x = 0,1,…,w
Tw Lw
Tx
7. e x , x = 0,1,…,w
lx
35
2.2 MATCH and INDEX functions and life table
MATCH and INDEX functions were introduced earlier to compute median age. In this
section, these functions will be used to search for a specific year of data in a range of
cells along with its life table. Refer earlier sections for the definition of MATCH and
INDEX functions.
Task:
Given female age-specific mortality rate (ASMR) between 1955 and 1994, construct life
table for each year of data.
Solution:
Steps:
1. Construct life table for year 1955.
36
2. Find Match Position by using function =MATCH(lookup_value, lookup_array,
match_type)
where
(a) the lookup_value is the reference year.
(b) the lookup_array is the range of year cells.
(c) the match_type is 0 (represents exact match).
37
3. To let Mx change accordingly to different year, use function =INDEX(array,
row_num, column_num) for column Mx.
where
(a) the array is the range of ASMR cells.
(b) row_num is the row in array from which to return a value (in this case, it refers to the
Match Position + row number).
38
4. Insert form controls so that life table changes accordingly to different year of data.
39
3. CHAPTER THREE
Linear Regression Analysis
Linear regression analysis is introduced to close life table.
Linear regression model is used to describe the relationship between a dependent variable
(y) and one or more independent variables (as denoted by x).
Steps:
1. To display Analysis tab, go to Office Button Excel Options Add-Ins
Manage: Excel Add-Ins Go Choose Analysis ToolPak OK
40
2. Go to Data Data Analysis Regression OK.
Select range of dependent data for Input Y Range, range of independent data for
Input X Range, and any blank cell for Output Range.
41
3.1.2 Method 2: INTERCEPT and SLOPE functions
42
3.2 Closing life table by extrapolation of qx
When the last age group of life table is an open-ended age group, you may calculate the
last open-ended person-years lived inaccurately. You need to close life table by fitting
q
function n x through the last, for example, six values available. Additional columns
1 n q x
were computed based on the following formulas:
q ( x)
1. y ( x) ln[ ] , where x = 45, …, 70 (last six values available)
1 q ( x)
2. y(x) = a + bx
q ( x) e ( abx)
3. e ( abx) , so q( x)
1 q ( x) 1 e ( abx)
43
Task:
Given Dx, Px and ax, construct a life table and extent the life table until the number of
people left alive (lx) is less than 1.
Solution:
Steps:
1. By using formula, find Mx and Qx. The formula for Qx is the same as qx.
q ( x)
2. Compute a new column named logit q by using formula y ( x) ln[ ] for the
1 q ( x)
last six age groups.
44
3. Let say you would like to extent life table until age group 120-124. Find fitted logit
by using formula y(x) = a + bx. Intercept and slope were obtained by fitting logit(q)
model.
45
e ( abx)
4. Compute back transformation column by using formula q( x) .
1 e ( abx)
5. The final qx is obtained from Qx for age group up to 70-74, and the remaining is
obtained from back transformation for age group 75-79 onwards.
46
6. By using the newly computed qx, find lx, dx, Lx, Tx and ex.
Take note: You can close life table at age group 115-119 as the value of lx is less than 1
after this age group.
47
4. CHAPTER FOUR
Measures of Fertility and Stable Population
where
(a) nBx[0,T] = births that occurred to women at ages x to x + n during the period 0 to T
(b) nPx[0,T] = person-years lived during the period 0 to T by women at ages x to x + n
F
nFx are often denoted nmx (if there is no confusion with ASMR) and called
maternity rates.
GRR represents the number of female births an average woman would have if she
lived through the end of her reproductive ages. It is a gross measure, because
mortality is not taken into account.
48
4. Net Reproduction Rate (NRR)
NRR is similar to GRR except that it takes mortality into account. NRR can be
interpreted as the average number of daughters that female members of a birth cohort
would bear during their reproductive life span if they were subject to the observed
age-specific maternity rates nFxF and mortality rates throughout their lifetimes.
x l0
n LFx
where represents the number of years lived in each age interval by an average
l0
female subjected to the period mortality conditions through her life span.
OR
(b) NRR ≈ p(AM)*GRR
where p(AM) is the probability of surviving to the mean age of the maternity function.
Since the age-specific maternity rates are the product of age-specific fertility rates
and of the ratio of female to total birth, we have the following approximation:
(1 SRB )
TFR (1 SRB ) * GRR NRR
p( AM )
where SRB is the ratio of male to female births.
Fertility rates that correspond to a NRR of 1 are often referred to as replacement level
fertility.
1 1 SRB
The replacement level of fertility corresponds to a GRR and a TFR
p( AM ) p( AM )
if NRR = 1.
The replacement level of TFR is usually about 2.1 for most developed countries. In
populations with high infant and child mortality, replacement level of TFR can be much
higher than 2.1.
49
If NRR > 1, each next generation is larger than the previous one and such population
increases across generations.
If NRR < 1, each next generation is smaller than the previous one and such population
declines across generations.
n FxF * ( x n / 2)
x
n
x
n FxF
m n F
n Lx
x
F
n Fx *
l0
n B x * ( x n / 2)
M x
n
x
n Bx
where
(a) x represents exact age
(b) n represents width of age interval
(c) nBx represents the distribution of births in the population by age of mother.
Summary:
1. TFR = ∑ ASFR
1
2. GRR = TFR*
1 SRB
F
n Lx 1
3. NRR = ∑ ASFR* *
l 0 1 SRB
50
Task:
Given age-specific mortality rate (Mx) and age-specific fertility rate (Fx), find:
(a) Total Fertility Rate (TFR)
(b) The mean of the age-specific fertility distribution - µ
(c) Mean age at childbearing - m
(d) Gross Reproduction Rate (GRR)
(e) Net Reproduction Rate (NRR)
Sex ratio at birth (SRB) is estimated at 1,060 male births per 1,000 female births and
fertility replacement level is set at 2.06.
Solution:
Steps:
1. By using functions used to construct life table, find ax, qx, lx, dx and Lx.
*Data provided until age 60 only. The last cell of ax, qx and Lx is not required to
follow standard rules because it is not the last age group.
51
2. Construct extra columns and find the summation of each column to facilitate the
calculation of fertility statistics.
52
(b) The mean of the age-specific fertility distribution - µ
53
(d) Gross Reproduction Rate (GRR)
54
4.2 Stable population
In stationary population, Crude Birth Rate (CBR) = Crude Death Rate (CDR) and thus
Crude Growth Rate (CGR) = CBR - CDR = 0.
In a stable non-stationary population, CBR (or Intrinsic Birth Rate, IBR) and CDR (or
Intrinsic Death Rate, IDR) are not equal.
55
Task:
Find stable population based on NRR above and plot a bar chart.
Solution:
Steps:
1. Find r, yx and Gx.
56
2. Find stable population in proportion so that it can be interpreted as the proportion
contributed by each age group to the total stable population.
57
5. CHAPTER FIVE
Working with Large Datasets/Lists
A list is a series of worksheet rows that contain related data, such as population by age
database or a set of names and phone numbers. A list can be used as a database, in which
rows are records and columns are fields. The first row of the list has labels for the
columns.
The COUNT function is used to count the number of cells that contain numbers, and
count numbers within the list of arguments. The COUNT function syntax is:
=COUNT(value1,[value2],…)
where
(a) value1 (required) is the first range within which you want to count numbers.
(b) value2 (optional) is the additional ranges within which you want to count numbers, up
to a maximum of 255 ranges.
Examples:
Take note: COUNT function can only count cells with numbers.
58
5.2 COUNTA function
The COUNTA function is used to count the number of cells that are not empty in a range
(including number and string cells). The COUNTA function syntax is:
=COUNTA(value1, [value2], …)
where
(a) value1 (required) is the first argument representing the values that you want to count.
(b) value2 (optional) is the additional arguments representing the values that you want to
count, up to a maximum of 255 arguments.
Example:
59
5.3 LARGE function
LARGE function is used to return the k-th largest value in a data set. The LARGE
function syntax is:
=LARGE(array,k)
where
(a) array is the range of data for which you want to determine the k-th largest value.
(b) k is the position (from the largest) in the array of data to return. For example, 1
represents the largest while 2 represents the second largest.
Example:
60
5.4 SMALL function
SMALL function is used to return the k-th smallest value in a data set. The SMALL
function syntax is:
=SMALL(array,k)
where
(c) array is the range of data for which you want to determine the k-th smallest value.
(d) k is the position (from the smallest) in the array of data to return. For example, 1
represents the smallest while 2 represents the second smallest.
Example:
61
5.5 Sorting a list
To sort a list, select one cell from the list. Then, choose the Sort command from the Data
menu. Excel examines your data to determine the extent of the list, determines whether
the list includes a heading row that should not be sorted, and then displays the Sort dialog
box.
62
5.6 Using filters to analyze a list
To filter a list means to hide all the rows except those that meet specified criteria. Excel
provides two filtering commands.
To use this command, first select any cell in your list. Then, choose AutoFilter from the
Filter submenu of the Data menu; Excel displays drop-down arrows next to each of the
column headings in your list. Clicking the arrow next to any heading reveals a list of that
heading's unique values, which you can use to specify filtering criteria.
To remove an AutoFilter for a particular column, open the column's drop-down list and
select (All). To remove all AutoFilters currently in effect, from the Data menu, choose
Filter and then choose Clear. To remove all AutoFilters and the drop-down arrows,
choose the AutoFilter command again thereby removing the check mark next to its name
on the Filter submenu of the Data menu.
At times you might want to copy, or extract, the rows that meet AutoFilter criteria to
some other part of your worksheet or to another worksheet altogether. Like any other
type of worksheet data, auto filtered rows can be copied with the Copy and Paste
commands on the Edit menu.
63
5.6.2 Advanced Filter (for complex criteria)
This command is useful if you want to extract selected filtered rows in a single step,
without having to use the Copy and Paste commands. Advanced Filter command will not
automatically copy selected rows to a new file or to a new worksheet within the current
workbook. To do these tasks, you must still use the Copy and Paste commands.
Criteria range is a cell range that contains a set of search conditions you can use with the
Advanced Filter command (Data menu, Filter submenu) to filter data in a list. A criteria
range consists of one row of criteria labels and at least one row that defines the search
conditions.
Steps:
1. Copy the column labels from the list for the columns that contain the values you want
to filter.
2. Paste the column labels in the first blank row of the criteria range.
3. In the rows below the criteria labels, type the criteria you want to match (e.g.
countries with TFR<1 between 2000 and 2005). Make sure there is at least one blank
row between the criteria values and the list.
64
4. Select any cell in your list.
5. On the Data menu, point to Filter, and then click Advanced Filter.
6. To filter the list by hiding rows that don't match your criteria, click Filter the list, in-
place. To filter the list by copying rows that match your criteria to another area of the
worksheet, click Copy to another location, click in the Copy to box, and then click the
upper-left corner of the paste area.
7. In the Criteria range box, enter the reference for the criteria range, including the
criteria labels.
65
Take note:
(a) Criteria on the same line are considered to be joined by AND.
66
(b) Criteria on separate lines are considered to be joined by OR.
(c) The column heading above a computed criterion must not be a copy of a column
heading in the list. The criteria-range heading can be blank, or it can contain anything
else you like (Note that this requirement is the opposite of what non-computed
criteria require).
(d) References to cells outside the list should be absolute.
(e) References to cells within the list should be relative.
67
6. CHAPTER SIX
Introduction to Visual Basic
1. Object
An object represents an element of an application, such as a worksheet, a cell or a
chart. In Visual Basic code, you must identify an object before you can apply one of
the object's methods or change the value of one of its properties.
2. Collection of objects
A collection is an object that contains several other objects, usually, but not always,
of the same type. In Microsoft Excel, for example, the Workbooks object contains all
the open Workbook objects.
3. Method
A method is an action that an object can perform. You can also manipulate an entire
collection of objects if the objects share common methods. For example, all open
workbooks may be closed with Close method.
4. Property
A property is an attribute of an object that defines one of the object's characteristics,
such as size, color, or screen location, or an aspect of its behavior, such as whether it
is enabled or visible. To change the characteristics of an object, you change the values
of its properties. You can retrieve information about an object by returning the value
of one of its properties.
5. Event
An event is an action recognized by an object, such as clicking the mouse or pressing
a key, and for which you can write code to respond. Events can occur as a result of a
user action or program code, or they can be triggered by the system.
68
7. Modules
In VB, all code is contained in modules.
8. Components of Modules
Modules are comprised of declarations and procedures. Procedures consist of
subroutines and functions.
9. Declarations
Provide VB information about options as well as variables that you want to be
accessible throughout the program.
10. Procedure
A named sequence of statements executed as a unit is called procedure. For example,
Function and Sub are type of procedures. A procedure name is always defined at
module level. All executable code must be contained in a procedure. Procedures can't
be nested within other procedures.
(a) Subroutines
All programs (models) contain at least one subroutine. A subroutine is simply a block
of code that does something. You call a Sub procedure using the procedure name
followed by the argument list.
69
(b) Functions
A function is a special type of subroutine that returns a value. Like a Sub procedure, a
Function procedure is a separate procedure that can take arguments, perform a series
of statements, and change the values of its arguments. However, unlike a Sub
procedure, you can use a Function procedure on the right side of an expression in the
same way you use any intrinsic function, such as Sqr, Cos, or Exp, when you want to
use the value returned by the function.
*Commands saved in sheets can only be used in the particular worksheet. Commands
saved in modules can be used in any active worksheets.
* To run VB code, press Run Run Sub/UserForm.
70
Examples
(a) Close workbook
Items in a collection can be identified by number or by name. For example, in the
following procedure, Workbooks(1) identifies the first open Workbook object.
Sub CloseFirst()
Workbooks(1).Close
End Sub
71
(b) Name a workbook
To set the value of a property, follow the reference to an object with a period, the
property name, an equal sign (=), and the new property value. For example, the
following procedure changes the caption of a Excel window by setting the Caption
property.
Sub ChangeName()
Windows(1).Caption = "NewName"
End Sub
72
(c) Return the name of the currently active workbook
You can retrieve information about an object by returning the value of one of its
properties. The following procedure uses a message box to display the title that
appears at the top of the currently active window.
73
6.2 Recording a macro
Macro is introduced to minimize the repetitive events/tasks. Besides that, user interface
created through macro act as a screen where user can give inputs with ease and enables
input validations.
When you record a macro, Excel stores information about each step you take as you
perform a series of commands. Then, macro is run to repeat the commands. If you make
mistake when you record the macro, corrections you make are also recorded. Visual
Basic stores each macro in a new module attached to a workbook. There are 2 options for
recording a macro:
By default, recorded macro uses absolute references, which means that exact cell
locations are recorded into the macro. The macro works only with the exact cells
addresses you use when you record the macro.
Task:
Copy text "Absolute" from Cell E1 to E3 and record a macro so that this action can be
applied on new worksheets.
Solution:
Steps:
1. Go to Developer Record Macro In the Macro name box, type Absolute, assign a
Shortcut key Ctrl+a, and then click OK.
74
2. Type "Absolute" in Cell E1 and then click Enter. Next, copy and paste in Cell E3.
4. Open Visual Basic Editor to view the contents of the macro recorded.
75
5. If you want to repeat this action in new worksheet, go to Macros Choose the Macro
Name you saved earlier Run
Alternatively, press Ctrl+a
*Take note: The action will take place on cell location when you started the macro.
76
6.2.2 Relative references
The actions recorded in the macro are relative to the starting cell location.
Task:
Copy text "Relative" from Cell E1 to E5 and record a macro so that this action can be
applied on new worksheets.
Solution:
Steps:
1. Go to Developer Use Relative References Record Macro In the Macro name
box, type Relative, assign a Shortcut key Ctrl+r, and then click OK.
77
2. Type "Relative" in Cell E1 and then click Enter. Next, copy and paste in Cell E5.
4. Open Visual Basic Editor to view the contents of the macro recorded.
78
5. If you want to repeat this action in another new sheet, go to Macros Choose the
Macro Name you saved earlier Run
Alternatively, press Ctrl+r
*Take note: The action will take place on active cell location.
** The recorded macros can be edited and use for other tasks.
79
6.3 Creating decision-making code
Examples
1. If a number in Cell A1 is greater than 100 (e.g. 150), it will return as 100 in Cell A2.
Sub OneStatement()
myNumb = Range("a1").Value
If myNumb > 100 Then myNumb = 100
Range("a2").Value = myNumb
End Sub
80
2. If the number in Cell A1 is equal to 0, then it will be bold. Otherwise, it will be italic.
Sub SeveralStatementsElse()
If Range("a1").Value = 0 Then
Range("a1").Font.Bold = True
Range("a1").Font.Italic = False
Else
Range("a1").Font.Bold = False
Range("a1").Font.Italic = True
End If
End Sub
81
3. Given basic salary RM2,000, calculate bonus based on different performance
(number of cars sold).
Steps:
(a) Type in salary and performance.
Sub Bonus1()
salary = Range("d1").Value
performance = Range("d3").Value
Select Case performance
Case 1
Bonus = salary * 0.1
Case 2, 3
Bonus = salary * 0.3
Case 4 To 6
Bonus = salary * 0.5
Case Is >= 8
Bonus = salary * 2
Case Else
Bonus = 0
End Select
Range("d5").Value = Bonus
End Sub
82
(c) Assign macro to a button form control so that bonus will change accordingly to
different performance.
Go to Developer Insert Button (Form Control) Right click the button Assign
macro Select Bonus1 OK
83
6.4 OFFSET and CELLS properties
OFFSET property returns a Range object that represents a cell or range of cells that offset
from the specified cell or range of cells. Syntax for OFFSET:
OFFSET(rows, columns)
where
(a) rows is the number of rows, up (negative) or down (positive), that you want the
upper-left cell to refer to.
(b) columns is the number of columns, to the left (negative) or right (positive), that you
want the upper-left cell of the result to refer to.
Task:
Create a number (e.g.8) in a cell subjected to OFFSET criteria (3 rows down, 5 columns
to the right, reference cell: A1).
Solution:
Sub test1()
Range("a1").Offset(3, 5).Value = 8
End Sub
84
6.4.2 CELLS property
CELLS property returns a Range object that represents the table cells in a column, row,
selection or range. Syntax for CELLS:
CELLS (rows, columns)
where
(a) rows is the number of rows (including the row of reference cell), up (negative) or
down (positive), that you want the upper-left cell to refer to.
(b) columns is the number of columns (including the column of reference cell), to the left
(negative) or right (positive), that you want the upper-left cell of the result to refer to.
Task:
Create a number (e.g.8) in a cell subjected to CELLS criteria (3 rows down, 5 columns to
the right, reference cell: A1).
Solution:
Sub test2()
Range("a1").Cells(3, 5).Value = 8
End Sub
*Take note: The Range object generated by CELLS consists of one cell only. The Range
object referred to by the OFFSET property of a range has the same number of rows and
columns as the original range.
85
6.5 Loops
Sub test3()
For i = 0 To 10
Range("a1").Offset(0, i).Value = i
Next i
End Sub
86
2. Create ascending order from 0 to 10 in Column 1, starting from Cell A1.
Sub test4()
For i = 0 To 10
Range("a1").Cells(i + 1, 1).Value = i
Next i
End Sub
3. Create ascending order in diagonal form from 0 to 10, starting from Cell A1.
Sub test5()
For i = 0 To 10
Range("a1").Offset(i, i).Value = i
Next i
End Sub
87
4. Create descending order from 10 to 0 in Row 1, starting from Cell A1.
Sub test6()
a = 10
For i = 0 To 10
Range("a1").Offset(0, i).Value = a - i
Next i
End Sub
88
Examples on OFFSET and CELLS and looping
1. Find median age for each year (OFFSET property).
Steps:
(a) Count number of years using COUNTA function
89
(c) Transpose the data by using TRANSPOSE function
TRANSPOSE function returns a vertical range of cells as a horizontal range, or vice
versa. TRANSPOSE function syntax is:
=TRANSPOSE(array)
where the array is the range of cells that you want to transpose.
The formula must be entered as an array formula for the TRANSPOSE function to
work as expected. After transposing the first cell, select the exact range (that will fit
in the transposed data), starting with the formula cell. Press F2, and then press
CTRL+SHIFT+ENTER to transpose the entire range of cells.
90
*Press F2.
91
(e) Loop the median age for all years using VB code.
Sub MedAge()
For i = 1 To 380
Range("a2:x2").Offset(i - 1, 0).Copy Destination:=Range("ad10")
Range("y2").Offset(i - 1, 0) = Range("ai22").Value
Next i
End Sub
92
2. Find life expectancy at age 0 and 65 for each year (CELLS property).
Steps:
(a) By using MATCH and INDEX function, construct a life table for any year (e.g. year
1899).
*Life table will change accordingly when you change to different year.
93
(b) Decide output range. Create three columns: Year, life expectancy at age 0 (e0) and life
expectancy at age 65 (e65).
94
(c) To generate the output for these three columns, use VB code.
Sub lt()
i1 = 1
For i = 1899 To 2001
Range("j2").Value = i
Range("u4").Cells(i1, 1).Value = Range("j2").Value
Range("u4").Cells(i1, 2).Value = Range("p4").Value
Range("u4").Cells(i1, 3).Value = Range("p69").Value
i1 = i1 + 1
Next i
End Sub
95
7. CHAPTER SEVEN
Population Projections
Population size in a distant future affects the behavior of many different socio-economic
and environment systems, such as social security system, food production and
distribution, water supply (at least local), energy consumption and production, pollution
of environment, healthcare system and many others.
Forecasts
A population forecast is a projection in which the assumptions are considered to yield a
realistic picture of the probable future development of a population.
Projections
Population projection is probably the demographic technique that is most frequently
requested by demography's "clients".
Projections are also made in terms of educational, economic and social characteristics.
Backward projections are used to trace the past evolution of the population.
The most frequently used method of projection is the component method or cohort-
component method which takes the population distributed by age and sex at a base date
and carries it forward in time, cohort by cohort, on the basis of separate allowances for
fertility, mortality and migration.
96
Formulas used for population projection (component method)
Assume 5-year age group and female population.
where
5SX is the ratio at which life table population survives 5 years of the projection period
5 Lx 5
5 Sx
5 Lx
4. The total number of expected births (B) during the five-year projected
50 50
5 5
B [ ( 5 P t x 5 Px ) Fx ] [ ( 5 P t x 5 P t x5 5 S x5 ) Fx ]
t 5
15 2 15 2
5. The estimation of the number of all those born during the projection period who
survive to its end
t5 L
5 P0 Bf 5 0
5 l0
97
7.1 Female population projection
Task:
Based on 2006 data, project female population until 2050.
Sex ratio at birth (SRB) is estimated at 1,060 male births per 1,000 female births.
Solution:
Steps:
1. Compute ax, qx, lx, dx, Lx and Tx.
Lx1
2. Compute Sx based on formula: 1 S x 1
1 Lx
98
3. Calculate population for 2007.
15 2
In Excel, B can be calculated by SUMPRODUCT function. The syntax is:
=SUMPRODUCT(array1, array 2, array 3, …)
where the arrays represent 2 to 30 arrays whose components you want to multiply and
then add.
99
4. Use VB code to project population.
Firstly, count the number of rows as this information will be used in VB code.
100
VB code:
Sub proj()
i2 = 0
For i = 2006 To 2050
Range("s9").Value = i
Range("p10:s120").Offset(i2, 10).Value = Range("p10:s120").Value
i2 = i2 + 111
Range("s10:s120").Value = Range("t10:t120").Value
Next i
End Sub
Before:
101
7.2 Extracting data and recording macro
After you obtained projected population, you can extract data that you want (e.g. specific
years of population, specific age, etc), and record the extracting process in a macro.
Steps:
1. Type in criteria (e.g. you want population between 2010 and 2020 for female aged 30)
2. Go to Developer Record Macro In the Macro name box, type extract, and then
click OK.
102
3. Go to Data Advanced Filter
4. Stop recording.
103
5. Insert Button list control and link it to the recorded macro.
104
6. Extracting data will change accordingly to the different criteria that you set by
pressing button form control.
105
7.3 Pivot table
It is easier to manage the projected population in pivot table form. To insert pivot table,
go to Insert Pivot table Drag in the fields to the desire locations.
106
7.4 Male population projection
Male population can also be projected by using B for females (because males don't
expect any births). Proportion of male births = 1 - proportion of female births
107