Free Essay

Vba Intro

In:

Submitted By jonander
Words 10883
Pages 44
An Introduction to VBA in Excel
Robert L. McDonald† First draft: November, 1995 November 3, 2000



Abstract This is a tutorial showing how to use the macro facility in Microsoft Office—Visual Basic for Applications—to simplify analytical tasks in Excel.

Contents
1 Introduction 2 Calculations without VBA 3 How to Learn VBA 4 Calculations with VBA 4.1 Creating a simple function . . . . . . . . . . . . 4.2 A Simple Example of a Subroutine . . . . . . . 4.3 Creating a Button to Invoke a Subroutine . . . 4.4 Functions can call functions . . . . . . . . . . . 4.5 Illegal Function Names . . . . . . . . . . . . . . 4.6 Differences Between Functions and Subroutines 3 3 4 5 5 7 7 8 9 9

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

∗ Copyright c 1995-2000 Robert L. McDonald. Thanks to Jim Dana for asking stimulating questions about VBA. † Finance Dept, Kellogg School, Northwestern University, 2001 Sheridan Rd., Evanston, IL 60208, tel: 847-491-8344, fax: 847-491-5719, E-mail: r-mcdonald@northwestern.edu.

CONTENTS

2

5 Storing and Retrieving Variables in a Worksheet 5.1 Using a named range to read and write numbers from spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Reading and Writing to Cells Which are not Named. . . 5.3 Using the “Cells” Function to Read and Write to Cells.

10 the . . . . . . . . . 11 12 13

6 Using Excel Functions 13 6.1 Using VBA to compute the Black-Scholes formula . . . . . . 13 6.2 The Object Browser . . . . . . . . . . . . . . . . . . . . . . . 15 7 Checking for Conditions 16

8 Arrays 17 8.1 Defining Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 18 9 Iterating 19 9.1 A simple for loop . . . . . . . . . . . . . . . . . . . . . . . . . 20 9.2 Creating a binomial tree . . . . . . . . . . . . . . . . . . . . . 20 9.3 Other kinds of loops . . . . . . . . . . . . . . . . . . . . . . . 22 10 Reading and Writing Arrays 10.1 Arrays as Output . . . . . . . . . 10.2 Arrays as Inputs . . . . . . . . . 10.2.1 The Array as a Collection 10.2.2 The Array as an Array . . 22 23 24 24 25 26 26 27 27 28 28

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

11 Miscellany 11.1 Getting Excel to generate your macros 11.2 Using multiple modules . . . . . . . . 11.3 Recalculation speed . . . . . . . . . . 11.4 Debugging . . . . . . . . . . . . . . . . 11.5 Creating an Add-in . . . . . . . . . . .

for . . . . . . . .

you . . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

12 A Simulation Example 29 12.1 What is the algorithm? . . . . . . . . . . . . . . . . . . . . . 29 12.2 VBA code for this example. . . . . . . . . . . . . . . . . . . . 30 12.3 A trick to speed up the calculations . . . . . . . . . . . . . . 32

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

2 CALCULATIONS WITHOUT VBA

3

1

Introduction

Visual Basic for Applications, Excel’s powerful built-in programming language, permits you to easily incorporate user-written functions into a spreadsheet.1 You can easily calculate Black-Scholes and binomial option prices, for example. Lest you think VBA is something esoteric which you will never otherwise need to know, VBA is now the core macro language for all Microsoft’s office products, including Word. It has also been incorporated into software from other vendors. You need not write complicated programs using VBA in order for it to be useful to you. At the very least, knowing VBA will make it easier for you to analyze relatively complex problems for yourself. This document presumes that you have a basic knowledge of Excel, including the use of built-in functions and named ranges. I do not presume that you know anything about writing macros or programming. The examples here are mostly related to option pricing, but the principles apply generally to any situation where you use Excel as a tool for numerical analysis. All of the examples here are contained in the Excel workbook VBA.XLS.

2

Calculations without VBA

Suppose you wish to compute the Black-Scholes formula in a spreadsheet. Suppose also that you have named cells2 for the stock price (s), strike price (k), interest rate (r ), time to expiration (t), volatility (v), and dividend yield (d). You could enter the following into a cell: s*exp(-d*t)*normsdist((ln(s/k)+(r-d+v ˆ 2/2)* t)/(v*t ˆ0.5)) −k * exp(-r * t)* normsdist((ln(s/k)+(r -d-vˆ2/2)*t)/(v*tˆ0.5)) Typing this formula is cumbersome, though of course you can copy the formula wherever you would like it to appear. It is possible to use Excel’s data table feature to create a table of Black-Scholes prices, but this is cumbersome and inflexible. If you want to calculate option Greeks (e.g. delta, gamma, etc...) you must again enter or copy the formulas into each cell
This document uses keystrokes which are correct for Office 97. VBA changed dramatically (for the better, in my opinion) between Office 95 and Office 97. The general idea remained the same, but specific keystrokes changed. So far I have not found changes required for Office 2000. 2 If you do not know what a named cell is, consult Excel’s on-line help.
1

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

3 HOW TO LEARN VBA

4

where you want a calculation to appear. And if you decide to change some aspect of your formula, you have to hunt down all occurences and make the changes. When the same formula is copied throughout a worksheet, that worksheet potentially becomes harder to modify in a safe and reliable fashion. When the worksheet is to be used by others, maintainabiltiy becomes even more of a concern. Spreadsheet construction becomes even harder if you want to, for example, compute a price for a finite-lived American option. There is no way to do this in one cell, so you must compute the binomial tree in a range of cells, and copy the appropriate formulas for the stock price and the option price. is is not so bad with a 3-step binomial calculation, but for 100 steps you will spend quite a while setting up the spreadsheet. You must do this separately for each time you want a binomial price to appear in the spreadsheet. And if you decide you want to set up a put pricing tree, there is no easy way to edit your call tree to price puts. Of course you can make the formulas quite flexible and general by using lots of “if” statements. But things would become much easier if you could create your own formulas within Excel. You can — with Visual Basic for Applications.

3

How to Learn VBA

Before we look at examples of VBA, it is useful to have appropriate expectations. There are several points: • For many tasks, VBA is simple to use. We will see in a moment that creating simple add-in functions in VBA (for example to compute the Black-Scholes formula) is easy. • You can do almost anything using VBA. If you can dream of something you would like Excel to do, the odds are that VBA will enable you to do it. • You will never learn all about VBA by reading a book. If a macro language is so powerful that it enables you to do everything, it is obviously going to be too complex for you to memorize all the commands. A book or tutorial (like this one) will enable you to use VBA to solve specific problems. However, once you want to do more, you will have to become comfortable figuring out VBA by trial and error. The way to do this is...

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

4 CALCULATIONS WITH VBA

5

• Learn to use the macro-recorder in Excel. If you turn on the macro recorder, Excel will record your actions using VBA! Try this: select Tools | Macro | Record New Macro in Excel. Then create a simple graph using the graph wizard. Look at the VBA code that Excel creates. (This example is described in more detail on p. 26 below.) It is daunting when you first look at it, but if you want to use VBA to create graphs, you can now simply modify the code that Excel has recorded for you. You do not need to create the basic code from scratch. Of course this raises the question of how to modify the VBA code you now have. The simple fact is that you must be comfortable with trial-and-error. If you are a serious Excel user, VBA can make your life much simpler and greatly extend the power of Excel. The main emphasis of this tutorial is to help you create your own functions. Most of the examples here are for option pricing, but it should be obvious that there are many other uses of VBA.

4
4.1

Calculations with VBA
Creating a simple function

With VBA, it is a simple matter to create your own function, say BSCall, which will compute a Black-Scholes option price. To do this, you must first open a special kind of worksheet, called a macro module. Here are the steps required to open a new macro module, and create a simple formula: 1. Open a blank workbook using File|New. 2. Select Tools|Macro|Visual Basic Editor from the Excel menu. 3. Within the VBA editor, select Insert|Module from the menu. You will find yourself in a new window, in which you can type macro commands. 4. Within the newly-created macro module, type the following exactly (be sure to include the “ ” at the end of the second line): ’ Here is a function to add two numbers. Works well, doesn’t it ? Function addtwo(x, y) addtwo = x + y End Function Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

4 CALCULATIONS WITH VBA

6

