VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA...

50
VBA Programming VBA Programming for Finance for Finance Instructor : Abhijit Biswas Indian Institute of Quantitative Finance

Transcript of VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA...

Page 1: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

VBA Programming VBA Programming for Financefor Finance

Instructor : Abhijit BiswasIndian Institute of Quantitative Finance

Page 2: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Introduction toIntroduction toVBA ProgrammingVBA Programming

Page 3: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Programming Basics Programming Basics Algorithms, Pseudocode and Flow Charts Programming Languages VB and VBA Language

Page 4: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Algorithms and Flow ChartsAlgorithms and Flow ChartsElements of a flow chart Terminal Box Process Box Input and Output Box Decision Box Disk File Data store Box Flow lines Data – Constant and Variable

Page 5: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Elements of a flow chart

Page 6: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Elements of a flow chart

Page 7: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Algorithms and Flow ChartsAlgorithms and Flow Charts A flow chart for adding two user input numbers A flow chart for printing the greater between two user input numbers A flow chart for printing the greatest number among five user input numbers A flow chart for printing the greatest number among any sequence user input numbers

Page 8: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

Flow chart for printing the Factorial of a number

Page 9: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA Programming Constants Variables – memory allocation Literals Statements

– Separator – New line character– Connector – UnderscoreAmpersand character

Key words Operators

Page 10: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA Programming Logic Structures Control Structures Sub routines Functions

Page 11: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Logic StructuresVBA Logic Structures

Page 12: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Logic StructuresVBA Logic Structures

Page 13: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Logic StructuresVBA Logic Structures

Page 14: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Control StructuresVBA Control Structures Do-While Loop Do While [condition]

[statements]Exit Do

Loop

Page 15: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Control StructuresVBA Control Structures Do Loop Do

[statements]Exit Do

While [condition]

Page 16: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Control StructuresVBA Control Structures For-Next Loop For [condition]

[statements]Exit For

Next

Page 17: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA ProgrammingVBA Data Types Fundamental Data Types

– Numeric data types– Non-Numeric data types

Derived Data Types– Enumeration– Type

Page 18: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesNumeric data types are types of data that consist of numbers, which can be computed mathematically with various standard operators such as add, minus, multiply, divide and more.

Page 19: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data Types

Type Storage  Range of ValuesByte 1 byte 0 to 255Integer 2 bytes -32,768 to 32,767Long  4 bytes -2,147,483,648 to 2,147,483,648

-3.402823E+38 to -1.401298E-45 for negative values1.401298E-45 to 3.402823E+38 for positive values.-1.79769313486232e+308 to -4.94065645841247E-324 for negative values4.94065645841247E-324 to 1.79769313486232e+308 for positive values.

Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807+/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use

+/- 7.9228162514264337593543950335 (28 decimal places).Decimal 12 bytes

Single 4 bytes

Double 8 bytes

Page 20: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesByte Data Type (0 to 255)Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data type doesn't include any negative values. If you attempt to assign one, VBA returns an error.

Page 21: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesInteger Data TypeThis is probably the most common data type in use, besides String. Use this data type to store only whole numbers that range from –32,768 to 32,767.

Page 22: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesLong Data TypeThe Long data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer data type. Use Long to store values from –2,147,483,648 to 2,147,486,647.

Page 23: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesSingle Data TypeThe Single data type stores precision numbers—numbers with decimal places or fractional numbers. The data type is similar to Double, but the range is smaller. Use this data type to store values from –3402823E38 to –1.401298E–45 or from 1.401298E–45 to 3.402823E38.

Page 24: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesDouble Data TypeUse the Double data type to store precision floating point numbers from:–1.79769313486232 E 308to –4.94065645841247 E -324 OR 1.79769313486232 E 308to 4.94065645841247 E -324.

Page 25: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesCurrency Data TypeUse the Currency numeric data type to store monetary values from –922,337,203,477.5808 to 922,337,203,685,477.5807.

Page 26: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Numeric Data TypesVBA Numeric Data TypesThe Decimal Data TypeThe Decimal data type is a subtype of Variant and not a truly separate data type all its own, accommodating values from –79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 if the value contains no decimal places.

Page 27: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesNonnumeric data types are data that cannot be manipulated mathematically using standard arithmetic operators. The non-numeric data comprises text or string data types, the Date data types, the Boolean data types that store only two values (true or false), Object data type and Variant data type .

Page 28: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data Types

Data Type Storage RangeString(fixed length) Length of string 1 to 65,400 charactersString(variable length) Length + 10 bytes 0 to 2 billion charactersDate 8 bytes January 1, 100 to December 31, 9999Boolean 2 bytes True or FalseObject 4 bytes Any embedded objectVariant(numeric) 16 bytes Any value as large as DoubleVariant(text) Length+22 bytes Same as variable-length string

