Sample Pages: Excel / VBA For Business & Accounting
Sample Pages: Excel / VBA For Business & Accounting
s
Analytical tools for improved decision making
ge
pa
SECOND EDITION
e
pl
1 Introduction 1
Overview of book 1
The key to programming and the concept of assignment 1
s
Financial statement analysis (FSA) 5
Basic spreadsheet structure (separate data section) 5
ge
Cell referenced formulas (no hard coding) 6
Enabling macros 6
Changing macro security 7
Creating a FSA spreadsheet 7
pa
3 Cost Behaviour and VBA fundamentals 9
Cost behaviour 9
e
Opening the Visual Basic Editor (VBE) 10
The VBE Project Explorer 10
pl
4
m
Creating a subroutine 13
Declaration of variables 14
Assignment of values to variables 15
Displaying values with a message box (MsgBox) 16
Creating a cost behaviour subroutine 17
Creating a CVP subroutine 17
Introduction to the Input Box 17
Costing 27
Message boxes - extended 28
Input boxes - extended 30
Modularisation (break your overall model into subsidiary models) 32
7
Budgeting, Sensitivity Analysis & VBA loops 35
Budgeting 35
Sensitivity analysis 36
Do loops 36
For / Next loops 37
Count controlled loops 38
8
The Macro Recorder & Troubleshooting / Debugging 41
s
Overview of macros 41
ge
The macro recorder – basic use 41
Using the macro recorder to find VBA methods 44
Troubleshooting and debugging 44
pa
9
Statement of Financial Performance (Income Statement), Data Validation
& Filters / Validation & Reasonability Testing 47
10
Statement of Financial Performance (Balance Sheet), Logical functions
/ Presentation & Reporting / Documentation 55
Sa
References 61
Appendix 62
Index 66
s
ge
OVERVIEW OF BOOK
The Excel spreadsheet provides you with an incredibly powerful and flexible means for perform-
ing calculations and analysing data. Our intention with this book is to introduce you to some of
pa
Excel’s more useful features that will assist you with various financial and management accounting
techniques. And to give you a taste of programming with Excel VBA.
We assume that you have a basic familiarity with Excel, e.g. that you can open & save an
Excel workbook file, enter data and create simple formulas. We further presume that you’re like
e
most Excel users and have no VBA (Visual Basic for Applications) skills. No VBA knowledge is
OK for many spreadsheeters. You can do an incredible amount of useful work with Excel without
pl
using Visual Basic. And this book will help you to expand your Excel skills. More importantly
though it will introduce you to the world of VBA and the virtually endless scope of capabilities
that it provides.
m
The key to writing computer program code is to remember that while computers are incredibly
fast, they are not very smart. You must tell them exactly what to do, when to do it and how to do
it. Instructions must be extremely simple with no uncertainty. You cannot assume anything.
A critical first step in learning to program is understanding the language and vocabulary of
programmers. This is especially important when they use everyday words in a technical respect.
The terms “constants,” “variables,” and “assignment” have specific meanings in programming
beyond their meaning in general conversations. Your first task is to make sure that you understand
these terms and their ramifications for computer equations.
The basis for all computer languages (including Excel VBA) is that information is denoted
either directly as constants or symbolically as variables. Constants are values that stay the same (as
you might have guessed). In contrast, variables can take on different values. A constant (or more
correctly “the value of a constant”) can be assigned to a variable. However, a variable (or more
correctly “the value of a variable”) cannot be assigned to a constant.
Consider the following two VBA programs or subroutines. As for all VBA subroutines, they
begin with the word “Sub” followed by the subroutine name and “()”. The conclude with the words
“End Sub” and contain statements or instructions to be executed by the computer in order of
appearance. Each of the subs below uses the VBA message box function as a convenient way to
display the value of a variable.
s
ge
Sub CalcProfitA provides the calculation for profit as 30 and displays this value with its
final line of code. However, sub CalcProfitB has placed the profit calculation statement before
assigning a value to the variable Expense. This results in Profit being calculated with revenue of
pa
90 and null expenses resulting in a profit of 90. The assignment of 60 to Expense comes after the
Profit calculation and the program isn’t smart enough to realise it should be included. You knew
that Profit should have been 30 again but the CalcProfitB sub displays a profit of 90. It’s wrong
because your instructions weren’t clear.
It is useful to understand what the computer does when an assignment statement is executed.
e
The equals sign in the assignment statement can be thought of as meaning “is replaced by”
m
or “is assigned the result.” It is critical to recognize that this use is different from the conventional
mathematical meaning of equality.
A variable is the label that the computer associates with the memory location where it
Sa
stores a constant value. As shown below for the first three statements of sub CalcProfitA. The
first two statements (Revenue = 90 and Expense = 60) results in the VBA program creating two
storage locations in its memory, which it labels Revenue and Expense to which it stores the values
90 and 60. The third statement Profit = Revenue – Expense instructs the computer to subtract
the stored value of the variable Expense from the stored value of variable Revenue and assign the
result to the variable Profit.
Memory locations Program statements
Revenue
90 Revenue = 90
Expense
30 Expense = 30
Profit
Profit = Revenue - Expense
60
Assignment statements must always have a single variable on the left side of the equals sign.
Because they both violate this rule, the following legitimate algebraic equations would be invalid
as VBA statements:
On the other hand, although the following statement is not a proper algebraic equation, it
is a perfectly valid assignment statement:
After this statement was executed, the memory location for the variable x would have a value
that was one higher than its previous value.
Of course, computers also must process information other than numbers. For example,
they need to be able to handle names, labels, and identification numbers, which consist of letters,
numbers, and symbols. Such alphanumerical information is formally referred to as a string. In
s
VBA, string constants are enclosed in quotes. The variables in which they are stored are called
string variables. Here’s an example of the assignment of a string constant, “Wayne Gretzky”, to a
ge
string variable, n:
n = “Wayne Gretzky” pa
Executing this line of code causes a memory location called n to be set up containing the
string constant Wayne Gretzky.
e
Problems
pl
s
ge
Financial statement analysis (FSA) provides an excellent means for illustrating the importance of
proper spreadsheet structure and the appropriate formula use.
pa
FINANCIAL STATEMENT ANALYSIS (FSA)
Financial ratios are simply mathematical computations that provide comparisons of various income
e
statement and balance sheet accounts. They offer an excellent way to scrutinize the financial
health of a business.
pl
The first step with FSA is to determine your target users. The target audience dictates which
key indicators and relationships require examination. Different users will have diverse goals which
will determine which ratios they find useful. Managers will tend to have a general interest in all
m
ratios, as they have an overall responsibility for both the short-term and the long-term performance
of the business. On the other hand, short-term lenders (e.g. suppliers) will primarily be interested
in the likelihood that they will be able to collect their account. As such, their main interest would
Sa
• Inputs
• Processing (steps required to generate the desired outputs)
• Outputs
5
While spreadsheet models are sometimes criticised by traditional programmers for com-
bining the processing and output components (and thus confusing matters), the IPO model is
still a useful starting point.
Good data is critical for all spreadsheet models. However, expert modelers concentrate
their efforts on the structure of their model, not with the search for and analysis of data. Novice
modelers will frequently spend a lot of time searching for and analysing data. Better or more
accurate data doesn’t necessarily mean that the model’s outputs will be better. It is essential to
remember that modeling is about forecasting the future. Data is past tense (i.e. history) which
may be useful for predicting future events but maybe not. The modeler should spend most of
their time developing an appropriate model structure (e.g. accurate and logical formulas). And
less time pursuing that elusive “perfect” data.
s
derestimated as there is not an immediate error and there may never be one. The fact that many
models fail to apply this basic software design principle is almost certainly a major contributor to
ge
the incidence of spreadsheet errors and the lack of reusability of many models.
Powell and Baker (2004, page 97) provide an excellent explanation of why it is important to
isolate input data or parameters.
pa
“A common source of errors in spreadsheets is the tendency to bury parameters
in cell formulas and to replicate the same parameter in multiple cells. This makes
identifying parameters difficult, because they are not immediately visible. It’s
also difficult to know whether all numerical values of a parameter have been
changed each time an update is required. By contrast, the habit of using a single
e
The problem of hard coding of values into formulae has an equivalent in traditional pro-
m
gramming and VBA where a well-constructed program would rarely (never) enter a value
(a number) into a line of executable code. The recommended programming technique is to
assign the desired value to a named variable (e.g. RedFont = 3) and use this variable in the
Sa
program code. This can be contrasted to the inferior design that would simply type the value
3 into a line of code. The prevalence of such inferior design practices is caused by the practice
being quicker in the short run as the developer avoids having to create a variable and assign a
value to it. The resulting ‘magic number’ in the code makes programs more difficult to read,
understand, and maintain.
ENABLING MACROS
Excel’s macro security settings must be dealt with whenever your spreadsheet contains macros.
You will receive a SECURITY WARNING advising you to this regard on opening of the file.
Indicate your trust of the macro developer by choosing the option to Enable Content to allow
you to run the macros. You should not enable macros from unknown sources.
see the Developer tab you need to select the FILE tab and then chose OPTIONS. Select the
CUSTOMISE RIBBON option and in the Main Tabs section (right side of the screen) “tick”
the box beside DEVELOPER. Select OK and you should be right. That is, your ribbon should
now display the Developer tab.
s
referenced ratio formulas to the output section of the spreadsheet. Exhibits 2a and 2b below
provide extracts from the Chapter2.xlsm spreadsheet file. Your task is to enter proper cell refer-
ge
enced spreadsheet formula for each of the current year ratios cells on Exhibit 2b. To assist you,
this worksheet provides answers for each of the ratios for the previous year and details of the
formulas to be used.
The Chapter2.xlsm spreadsheet provides the “Check my work” button that accesses VBA
pa
code to assess your assignment attempt. The self-assessment macro verifies the accuracy of your
formula by checking that your ratio formulas provide the correct value with a separate set of
financial statement data. It does so by filling the ratio cells green if your formula is correct and
red if not. This is an example of the type of VBA code that you will learn how to create.
e
pl
m
Sa
s
ge
pa
Exhibit 2b. Financial statement ratios output section
e
pl
m
Sa