5. Return to Excel. In cell A1 enter =addtwo(3,5) 6. Hit . You will see the result “8”. Congratulations! You have just created an add-in function! There are a number of things to notice about this example: • You need to tell the function what to expect as input, hence the list “(x,y)” following the name of the function. • You specify the value of the function with a line where you set the function name (“addtwo”) equal to a calculation (“x+y”). • An apostrophe denotes a comment, i.e. text which is not interpreted by VBA. You do not need to create comments, but they are very useful if you ever return to work you did several months ago and have to figure out what you did. • VBA is line-oriented. This means that when you start a comment with an apostrophe, if you press to go to a new line, you must enter another apostrophe or else—since what you type will almost surely not be a valid command—VBA will report an error. You can continue a line to the next line by typing an underscore, i.e. “ ” without the quotes. (You can test this out by deleting the “ ” in the example above.) • When you entered the comment and the function, VBA automatically color-coded the text and completed the syntax (the line “End Function” was automatically inserted).. Comments are coded green and the reserved words “Function” and “End” were coded in blue and automatically capitalized. • The function you typed now appears in the function wizard, just as if it were a built-in Excel function! To see this, open the function wizard using Insert|Function. In the left-hand pane (“function category”), scroll to and highlight “User Defined”. Note that “Addtwo” appears in the right-side pane (“function name”). Click on it and Excel pops up a box where you are prompted to enter inputs for the function.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

4 CALCULATIONS WITH VBA

7

4.2

A Simple Example of a Subroutine

A function returns a result. A subroutine (called a “sub” by VBA) performs an action when invoked. In the above example we used a function, because we wanted to supply two numbers and have VBA add them for us and tell us the sum. For our purposes, this is by far the most important use of VBA. However, it is often useful to create a subroutine, which is a set of statements which execute when invoked. Here is how to create one (the following is easier to do than it is to explain): 1. Return to the Visual Basic Editor 2. Click on the “Module1” window. 3. At the bottom of the module (i.e. below the function we just created) enter the following: Sub displaybox() response = MsgBox(”Greetings!”) End Sub 4. Return to Excel 5. Run the subroutine by using Tools|Macro, then double-clicking on “displaybox” out of the list. We have just created and run a subroutine. It pops up a dialog box which displays a message. The “MsgBox” function can be very useful for giving information to the spreadsheet user.

4.3

Creating a Button to Invoke a Subroutine

In the previous section we ran the subroutine by clicking on Tools|Macro|Macros and then double-clicking on the subroutine name. If you are going to run the subroutine often, creating a button in the spreadsheet is a way to create a shortcut to the subroutine. Here is how to create a button: 1. Move the mouse to the Excel toolbar and right-click once. 2. You will see a list of toolbar names. Move the highlight bar down to “forms” and left-click. A new toolbar will pop up.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

4 CALCULATIONS WITH VBA

8

3. The rectangula icon on this toolbar is the “create button” icon, which looks something like a button (of the software, not the clothing, variety). Click on it. 4. The cursor changes to a crosshair. Move the mouse to the spreadsheet, hold down the left mouse button, and drag to create a rectangle. When you lift your finger off the mouse button, a dialog box will pop up and one of the choices will be “Displaybox”. Double click on that. 5. Now move the mouse away from the button you’ve created and click once (this de-selects the button). Move the mouse back to the button you created and left-click once on it. Observe the dialog box which pops up, and click either “yes” or “no” to get rid of the dialog. Some comments: • This is obviously a trivial example. However, if you have a calculation which is particularly time-consuming (for example a Monte Carlo calculation) you might want to create a subroutine to activate it, and a button to activate the subroutine would be natural. • There is a more sophisticated version of the MsgBox function which permits you to customize the appearance of the dialog box. It is documented in the on-line help and an example of its use is contained in DisplayBox2 in the workbook. One nice feature of this more sophisticated version is that within the subroutine, we could have checked the value of the variable “response”, and had the subroutine perform different actions depending upon which button the user clicked. For an example of this, see the example Displaybox2.3

4.4

Functions can call functions

As a final example, we will demonstrate that a function can call a function (or a subroutine). 1. Enter this code in the “Module1” window
If you have examined the code for Displaybox2, you may be puzzled by checking to see if response = “vbYes”. VbYes is simply an internal constant which VBA uses to check for a “yes” button response to a dialog box. The possible responses—documented in the help file—are vbOK, vbCancel, vbAbort, vbRetry, vbIgnore, vbYes, and vbNo.
3

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

4 CALCULATIONS WITH VBA

9

Function addthree(x, y, z) addthree = addtwo(x, y) + z End Function 2. Now in cell A2, enter =addthree(3,5,7) The answer 15 will appear. This is again a trivial example, but it illustrates what you can do with functions.

4.5

Illegal Function Names

You need to be aware of what you can and cannot name a function. In Office 97, the documentation has this useful information: the function name “follows standard variable naming conventions.” This is obviously not very helpful. There are some obvious restrictions. (Please don’t take the following as definitive, these are mostly things I’ve figured out.) You cannot name a function a number. You cannot use the following characters in a function name: space . , + - : ; ” ’ ‘ # $ % / \. It was easy to produce this list because the Visual Basic compiler lets you know immediately that something is wrong if you try to put any of these characters in a function name. Note that you can use an underscore where you would like to have a space for readability of the function name. So “BS 2”, for example, is a legal function name. Here is a more subtle issue. There are function names which are legal but which you should most definitely not use. “BS2” is an example. This would be fine as the name of a subroutine, which is not called directly from a cell. But think about what happens if you give this name to a user-defined function. You enter, for example, “BS2(3)”, in a cell. How does Excel understand this? The problem is that “BS2” is also the name of a cell. So if you try to use it as a function in the spreadsheet, Excel will return the cryptic error “#REF!” and you will likely have no idea what is wrong. This is why, later in this tutorial, you will see functions named “BS 2”, “BS 3”, and so on.

4.6

Differences Between Functions and Subroutines

Functions and subroutines are not interchangable and do not have the same capabilities. Subroutines are meant to be invoked by a button or otherwise Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

5 STORING AND RETRIEVING VARIABLES

10

explicitly, while functions are meant to be inserted into cells and return results. Because of their different purpose, some VBA capabilities will work in one but not the other. In a subroutine, for example, you can write to cells of the workbook. With a subroutine you could perform a calculation and have the answer show up in cell A1. You cannot write to cells from within a function (there are several ways to do this from a subroutine; to my knowledge none of them work within functions). You cannot activate a worksheet or change anything about the display from within a function. Subroutines, on the other hand, can not be called from cells. Ocassionally you may find these restrictions annoying, but they exist because functions and subroutines are intended for different purposes.

5

Storing and Retrieving Variables in a Worksheet

Suppose that there is a value in the spreadsheet which you want to have affect your function or subroutine. (For instance you might have a variable which determines whether the option to be valued is American or European.) Or suppose you create a subroutine which performs computations. You may want to display the output in the spreadsheet. (For example, you might wish to create a subroutine to draw a binomial tree.) This raises the general question: if you are using VBA, how do you read and write values from the spreadsheet? Obviously if you are going to read and write numbers to specific locations in the spreadsheet, you must identify those locations. The easiest way to identify a location is to use a named range. The alternatives—which we will examine below—requires that you “activate” a specific location or worksheet within the workbook, and then read and write within this activated region. There are at least three ways to read and write to cells: • “Range” lets you address cells by name, • “Range().Activate” and “ActiveCell” lets you easily get at cells by using traditional cell addresses (e.g. “A1”), and • “Cell” lets you address cells using a row and column numbering scheme. Although you are probably thinking that it seems silly to have so many ways to get to cells, each is useful at different times.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

5 STORING AND RETRIEVING VARIABLES

11

5.1

Using a named range to read and write numbers from the spreadsheet

1. Enter the following function in Module1: Sub ReadVariable() x = Range(”test ”) MsgBox (Str(x)) End Sub 2. Select cell A1 in sheet “Sheet2”, then Insert|Name|Define, and type “test”, then click OK. You have just created a named range 3. Enter the value “5” in the cell you just named “test”. 4. Tools|Macro, then double-click on “ReadVariable”. At this point you have just read from a cell and displayed the result. Note that “x” is a number in this example. In order to display it using MsgBox, we first need to convert it into a string. We did this using the built-in “Str” function. (You can find locate this function by using—you guessed it—the object browser, looking under VBA, then “conversions”.) As you might guess, you can use the Range function to write as well as read. 1. Enter the following subroutine in Module1: Sub WriteVariable() Range(”Test2”) = Range(”Test”) MsgBox (”Number copied!”) End Sub 2. Give the name “test2” to cell sheet2.b2. 3. Enter a number in sheet2.b2. 4. Go to Tools|Macro, then double click on “WriteVariable”. 5. The number from Test is copied to Test2.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