Page 29: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesBoolean Data Type (–1 or 0)Use the Boolean numeric data type to store logical data that contains only two values: on and off, true and false, yes and no, and so on. The keywords True and False are predefined constants and are interchangeable with the values –1 and 0, respectively.

Page 30: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesString Data TypeString is another very common data type; it stores values or numbers, but treats them as text. There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400 characters. To declare a fixed string, use the Dim statement in the formDim variablename As String * stringlength

Page 31: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesIn contrast, the variable String data type grows and shrinks as required to fit its stored value. By default, all String variables are of this type. To declare this type, use the Dim statement in the form

Dim variablename As String

Page 32: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesDate Data TypeThe Date data type stores a specially formatted numeric value that represents both the date and time. You don't have to store both the date and time value. The Date data type accepts either the date or the time, or both. Possible values range from January 1, 100 to December 31, 9999.

Page 33: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesObject Data TypeAn Object variable is actually a reference to an Access object, such as a form, report, or control. Or, the data type can reference an ActiveX component, or a class object created in a class module.

Page 34: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesVariant Data TypeThe Variant data type stores numeric and non-numeric values. This data type is the most flexible of the bunch because it stores very large values of almost any type (matches the Double numeric data type). Use it only when you're uncertain of the data's type or when you're accommodating foreign data and you're not sure of the data type's specifications.

Page 35: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesThe Variant data type is VBA's default, so the following code interprets varValue as a Variant:

Dim varValue

Page 36: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA NonVBA Non--Numeric Data TypesNumeric Data TypesAlthough the Variant data type is flexible, VBA processes these data types a little slower because it must determine the most accurate data type for the assigned value. However, most likely, you'll never notice the performance hit.

The biggest disadvantage is the data type's lack of readability. By that, we mean that you can't easily determine the appropriate data type by viewing the code, and that can be a problem.

Page 37: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA ProgrammingVariable Declaration Variable Naming ConventionVariable Scope

Page 38: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA VBA Variable DeclarationIn VBA, one needs to declare the variables before using them by assigning names and data types. They are normally declared in the general section of the codes' windows using the Dim statement. The format is as follows:

Dim Variable Name As Data Type

Page 39: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA VBA Variable DeclarationExamples:

Dim Myname As String Dim firstnum As Integer Dim total As Long Dim doDate As Date

Page 40: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA VBA Variable NamingThe following are the rules when naming the variables in Visual Basic

It must be less than 255 characters No spacing is allowed It must not begin with a number Period is not permitted

Page 41: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA VBA Variable Naming

Valid Name Invalid NameMy_Car My.Car ThisYear 1NewBoyLong_Name_Can_beUSE He&HisFather (*& is not acceptable)

Page 42: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA OperatorsVBA Operators Arithmetic Operators

– Addition– Subtraction– Multiplication– Exponentiation

String Operators– Concatenation

Page 43: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA Key wordsVBA Key words All words for data types All words for VB commands

Page 44: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA ProgrammingArrays in VBA Array data types Single dimensional Arrays Multi dimensional Arrays Dynamic Arrays How we can access arrays’ elements with control structures

Page 45: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA ProgrammingArrays and matrix algebraHow efficiently Multidimensional Array can be used to manipulate matricesCoding Correlation and Covariance Matrices

Page 46: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

User Defined FunctionsUser Defined FunctionsUser Defined Functions (UDF) can provide great power and convenience and can be very simple to write.

Page 47: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA UDFVBA UDF

Page 48: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

VBA ProgrammingVBA ProgrammingDebugging your subroutine / user defined function in VBASteppingAdd watchQuick watchTogglingClearing

Page 49: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

UDF UDF -- LimitationsLimitationsBut there are some problem areas that may need

special attention in your UDF coding:

The UDF code must be in a General Module, not a Sheet Module.

Action ignored: UDF "does nothing" Not recalculated when needed or always

recalculating. Unexpectedly returns #Value or other error. Calculates more than once in a recalculation, the

Function Wizard or when entered. Slow to calculate.

Page 50: VBA Programming for Finance - IIQFiiqf.org/digitallibrary/vba/201208/VBAforFinance.pdf · VBA Non-Numeric Data Types Although the Variant data type is flexible, VBA processes these

Indian Institute of Quantitative Finance

UDF UDF -- LimitationsLimitationsExcel will not allow a UDF written in VBA to alter anything except the value of the cell in which it is entered. You cannot make a VBA UDF which directly:Alters the value or formula or properties of another cell. Alters the formatting of the cell in which it is entered. Alters the environment of Excel. This includes the cursor. Uses FIND, SpecialCells, CurrentRegion, CurrentArray, GOTO, SELECT, PRECEDENTS etc : although you can use Range.End.Note you can use FIND in Excel 2002/2003/2007