10. visual basic functions
DESCRIPTION
10. Visual Basic Functions. Open Excel. Click File -> Save As. Save As a Excel Macro-Enabled Workbook. Click File -> Options. Click Customize Ribbon. Tick Developer. Click OK. Select Developer menu. Functions. Excel uses functions to do most calculations =SUM(x) =COUNTIF( x,y ) - PowerPoint PPT PresentationTRANSCRIPT
10. Visual BasicFunctions
Open Excel
Click File -> Save As
Save As a Excel Macro-Enabled Workbook
Click File -> Options
Click Customize Ribbon
Tick Developer
Click OK
Select Developer menu
Functions
• Excel uses functions to do most calculations=SUM(x)=COUNTIF(x,y)=VLOOKUP(x,y,z)
• They take an input value e.g. x, y, z then perform a calculation and return a result
• We can write our own functions in Visual Basic
Area of a rectangle
• We want to be able to type in a function such as=RectangleArea(Height, Width)
• Excel should do the following calculationArea = Height * Width
• Excel should then give us the result
Click on Visual Basic
Click Insert -> Module
Type Function RectangleArea(Height, Width)
Press Enter on Keyboard and it will addEnd Function
This means Excel asks you for Height and WidthIt will then do calculations with these variables
Type RectangleArea = Height * Width
Click Save
Close Visual Basic editor
Click Formulas -> Insert Function
Select Category All
Scroll down to RectangleArea
Click OK
You are asked for Height and Weight
Type in Height 3 and Width 2
Click OK
The result of the function is displayed in the cell
You can also use this function by typing into the cell directly
Click cell B1
Type =RectangleArea(5,3) and press Enter
Calculates the result for you
Pricing a Futures Contract
• Traders may want to calculate futures prices
• We want Excel to calculate the Futures price by providing the Spot price, Risk-Free rate and Time
=Future(Spot, Rf, Time)
• Excel should calculate the following formulaFuture = (Spot)e(Rf)(Time)
Click Visual Basic
Type Function Future(Spot, Rf, Time) Future = Spot * exp(Rf * Time)
Save and Close
Type in the following input values
In cell D10 type =Future(D6,D7,D8) and press Enter
Calculates Futures Price
Challenge
• Create a function which will calculate the price of a share using the Dividend Growth model
• You want a user to be able to use a function where they get the price by typing in a function such as:
=DGMPrice (Dividend, InterestRate, GrowthRate)
• Excel should then do the following calculation and return the result
DGMPrice = Dividend / (InterestRate – GrowthRate)
• What is the fair price of a share if it has a dividend of £2, interest rate of 0.06, and growth rate of 0.02?