5 STORING AND RETRIEVING VARIABLES

12

5.2

Reading and Writing to Cells Which are not Named.

You can also access a specific cell directly. In order to do this, you first have to “activate” the worksheet containing the cell. Here is VBA code to read a variable: Sub ReadVariable2() Worksheets(”Sheet2”).Activate Range(”A1”).Activate x = ActiveCell.Value MsgBox (Str(x)) End Sub Notice what is happening: we first activate the worksheet named “sheet2”. Next we activate the cell “A1” within Sheet2. You will notice that when you have finished calling this function, the cursor has moved to cell A1 in Sheet2! This is because what Excel means by “Active Cell” is whatever cell the cursor happens to be on; the first two lines instruct the cursor to move to sheet2.a1. The active cell has properties, such as the font, color of the cell, formatting, etc... All of these properties may be accessed using the ActiveCell function. For fun, insert the line ActiveCell.Font.Bold = True after the MsgBox function. Then switch to sheet2,run the subroutine, and watch what happens to cell A1. (I’ll bet you can guess before you run the example!) We can also assign a value to ActiveCell.Value; this is a way to write to a cell. Here is a macro which does this: Sub WriteVariable2() Worksheets(”Sheet2”).Activate Range(”A1”).Activate X = ActiveCell.Value Range(”B1”).Activate ActiveCell.Value = X End Sub This subroutine reads the number from Sheet2.A1 and copies it to Sheet2.B1.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

6 USING EXCEL FUNCTIONS

13

5.3

Using the “Cells” Function to Read and Write to Cells.

There is yet another way to read and write to cells. The “Cells” function lets you address cells using a numerical row and column numbering scheme. Here is an example illustrating how Cell works: Sub CellsExample() ’ Make ”Sheet2” the active sheet Worksheets(”Sheet2”).Activate ’ The first entry is the row, the second is the column ’ Write the number 1 into cell A3 Cells (3, 1) = 1 ’ Write the number 2 into cell A4 Cells (4, 1) = 2 ’ Copy the number from cell A3 into cell C3 Cells (3, 3) = Cells (3, 1) ’ Copy the number from cell A4 into cell C4 Cells (4, 3) = Cells (4, 1) End Sub This example reads the numbers 1 and 2 into A3 and A4, and copies them into C3 and C4. Later we will use the Cells function to draw a binomial tree.

6
6.1

Using Excel Functions from Within VBA
Using VBA to compute the Black-Scholes formula

There is only one complicated piece of the Black-Scholes calculation: computing the cumulative normal distribution, the “N()” function in the formula. Based on the very first example above, we would like to do something like the following: function BS(s,k,v,r,t,d) BS=s∗exp(−d∗t)∗normsdist((ln(s/k)+(r−d+vˆ2/2)∗t)/(v∗tˆ0.5)) −k∗exp(−r∗t)∗normsdist((ln(s/k)+(r−d−vˆ2/2)∗t)/(v∗tˆ0.5)) end function Unfortunately, this doesn’t work. The reason it doesn’t work is that VBA does not understand either “ln” or “NormSDist”. Though these are functions in Excel they are not functions in VBA, even though VBA is part of Excel! (Who says software is getting easier to use?) Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

6 USING EXCEL FUNCTIONS

14

Instead of using “ln”, we can use “log”, which is the VBA version of the same function. However, there is no VBA versions of NormSDist. Fortunately, there is a way for you to tell VBA that NormSDist is located inside of Excel. This will then make the function available for use in VBA. The following example will first show you the error you get if you fail to call NormSDist correctly: 1. Click on the “Module1” tab 2. Enter the following: Function BS(s, k, v, r, t , d) d1 = (Log(s / k) + (r − d + v \ˆ{} 2 / 2) ∗ t) / (v ∗ sqr(t)) d2 = d1 − v ∗ sqrt(t) bs = s∗exp(−d∗t)∗normsdist(d1)−k∗exp(−r∗t)∗normsdist(d2) End Function Comment: To save a little typing and to make the function more readable, we are defining the Black-Scholes “d1” and “d2” separately. You will also notice that instead of entering “ln”, we entered “log”, which—as we noted above—is built into VBA. 3. Enter into the spreadsheet =bs(40,40,.3,.08,.25,0) Hit . You will get the error message “sub or function not defined”. This error occurs because there is no version, however spelled, of “NormSDist” which is built-in to VBA. Instead, we have to tell VBA where to look for “NormSDist”. We do this by typing instead “WorksheetFunction.NormSDist”.4 Correctly typed, the function becomes:
4 If you are curious about this, do the following: Select View|Object Browser or press F2. Click on the drop-down arrow under “libraries/workbooks”, then select “Excel”. Under “Objects/Modules” click on “Application”, then under “Methods/Properties” scroll down to “NormSDist”. You have now just located “NormSDist” as a method available from the application. If you scroll around a bit you will see that there is an enormous and overwhelming number of functions available to be called from VBA. By the way, you should not make the mistake of thinking that you can call any Excel function simply by prefacing it with “WorksheetFunction” Try it with “sqrt” and it won’t work. The only way to know for sure which functions you can and can’t call is by using the object browser.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

6 USING EXCEL FUNCTIONS

15

Function BS(s, k, v, r, t , d) d1 = (Log(s / k) + (r − d + v ˆ 2 / 2) ∗ t) / (v ∗ t ˆ 0.5) d2 = d1 − v ∗ t ˆ 0.5 bs = s∗exp(−d∗t)∗WorkSheetFunction.NormSDist(d1) \ −k∗exp(− \ r∗t)∗WorkSheetFunction.NormSDist(d2) End Function The Black-Scholes function will now evaluate correctly to 2.78.

6.2

The Object Browser

The foregoing example illustrates an extremely powerful feature of VBA: it can access the functions built into Excel if you can tell it where to find them. The way you locate other functions is to use the Object Browser, which is part of VBA. Here is how to use it: 1. From within a macro module, press the F2 key. This will pop up a dialog box with the title “Object Browser”. 2. In the top left you will see a drop-down box which says “All Libraries”. Click on the down arrow at the right of this line. You will see a dropdown list with, at a minimum, “VBA” and “Excel” as two entries. (There may be other entries, depending upon how you have set up Excel.) 3. Click on VBA. 4. In the “Classes” list, click on “Math”. 5. To the right, in the “Members of Math” box, you now have a list of all the math functions which are available in VBA. Note that “log” is included in this list, but not “ln” or “NormSDist”. If you right-click on “log” and then click on “help”, you will see that “log” returns the natural logarithm. 6. Move back to the top left box which now says “VBA”. Click on the down arrow at the right of this line. 7. Click on Excel. 8. In the “Objects/Modules” list, click on “WorksheetFunction”.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

7 CHECKING FOR CONDITIONS

16

9. To the right, in the “Members of WorksheetFunction” box, you now have a list of Excel built-in functions which may be called from a macro module by specifying “ WorksheetFunctionfunctionname”. Note that both“ln” and “NormSDist” are included in this list. Note also that “log” is included in this list, but be aware that Excel’s “log” function is base 10 by default (you can specify a different base), while VBA’s is base e.5 Note also that “Sqrt” is not included. For some reason Excel’s Sqrt function is not available in VBA, which means that you cannot count on an Excel function automatically being available (though most are). If you create any VBA functions which are even moderately ambitious, you are going to use the object browser. It is the heart and soul of VBA.

7

Checking for Conditions

Frequently you want to perform a calculation only if certain conditions are met. For example, you would not want to calculate an option price with a negative volatility. It makes sense to check to see if your inputs make sense before proceeding with the calculation, and aborting—possibly with an error message—if they do not. The easy way to check is to use the construct is If ... Then ... Else.7 Here is an example of its use in checking for a negative volatility in the Black-Scholes formula:8 Function BS 2(s, k, v, r, t , d) If v > 0 Then BS 2 = BS(s, k, v, r , t , d) Else MsgBox (”Negative volatility!”) BS 2 = CVErr(xlErrValue) End If End Function
Scroll down to the “:Log” entry and then click on the “?” button at the bottom left. If you use “log” in a spreadsheet, or if you use “worksheetfunction.log” in a function, you will get the base 10 logarithm. However, if you use “log” in a function, you will get the base e logarithm! Be very very sure you know what you are doing when you use a function! 7 There is also a Case ... Select construct which we will not use. 8 You need to be aware that VBA will expect the “If then”, “Else”, and “End if” pieces to be on separate lines. If you write “else” on the same line as “If then”, for example, the code above will fail.
5

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

8 ARRAYS

17

This function checks to see if volatility is greater than 0; if it is, the function computes the Black-Scholes formula using the BS function we created earlier. If volatility is not greater than zero, then two things happen: (i) a MsgBox pops up which informs you of the mistake and (ii) the function returns a value indicating that there is an error. In general you should be cautious about putting message boxes into a function (as opposed to a procedure), since every time the spreadsheet is recalculated the message box will pop up. Since error-checking is often critically important (you would not want to quote a client a price on a deal for which you had accidentally entered a negative volatility!), it is worth expanding a bit on the use of the CVErr function. If the user enters a negative volatility, you could just have Excel return a nonsense value for the option, such as -99. This would be a bad idea, however. What if you have a complicated worksheet with many option calculations? If you failed to notice the error, the -99 would be treated as a true option value and propagated throughout your calculations. Alternatively, you could have the function return a string such as “You dweeb, you entered a negative volatility”. Apart from the fact that a colleague might use your spreadsheet and take the message personally, this is also not a good idea. Entering a string in a cell when you should have a number could have unpredictable effects on calculations which depend on the cell. While it is obvious that an addition between a string and a number will fail, suppose you are performing a regression or a frequency count. Are you sure what will happen to the calculation if you introduce a string among the numbers in your data? By using CVErr along with one of the built-in error codes (all of which are called “xlErrsomething”) you are guaranteeing that your function will return a result which Excel recognizes as a numerical error. The Excel programmers have already thought through the issues of how subsequent calculations should respond to a recognized error, and Excel usually does something reasonable in those circumstances. The Excel error codes are documented in the on-line VBA help.

8

Arrays

Often you will wish to use a single variable to store many numbers. For example, in a binomial option calculation, you have a stock price tree. After n periods, you have n possible stock prices. It can be useful to write the

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

8 ARRAYS

18

lowest stock price as P(1), the next as P(2), and the highest as P(n). The variable P is then called an array—it is a single variable which stores more than one number. You access the different elements of the array by selecting the appropriate argument.

8.1

Defining Arrays

When you create an array, it is necessary to tell VBA how big the array is going to be. You do this by using the Dim statement. Here are some examples of how to use Dim: Dim P(2) as double This creates an array of three double precision real numbers, with the array index running from 0 to 2. (By default, the first subscript in an array is 0.) If you had written Dim P(3 to 5) as double you would have created a 3-element array with the index running from 3 to 5. In this example we told Excel that the variable is type “double”. This was not necessary—we could have left the type unspecified and permitted Excel to determine the type automatically. It is faster and easier to detect mistakes, however, if we specify the type. Here is a routine which defines a 3-element array, reads numbers into the array, and then writes the array out into dialog boxes: Sub UseArray() Dim X(2) As Double X(0) = 0 X(1) = 1 X(2) = 2 y = X(0) MsgBox (Str(y)) y = X(1) MsgBox (Str(y)) y = X(2) MsgBox (Str(y)) End Sub You should enter this code and execute it to see what happens. The subroutine UseArray can also be written as follows Sub UseArray2() Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

9 ITERATING

19

X = Array(0, 1, 2) y = X(0) MsgBox (Str(y)) y = X(1) MsgBox (Str(y)) y = X(2) MsgBox (Str(y)) End Sub There is an interesting difference between UseArray and UseArray2 in the way arrays are declared. In UseArray, there is a dimension statement, and then array elements are created one by one. In UseArray2, there is no dimension statement, and the array function (built into VBA) is used to initialize the array. UseArray will fail without the “Dim” statement, and UseArray2 will fail with the “Dim” statement. I don’t know why. You might wonder why in this example I write y = X(2) MsgBox (Str(y)) instead of just MsgBox (Str(X(2))) The reason is that “MsgBox(Str(X(2)))” does not work. It seems that it should, but it doesn’t.1 Finally, notice the repetition in these examples! The statements which put numbers into the array are essentially repeated three times (albeit more compactly in UseArray2), and the statements which read numbers out of the array are repeated three times. Suppose the array had 100 elements! Surely there must be a better way. There is. It is time to learn about how to perform repetitive calculations by iterating.

9

Iterating

Many kinds of option calculations are repetitive. For example, when we compute a binomial option price, we generate a stock price tree and then traverse the tree, calculating the option price at each node. Similarly, when
This is just one example of why real programmers might kick silicon in your face if you tell them you’re using VBA instead of, say, C++. But you can just smile, get your work done, and close the deal, while they’re still yapping about how VBA is nothing but a toy.
1

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

9 ITERATING

20

we compute an implied volatility we need to perform a calculation repeatedly until we arrive at the correct volatility. VBA provides us with the ability to write one or more lines of code which can be repeated as many times as we like.

9.1

A simple for loop

Here is an example of a for loop, which does exactly the same thing as the UseArray subroutine above: Sub UseArrayLoop() Dim X(2) As Double For i = 0 To 2 X(i) = i Next i For i = 0 To 2 y = X(i) MsgBox (Str(y)) Next i End Sub This table translates the syntax in the first loop above: For i = 0 to 2 Repeat the following statements three times, the first time setting i = 0, the next time i = 1, and finally i = 2. Set the variable y equal to the ith value of X. Go back and repeat the statement for the next value of i.

y = X(i) Next i

9.2

Creating a binomial tree

In order to create a binomial tree, we need the following information: • The initial stock price • The number of time periods • The magnitudes up and down by which the stock moves. Suppose we wish to draw a tree where the initial price is 100, we have 10 binomial periods, and the moves up and down are u = 1.25 and d = 1/u Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

9 ITERATING

21

= .8. Here is a subroutine, complete with comments explaining the code, which will create this tree. You first need to name a worksheet “output”, and then we will write the tree to this worksheet. The number of binomial steps and the magnitude of the moves are read from named cells, which can be in any worksheet. I have placed those named cells in Sheet1. We will put these named cells in Sheet2.6 Sub DrawBinomialTree() ReDim stock(2) ’ provide default of 2 steps if no steps specified Dim i as integer Dim t as integer n = Range(”n”) ’ number of binomial steps u = Range(”u”) ’ move up P0 = Range(”p0”) ’ initial stock price d = 1 / u ’ move down ReDim stock(n + 1) ’ array of stock prices Worksheets(”Output”).Activate ’ Erase any previous calculations Worksheets(”Output”).Cells.ClearContents Cells (1, 1) = P0 ’ We will adopt the convention that the column holds the ’ stock prices for a given point in time. The row holds ’ stock prices over time. For example, the first row ’ holds stock prices resulting from all up moves, the ’ second row holds stock prices resulting from a single ’ down move, etc... ’ The first loop is over time For t = 2 To n Cells (1, t) = Cells (1, t $−$ 1) ∗ u ’ The second loop is across stock prices at a given time For i = 2 To t Cells( i , t) = Cells( i $−$ 1, t $−$ 1) ∗ d Next i Next t End Sub
If it is not obvious to you that using the “Cells” function is the best way to write to cells here, you should think about how you would do it using the other methods we discussed earlier.
6

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

10

READING AND WRITING ARRAYS

22

Several comments: • There is a simple command to clear an entire worksheet, namely: Worksheets(“worksheetname”).Cells.ClearContents. • The use of the Cells function means that you can perform the calculation exactly as you would if you were writing it down, using subscripts to denote which price you are dealing with. Think about how much more complicated it would be to use traditional row and column notation (e.g. “A1”) to perform the same function. Note that this function uses the “ReDim” command to specify a flexible array size. Sometimes you do not know in advance how big your array is going to be. In this example you are unsure how many binomial periods the user will specify. If you are going to use an array to store the full set of prices at each point in time, this presents a problem—how large do you make the array? You could specify the array to have a very large size, one larger than any user is ever likely to use, but this kind of practice could get you into trouble someday. Fortunately, VBA permits you to specify the size of an array using a variable. You do it by using the Redim (“dimension”) statement.

9.3

Other kinds of loops

We will not go into them here, but you should be aware that there are other looping constructs available in VBA. The following kinds of loops are also available: • Do Until .... Loop and Do ... Loop Until • Do While ... Loop and Do ... Loop While • While ... Wend • For Each ... In ... Next If you ever think you need them, you can look these up in the on-line help.

10

Reading and Writing Arrays

A powerful feature of VBA is the ability to read arrays as inputs to a function and also to write functions which return arrays as output. Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

10

READING AND WRITING ARRAYS

23

10.1

Arrays as Output

Suppose you would like to create a single function which returns two numbers: the Black-Scholes price of a call option, and the option delta. Let’s call this function BS 3 and create it by modifying the function BS from Section 6.1. Function bs 3(s, k, v, r , t , d) d1 = (Log(s / k) + (r − d + 0.5 ∗ v ˆ 2) ∗ t ) / ( v ∗ t ˆ 0.5) d2 = d 1 − v ∗ t ˆ 0.5 nd1 = WorksheetFunction.NormSDist(d1) nd2 = WorksheetFunction.NormSDist(d2) delta = Exp(−d ∗ t) ∗ nd1 price = s ∗ delta − k ∗ Exp(−r ∗ t) ∗ nd2 bs 3 = Array(price, delta) End Function The key section is the line bs 3 = Array(price, delta) We assign an array as the function output, using the array function introduced in Section 8. If you just enter the function bs 3 in your worksheet in the normal way, in a single cell, it will return a single number. In this case, that single number will be the option price, which is the first element of the array. If you want to see both numbers, you have to enter bs 3 as an array function: select a range of two cells, enter the formula in the first, and then hit Ctrl-Shift-Enter to create the array function. There is a 50% probability you just discovered a catch. The way we have written bs 3, the array output is horizontal. If you enter the array function in cells a1:a2, for example, you will see the option price repeated twice. If you enter the function in a1:b1, you will see the price and the delta. What happens if we want vertical output? The answer is that we transpose the array using the Excel function of that name, modifying the last line to read bs 3 = WorksheetFunction.Transpose(Array(price, delta)) This will make the output be vertical. There is also a way to make the output both horizontal and vertical. We just have to return a 2 × 2 array. Here is an illustration of how to do that: Function bs 4(s, k, v, r , t , d) Dim temp(1, 1) As Double Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

10

READING AND WRITING ARRAYS

24

d1 = (Log(s / k) + (r − d + 0.5 ∗ v ˆ 2) ∗ t ) / ( v ∗ t ˆ 0.5) d2 = d 1 − v ∗ t ˆ 0.5 nd1 = WorksheetFunction.NormSDist(d1) nd2 = WorksheetFunction.NormSDist(d2) delta = Exp(−d ∗ t) ∗ nd1 price = s ∗ delta − k ∗ Exp(−r ∗ t) ∗ nd2 temp(0, 0) = price temp(0, 1) = delta temp(1, 0) = delta temp(1, 1) = 0 bs 4 = temp End Function Now it does not matter whether you select cells a1:a2 or a1:b1; either way, you will see both the price and the delta.2

10.2

Arrays as Inputs

We may wish to write a function which processes many inputs, where we do not know in advance how many inputs there will be. Excel’s built-in functions “sum” and “average” are two familiar examples of this. They both can take a range of cells as input. It turns out it is easy to write functions which accept ranges as input. Once in the function, the array of numbers from the range can be manipulated in at least two ways: as a collection, or as an actual array with the same dimensions as the range. 10.2.1 The Array as a Collection

First, here are two examples of how to use a collection. Sumsq takes a set of numbers, squares each one, and adds them up: Function sumsq(x) Sum = 0 For Each y In x Sum = Sum + y ˆ 2 Next sumsq = Sum End Function The function sumsq can take a range (e.g. “a1:a10”) as its argument. The “for each” construct in VBA loops through each element of a collection,
2

What do you see if you select cells a1:b2? What about a1:d4?

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

10

READING AND WRITING ARRAYS

25

without our having to know in advance how many elements the collection has. There is another way to loop through the elements of a collection. The function SumProd takes two equally-sized arrays, multiplies them element by element, and returns the sum of the multiplied elements. In this example, because we are working with two collections, we need to use a more standard looping construct. To do this, we need to first count the number of elements in each array. This is done using the “count” property of a collection. If there is a different number of elements in each of the two arrays, we exit and return an error code. Function SumProd(x1, x2) n1 = x1.Count n2 = x2.Count If n1 n2 Then ’ exit if arrays not equally−sized sumprod = CVErr(xlErrNum) End End If Sum = 0 For i = 1 To n1 Sum = Sum + x1(i) ∗ x2(i) Next i i =1 SumProd = Sum End Function 10.2.2 The Array as an Array

We can also treat the numbers in the range as a literal array. The only trick to doing that is that we need to know the dimensions of the array, i.e. how many rows and columns it has. The function RangeTest illustrates how to do this. Function RangeTest(x) prod = 1 r = x.Rows.Count c = x.Columns.Count For i = 1 To r For j = 1 To c prod = prod ∗ x(i, j ) Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

11

MISCELLANY

26

Next j Next i RangeTest = WorksheetFunction.Transpose(Array(prod, r, c)) End Function This function again multiplies together the cells in the range. It returns not only the product, but also the number of rows and columns. When x is read into the function, it is considered by VBA to be an array.3 “Rows” and “Columns” are properties of an array. The construct x.Rows.Count tells us the number of rows in the array. With this capability, we could multiply arrays, check to see whether two ranges have the same dimensions, and so on.

11
11.1

Miscellany
Getting Excel to generate your macros for you

Suppose you want to perform a task and you don’t have a clue how to program it in VBA. For example, suppose you want to create a subroutine to set up a graph. You can set up a graph manually, and tell Excel to record the VBA commands which accomplish the same thing. You then examine the result and see how it works. To do this, select Tools|Record Macro|Record New Macro. Excel will record all your actions in a new module located at the end of your workbook, i.e. following Sheet16. You stop the recording by clicking the -?- button which should have appeared on your spreadsheet when you started recording. Macro recording is an extremely useful tool for understanding how Excel and VBA work and interact; this is in fact how the Excel experts learn how to write macros which control Excel’s actions. For example, here is the macro code Excel generates if you use the chart wizard to set up a chart using data in the range A2:C4. You can see among other things that the selected graph style was the fourth line graph in the graph gallery, and that the chart was titled “Here is the title”. Also, each data series is in a column and that the first column was used as the x-axis (“CategoryLabels:=1”).
3

You can verify this by using the VBA function IsArray. For example, you could write y = IsArray(x)

and y will have the value “true” if x is a range input to the function.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

11

MISCELLANY

27

’ Macro1 Macro ’ Macro recorded 2/17/99 by Robert McDonald ’ ’ Sub Macro1() Range(”A2:C4”).Select ActiveSheet.ChartObjects.Add(196.5, 39, 252.75, 162).Select Application.CutCopyMode = False ActiveChart.ChartWizard Source:=Range(”A2:C4”), Gallery:=xlLine, Format:=4, PlotBy:=xlColumns, CategoryLabels:=1,SeriesLabels :=0, HasLegend:=1, Title:=”Here is the Title”, CategoryTitle :=”X−Axis”, ValueTitle:=”Y−Axis”, ExtraTitle:=”” End Sub

11.2

Using multiple modules

You can split up your functions and subroutines among as many modules as you like—functions from one module can call another, for example. Using multiple modules is often convenient for clarity. If you put everything in one module you will spend a lot of time scrolling around.

11.3

Recalculation speed

One unfortunate drawback of VBA—and of most macro code in most applications—is that it is slow. When you are using built-in functions, Excel performs clever internal checking to know whether something requires recalculation (you should be aware that on occasion it appears that this clever checking goes awry and something which should be recalculated, isn’t). When you write a custom function, however, Excel is not able to perform its checking on your functions, and it therefore tends to recalculate everything. This means that if you have a complicated spreadsheet, you may find very slow recalculation times. This is a problem with custom functions and not one you can do anything about. There are tricks for speeding things up. Here are two: • If you are looping a great deal, be sure to declare your looping index variables as integers. This will speed up Excel’s handling of these variables. For example, if you use i as the index in a for loop, use the statement Dim i as integer Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

11

MISCELLANY

28

• While a lengthy subroutine is executing, you may wish to turn off Excel’s screen updating. You do this by Application.ScreenUpdating = False This will only work in subroutines, not in functions. If you want to check the progress of your calculations, you can turn ScreenUpdating off at the beginning of your subroutine. Whenever you would like to see your calculation’s progress (for example every 100 th iteration) you can turn it on and then immediately turn it off again. This will update the display. • Finally, here is a good thing to know: Ctrl-Break will (Usually) stop a recalculation! Remember this. Someday you will thank me for it. Ctrl-Break is more reliable if your macro writes output to the screen or spreadsheet.

11.4

Debugging

We will not go into details here, but VBA has very sophisticated debugging capabilities. For example, you can set breakpoints (i.e. lines in your routine where Excel will stop calculating to give you a chance to see what is happening) and watches (which means that you can look at the values of variables at different points in the routine). Look up “debugging” in the on-line help.

11.5

Creating an Add-in

Suppose you have written a useful set of option functions and wish to make them broadly available in your spreadsheets. You can make the functions automatically available in any spreadsheet your write by creating an addin. To do this, you simply switch to a macro module and then Tools|Make Add-in. Excel will create a file with the XLA extension which contains your functions. You can then make these functions automatically available by Tools|Add-ins, and browse to locate your own add-in module if it does not appear on the list. Any functions available through an add-in will automatically show up in the function list under the set of “user-defined” functions.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

12

A SIMULATION EXAMPLE

29

12

A Simulation Example

Suppose you have a large amount of money to invest. Suppose that at the end of the next five years you wish to have it fully invested in stocks. It is often asserted in the popular press that it is preferable to invest it in the market gradually, rather than all at once. In particular, consider the strategy of each quarter taking a pro rata share of what is left and investing it in stocks. So the first quarter invest 1/20th in stocks, the second invest 1/19th of money remaining in stocks, etc... It is obvious that the strategy in which we invest in stocks over time should have a smaller average return and a lower standard deviation than a strategy in which we plunge into stocks, but how much lower and smaller? Monte Carlo simulation is a natural tool to address a question like this. We will first see how to structure the problem and then analyze it in Excel.9,10 You may not understand the details of how the random stock price is generated. That does not matter for purposes of this example; rather, the important thing is to understand how the problem is structured and how that structure is translated into VBA.

12.1

What is the algorithm?

To begin, we describe the investment strategy and the evolution over time of the portfolio. Suppose we initially have $100 which is invested in bonds and nothing invested in stock. Let the variables BONDS and STOCK denote the amount invested in each. Let h be the fraction of a year between investments in stock (so for example if h = .25, there are 4 transfers per year from bonds to stock), and let r, µ, and σ denote the risk-free rate, the expected return on the stock, and the volatility of the stock. Suppose we switch from bonds to stock 20 times, once a quarter for 5 years. Let n = the number of times we will switch. We need to know the stock price each time we switch. Denote these prices by PRICE(0), PRICE(1), PRICE(2), ... , PRICE(20). Now each period, at the beginning
The following example is considerably more complicated than those that precede it. It is designed to illustrate many of the basic concepts in a non-trivial fashion. You may wish to skip it initally, and return to it once you have had some experience with VBA. 10 If you are thinking about option-pricing, you might expect this example to be computed using the risk-neutral distribution. Instead, we will compare the actual payoff distributions of the two strategies in order to compare the means and standard deviations. If we wished to value the two strategies, we would substitute the risk-neutral distribution by replacing the 15% expected rate of return with the 10% risk-free rate. After making this substitution, both strategies would have the same expected payoff of $161 (100 ×1.15). Since both strategies entail buying assets at a fair price, there is no need to perform a valuation! Both will be worth the initial investment.
9

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

12

A SIMULATION EXAMPLE

30

of the period we first switch some funds from bonds to stock. At the end of the period, we figure out how much we earned over the period. If we wish to switch a roughly constant proportion each period, we could switch 1/20 the first period, 1/19 with 19 periods to go, and so forth. This suggests that at the beginning of period j, bonds(j)=bonds(j−1) ∗ (1−1/(n+1−j)) stock(j)=stock(j−1)+bonds(j−1)/(n+1−j) At the end of the period we have stock( j)=stock(j) ∗ price( j)/price ( j−1) bonds(j) = bonds(j) ∗ exp(r ∗ h) In words, during period j, we earn interest on the bonds and capital gains on the stock. We can think of the STOCK(j) and BONDS(j) on the righthand side as denoting beginning of period values after we have allocated some dollars from bonds to stock, and the values on the right-hand side as the end-of-period values after we have earned interest and capital gains. We compute capital gains on the stock by price( j) = price( j−1) ∗ exp((mu − 0.5 ∗ v ˆ 2) ∗ h + v ∗ h ˆ (0.5) ∗ WorksheetFunction.NormSInv(rnd())) As mentioned above, it is not important if you do not understand this expression. It is the standard way to create a random lognormally-distributed stock price, where the expected return on the stock is mu, the volatility is v, and the length of a period is h. At the end, j = n, and we will invest all remaining bonds in stock, and earn returns for one final period. This describes the stock and bond calculations for a single set of randomlydrawn lognormal prices. Now we want to repeat this process many times. Each time, we will save the results of the trial and use them to compute the distribution.

12.2

VBA code for this example.

We will set this up as a subroutine. The first several lines in the routine simply activate the worksheet where we will write the data, and then clear the area. We need two columns: one to store the terminal portfolio value if we invest fully in stock at the outset, the other to store the terminal value if invest slowly. Note that we have set it up to run 2000 trials, and we also clear 2000 rows. We tell VBA that the variables “bonds”, “stock”, and “price” are going to be arrays of type double, but we do not yet know what size to make the array. The “Worksheets(“Invest Output”).Activate makes Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

12

A SIMULATION EXAMPLE

31

the “invest output” worksheet the default worksheet, so that all reading and writing will be done to it unless another worksheet is specified.
1 2 3 4 5 6 7 8 9

Sub Monte invest() Dim bonds() As Double Dim stock() As Double Dim price() As Double Worksheets(”Invest Output”).Activate Range(”a1 .. b2000 ”). Select Selection .Clear ’ number of monte−carlo trials iter = 2000 Now we set the parameters. The risk-free rate, mean return on the stock, and volatility are all annual numbers. We invest each quarter, so h = 0.25. There are 20 periods to keep track of since we invest each quarter for 5 years. Note that once we specify 20 periods, we can dimension the bonds, stock, and price variables to run from 0 to 20. We do this using the “Redim” command.

10 11 12 13 14 15 16 17 18 19 20 21

’ number of reinvestment periods n = 20 ’ Reset the dimension of the bonds and stock variable ReDim bonds(0 To n), stock(0 To n), price(0 To n) ’ length of each period h = 0.25 ’ expected return on stock mu = 0.15 ’ risk −free interest rate r = 0.1 ’ volatility v = 0.3 Now we have an outer loop. Each time through this outer loop, we have one trial, i.e. we draw a series of 20 random stock prices and we see what the terminal payoff is from our 2 strategies. Note that before we run through a single trial we have to initialize our variables: the initial stock price is 100, we have $100 of bonds and no stock, and “Price(0)”, which is the intial stock price, is set to 100.

22 23 24

’ each time through this loop is one complete iteration For i = 1 To iter price(0) = 100 bonds(0) = 100 Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

12

A SIMULATION EXAMPLE

32

25

stock(0) = 0 This is the heart of the program. Each period for 20 periods we perform our allocation as above. Note that we draw a new random stock price using our standard lognormal expression.

26 27 28 29 30 31 32 33 34 35 36 37 38 39

For j = 1 To n ’ allocate 1/n of bonds to stock stock( j) = stock(j−1) + bonds(j−1) / (n + 1 − j) bonds(j) = bonds(j−1) ∗ (1 $−$ 1 / (n + 1 $−$ j)) ’ draw a new lognormal stock price price( j) = price( j−1) ∗ Exp((mu − 0.5 ∗ v ˆ 2) ∗ h + v ∗ h ˆ (0.5) ∗ WorksheetFunctionNormSInv(Rnd())) ’ earn returns on bonds and stock bonds(j) = bonds(j) ∗ Exp(r ∗ h) stock(j) = stock(j ) ∗ ( price( j ) / price( j−1)) Next j Once through this loop, all that remains is to write the results to “sheet1”. The following two statements do that, by writing the terminal price to column 1, row i, and the value of the terminal stock position to column 2, row i.

40 41 42 43 44 45

ActiveSheet.Cells( i , 1) = price(n) ActiveSheet.Cells( i , 2) = stock(n) Next i End Sub Note that you could also write the data across in columns: you would do this by writing ActiveSheet.Cells (1, i) = p1 This would write the terminal price across the first row.

12.3

A trick to speed up the calculations

Modify the inner loop by adding the two lines referring to “screenupdating”:

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

12

A SIMULATION EXAMPLE

33

’ each time through this loop is one complete iteration For i = 1 To iter Application.ScreenUpdating = false ... If ( i mod 100 = 0) then application.screenupdating = true ActiveSheet.Cells( i , 1) = price( i ) ActiveSheet.Cells( i , 2) = stock( i ) Next i The first line prevents Excel from updating the display as the subroutine is run. It turns out that it takes Excel quite a lot of time to redraw the spreadsheet and graphs when numbers are added. The second line at the end redraws the spreadsheet every 100 iterations. The “mod” function returns the remainder from dividing the first number by the second. Thus, i mod 100 will equal 0 whenever i is evenly divisible by 100. So on iteration numbers 100, 200, etc..., the spreadsheet will be redrawn. This cuts the calculation time approximately in half. Note that “Application.ScreenUpdating” is an example of a command which only works within a subroutine. It will not work within a function.

Copyright c 1995-2000, Robert L. McDonald. November 3, 2000

Similar Documents

Free Essay

Energy Drink

...• IT-based, everything based on Excel (and VBA) • Professional data source: Datastream Results • • • • Better understanding of (known) financial theory Better Excel-skills Better feeling with real world data All in all: Easier to get started (at your thesis/job) 3 / 17 1 Examples of skills after the course In half a work-day (or 4 hours exam :-) you will be able to solve one of these problems: • • • • Form a reasonable stock portfolio. Price an option, (Eur, Amr, Ber, Asian) Estimation of the term structure, (cubic spline). Predict the standard deviation of stock returns for next week, (basis for risk management). - You know the theories, but it will take you half a “term paper” to get an answer if you have not tried it before. 4 / 17 Structure of the Lessons 15 lessons (2 hours lecture and exercises) 14 has the following structure: 1. 2. 3. 4. E-lec: Review of last weeks exercise E-lec: Financial theory and Excel for next exercise You solve the exercises (guiding solution available) Physical lecture: Your questions / problems / Homepage... Very exercise based • Little reading, lots of “hands on” computer work • Exam on computers, (like exercises) • Hence: Excel-routine raises your grade. 5 / 17 2 Plan for the Lessons • Lesson 1: – Lec 1.1 (now) (physical!) – Lec 1.2 Excel functionality (online) • Lesson 2: – Lec 2.1 Financial and statistical issues (online) – Lec 2.2 Introduction to VBA...

Words: 851 - Pages: 4

Free Essay

Mr Menglin Liu

...Welcome Dr Lara Cathcart and Professor James Sefton Programme Directors of the MSc Finance Suite of programmes Imperial Facts & figures Founded 1907 Students from 126 countries Taught courses 242 Imperial Research Assessment Exercise 2008 (% 4* and 3*) 1 Imperial College London 72.9% 2 University of Cambridge 71.2% 3 University of Oxford 70.3% 4 London School of Economics 68.4% 5 University College London 65.8% 6 University of Manchester 65.4% 7 University of Warwick 64.9% 8 University of Essex 62.9% 9 University of Edinburgh 62.8% 10 Queen Mary, University of London 62.6% Business School Rank 1 2= 2= 5= 5= 5= 9= 9= 9= RAE 2008 Business & Management London Business School Imperial College London Cardiff University University of Bath London School of Economics University of Oxford Cranfield University Lancaster University University of Warwick %4* and 3* 85% 85% 70% 70% 70% 70% 70% 65% 75% 75% FT = 2 Rankings of Pre-experience MSc in Finance (June 2014) University of Cambridge 75% Refers to 2010 – 11 intake 5= ImperialKing's College LondonWorld and 3rd in UK Business School 16th in Rank based on 4* only Suite of Finance programmes • The average acceptance rate is around 11%. • In terms of selectivity the finance programmes are top in the Business School and in the top 10% of Imperial College. Business School • Exciting year to join Business School -Growing School under our new Dean Professor G. ‘Anand’ Anandalingam -Two new MSc Finance...

Words: 742 - Pages: 3

Premium Essay

Financial Modelling

...Advanced Modelling in Finance using Excel and VBA Mary Jackson and Mike Staunton JOHN WILEY & SONS, LTD Chichester ž New York ž Weinheim ž Brisbane ž Singapore ž Toronto Copyright  2001 by John Wiley & Sons, Ltd, Baffins Lane, Chichester, West Sussex PO19 1UD, England National 01243 779777 International (C44) 1243 779777 e-mail (for orders and customer service enquiries): cs-books@wiley.co.uk Visit our Home Page on http://www.wiley.co.uk or http://www.wiley.com All Rights Reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency, 90 Tottenham Court Road, London W1P 9HE, UK, without the permission in writing of the publisher. Other Wiley Editorial Offices John Wiley & Sons, Inc., 605 Third Avenue, New York, NY 10158-0012, USA Wiley-VCH Verlag GmbH, Pappelallee 3, D-69469 Weinheim, Germany John Wiley & Sons Australia Ltd, 42 McDougall Street, Milton, Queensland 4064, Australia John Wiley & Sons (Asia) Pte Ltd, 2 Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809 John Wiley & Sons Canada Ltd, 6045 Freemont Blvd, Mississauga, ONT, L5R 4J3, Canada British Library Cataloguing in Publication Data A catalogue record for this book is available from the British...

Words: 57326 - Pages: 230

Premium Essay

Access 2007

...Microsoft® Office Access 2007 ™ ALL-IN-ONE DESK REFERENCE FOR DUMmIES ‰ by Alan Simpson, Margaret Levine Young, Alison Barrows, April Wells, Jim McCarter Microsoft® Office Access 2007 ™ ALL-IN-ONE DESK REFERENCE FOR DUMmIES ‰ Microsoft® Office Access 2007 ™ ALL-IN-ONE DESK REFERENCE FOR DUMmIES ‰ by Alan Simpson, Margaret Levine Young, Alison Barrows, April Wells, Jim McCarter Microsoft® Office Access™ 2007 All-in-One Desk Reference For Dummies® Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www...

Words: 190131 - Pages: 761

Premium Essay

Electronic Banking

...CONTENTS S.No. Title 1 Introduction 1.1 project background 1.2 Problem statement 1.3 Project objectives 2 Project Deliverables 2.1 Introduction project management and 2.2 planning 2.3 Analysis 2.4 Design 2.5 Implementation 2.6 2.7 3 Testing Evaluation Research 3.1 Research on strategy 3.2 Research on similar project 3.3 Research on methodologies Research on Development 3.4 tools 4 References Page Number 1 1 3 3 3 3 4 5 5 6 7 8 8 8 8 9 12 14 1 THE WORKING OF ATM Chapter 1: Introduction 1.1 project background Automated teller Goods (Automatic teller equipment) will often be a growing number of utilized these days as a possible easy and simple, hassle-free assortment with regards to guide Tellers. However, there may be present research upwards with regards to quite a few apparent many different numerous insecurities by way of ATMs, their own features and the way they may be successful. This kind of forms identifies what sort of financial institution works usually in addition to signifies specific issues regarding security through employing these kinds of Cash machine gadgets. Evaluation signifies that existing Bank techniques appear to offer we “security through obscurity” instead of the a lot encouraged “open, specialist review” strategy. This may at risk of become due to Financial institution businesses never improving their unique executive so that we can preserve computability along with ATM machine vendors. Work with a considerably guarded style concerning financial...

Words: 5025 - Pages: 21

Free Essay

Pokemon Emerald, More, Top

...pokemon * Prime * Sign in or Register * Top of Form Bottom of Form IGN's Best of January Winners * Game Highlights * Create Wiki * Review * Videos * Images * All Articles Pokemon Emerald Version - Gameshark Codes List ========================================================== ========================================================== POKEMON EMERALD US VERSION GAMESHARK CODES FAQ (Version 3) ========================================================== ========================================================== ================= TABLE OF CONTENTS ================= Section 1:Version Updates Section 2:Master Code (Gameshark) Section 3:Wild Pokemon Modifier Section 4:Wild Pokemon Level Modifier Section 5:Wild Pokemon Nature And Gender Modifier Section 6:Wild Pokemon Modifier Second Part Section 7:Wrap Codes Section 8:TM Modifier Section 9:Buy Any Item Section 10:General Codes Section 10:Note From The Author Section 10:Thanks ==================================================== ========================= SECTION 1:VERSION UPDATES ========================= ---------- Version 2 ---------- Added Complete Wild Pokemon Modifier with Gender and Nature Modifier Added TM Modifier ---------- Version 3 ---------- Added Buy Any Item Section:9 And General Codes Section Section:10 ============================================================ ========================== MASTER CODE...

Words: 20441 - Pages: 82

Free Essay

Linux as a Personal Desktop Operating System

...Table of Contents Beginning Red Hat Linux 9 ...............................................................................................................................1 Introduction.........................................................................................................................................................4 Who Is This Book For?...........................................................................................................................4 What You Need to Use This Book.........................................................................................................4 What Is Linux? ........................................................................................................................................5 Linus Torvalds and the Birth of Linux.............................................................................................5 Why Choose Linux?.........................................................................................................................6 What Is Red Hat Linux 9?................................................................................................................7 What Is Covered in This Book?..............................................................................................................8 Conventions in This Book......................................................................................................................9 Customer Support...............

Words: 155032 - Pages: 621

Free Essay

Dr Manger

...Organizational Theory, Design, and Change Jones 6th Edition Test Bank Click here to download the solutions manual / test bank INSTANTLY!!! http://solutionsmanualtestbanks.blogspot.com/2011/10/organizational-theory-d esign-and-change_18.html ----------------------------------------------------------------------Organizational Organizational Organizational Organizational Theory, Theory, Theory, Theory, Design, Design, Design, Design, and and and and Change Change Change Change Jones Jones Jones Jones 6th 6th 6th 6th Edition Edition Edition Edition Test Test Test Test Bank Bank Bank Bank -------------------------------------------------------------------------***THIS IS NOT THE ACTUAL BOOK. YOU ARE BUYING the Test Bank in e-version of the following book*** Name: Organizational Theory, Design, and Change Author: Jones Edition: 6th ISBN-10: 0136087310 Type: Test Bank - The test bank is what most professors use an a reference when making exams for their students, which means there’s a very high chance that you will see a very similar, if not exact the exact, question in the test! - The file is either in .doc, .pdf, excel, or zipped in the package and can easily be read on PCs and Macs. - Delivery is INSTANT. You can download the files IMMEDIATELY once payment is done. If you have any questions, please feel free to contact us. Our response is the fastest. All questions will always be answered in 6 hours. This is the quality of service we are providing and we hope to be your...

Words: 29834 - Pages: 120

Free Essay

Customer Satisfaction

...Transforming Lives Communities The Nation …One Student at a Time Disclaimer Academic programmes, requirements, courses, tuition, and fee schedules listed in this catalogue are subject to change at any time at the discretion of the Management and Board of Trustees of the College of Science, Technology and Applied Arts of Trinidad and Tobago (COSTAATT). The COSTAATT Catalogue is the authoritative source for information on the College’s policies, programmes and services. Programme information in this catalogue is effective from September 2010. Students who commenced studies at the College prior to this date, are to be guided by programme requirements as stipulated by the relevant department. Updates on the schedule of classes and changes in academic policies, degree requirements, fees, new course offerings, and other information will be issued by the Office of the Registrar. Students are advised to consult with their departmental academic advisors at least once per semester, regarding their course of study. The policies, rules and regulations of the College are informed by the laws of the Republic of Trinidad and Tobago. iii Table of Contents PG 9 PG 9 PG 10 PG 11 PG 11 PG 12 PG 12 PG 13 PG 14 PG 14 PG 14 PG 14 PG 15 PG 17 PG 18 PG 20 PG 20 PG 20 PG 21 PG 22 PG 22 PG 22 PG 23 PG 23 PG 23 PG 23 PG 24 PG 24 PG 24 PG 24 PG 25 PG 25 PG 25 PG 26 PG 26 PG 26 PG 26 PG 26 PG 26 PG 27 PG 27 PG 27 PG 27 PG 27 PG 27 PG 28 PG 28 PG 28 PG 28 PG 28 PG 33 PG 37 Vision Mission President’s...

Words: 108220 - Pages: 433

Free Essay

Visio 2010 Español

...Contenido Iniciando con Visio 2010 - 1 - Soluciones disponibles en cada edición de Visio - 1 - 2. Entendiendo el entorno de Visio 2010 - 3 - El entorno de dibujo contra la vista Backstage - 3 - La fluida interfaz de usuario (la cinta) - 4 - Las formas y las ventanas de dibujo - 6 - 3. Creando diagramas conectados - 8 - Hacer un SmartShape desde una galería de símbolos - 8 - Acomodando los cuatro mejores SmartShapes en una galería de símbolos - 8 - Haciendo uso de la lista de selección del cursor-Tip - 9 - Utilizando los puntos de selección Auto-Connect - 9 - Insertando un SmartShape entre dos SmartShapes ya existentes - 10 - Borrando un SmartShape entre dos SmartShapes ya existentes - 10 - Utilizando alineación automática y el espacio para organizar SmartShapes - 11 - Usando Re-Layout para reorganizar diagramas conectados en la página - 11 - Usando los ajustes y técnicas Snap - 12 - Usando de los ajustes y las técnicas de pegamento - 13 - 4. Formateando SmartShapes y el texto asociado - 15 - Formateando rellenos de formas - 15 - Formateando líneas de forma - 16 - Formateando texto con forma - 18 - 5. Insertando objetos adicionales a los diagramas - 21 - Insertando páginas en primer plano y fondo - 21 - Insertando Ilustraciones incluyendo fotos, imágenes prediseñadas, gráficos y dibujos CAD - 22 - Insertando contenedores y llamadas - 24 - Inserting hyperlinks - 25 - Inserting text-related objects including text boxes, screen...

Words: 68400 - Pages: 274

Premium Essay

Damsel

...2014-2015 Undergraduate Academic Calendar and Course Catalogue Published June 2014 The information contained within this document was accurate at the time of publication indicated above and is subject to change. Please consult your faculty or the Registrar’s office if you require clarification regarding the contents of this document. Note: Program map information located in the faculty sections of this document are relevant to students beginning their studies in 2014-2015, students commencing their UOIT studies during a different academic year should consult their faculty to ensure they are following the correct program map. i Message from President Tim McTiernan I am delighted to welcome you to the University of Ontario Institute of Technology (UOIT), one of Canada’s most modern and dynamic university communities. We are a university that lives by three words: challenge, innovate and connect. You have chosen a university known for how it helps students meet the challenges of the future. We have created a leading-edge, technology-enriched learning environment. We have invested in state-of-the-art research and teaching facilities. We have developed industry-ready programs that align with the university’s visionary research portfolio. UOIT is known for its innovative approaches to learning. In many cases, our undergraduate and graduate students are working alongside their professors on research projects and gaining valuable hands-on learning, which we believe is integral...

Words: 195394 - Pages: 782

Free Essay

Core Concepts of Ais

...CORE CONCEPTS OF Accounting Information Systems Twelfth Edition Mark G. Simkin, Ph.D. Professor Department of Accounting and Information Systems University of Nevada Jacob M. Rose, Ph.D. Professor Department of Accounting and Finance University of New Hampshire Carolyn Strand Norman, Ph.D., CPA Professor Department of Accounting Virginia Commonwealth University JOHN WILEY & SONS, INC. VICE PRESIDENT & PUBLISHER SENIOR ACQUISITIONS EDITOR PROJECT EDITOR ASSOCIATE EDITOR SENIOR EDITORIAL ASSISTANT PRODUCTION MANAGER PRODUCTION EDITOR MARKETING MANAGER CREATIVE DIRECTOR SENIOR DESIGNER PRODUCTION MANAGEMENT SERVICES SENIOR ILLUSTRATION EDITOR PHOTO EDITOR MEDIA EDITOR COVER PHOTO George Hoffman Michael McDonald Brian Kamins Sarah Vernon Jacqueline Kepping Dorothy Sinclair Erin Bascom Karolina Zarychta Harry Nolan Wendy Lai Laserwords Maine Anna Melhorn Elle Wagner Greg Chaput Maciej Frolow/Brand X/Getty Images, Inc. This book was set in 10/12pt Garamond by Laserwords Private Limited, and printed and bound by RR Donnelley/Jefferson City. The cover was printed by RR Donnelley/Jefferson City. This book is printed on acid free paper. Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and understanding for more than 200 years, helping people around the world meet their needs and fulfill their aspirations. Our company is built on a foundation of principles that include responsibility to the...

Words: 241803 - Pages: 968