[Slides notes] Financial Modeling using VBA
来源:互联网 发布:淘宝天机平台wifi 编辑:程序博客网 时间:2024/05/21 06:36
Initialization:
1. Left click Office Button
, Left click Excel Options
. Check Show Developer tab in the Ribbon
. Left click OK
.
2. Select Developer tab
. Left click on Macro Security
.
3. Check Enable all macros and Trust access to the the VBA project model. Left click OK
.
- Macro Recorder
- Object
- Basics
- Variables
- Constants
- Operators
- Statements
- Array
- Sub Function
- Built-In Function
Macro Recorder
Press Alt + F8
to look over macro, to edit or run it.
- Record your actions in VBA
- Under the
Developer Tab
, left click onRecord Macro
. Name the record and clickOK
. - Begin record the actions in VBA and left click the mouse button on
Stop Recording
- Under the
- Assign marco to button
- Select
Developer tab
, left click onInsert
. And selectButton
- Left click mouse button anywhere, and draw a button.
- In the
Assign Macro
dialogue, select a function and clickOK
. - Name the button.
- Select
- Modifying macro code
Select the cell and assign the value. Or assign the value directly. - Absolute and Relative Reference
''''Absolute ReferenceSub EnterValues()'' EnterValues Macro' Macro recorded 7/21/2008 by CD Shum' Range("B40").Select ActiveCell.FormulaR1C1 = "ABC" Range("B41").Select ActiveCell.FormulaR1C1 = "DEFG" Range("B42").Select ActiveCell.FormulaR1C1 = "100" Range("B43").Select ActiveCell.FormulaR1C1 = "200" Range("B44").Select ActiveCell.FormulaR1C1 = "=R[-1]C" Range("B45").SelectEnd Sub
''''Relative ReferenceSub EnterValuesRelative()'' EnterValuesRelative Macro' Macro recorded 7/21/2008 by CD Shum' ActiveCell.Offset(0, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "ABC" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "DEFG" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "100" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "200" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "=R[-1]C" ActiveCell.Offset(1, 0).Range("A1").SelectEnd Sub
Object
Help: Alt + F11
to go to Microsoft Visual Basic. F1
to go to Microsoft Visual Basic Help. Excel Help
, left click Excel Object Model Reference
.
Basics
- Invoke Visual Basic Editor
PressAlt + F11
or under Developer tab, left clickVisual Basic
. - Project Explorer Window
If not visible, selectView
–>Project Explorer
.
Code in Microsoft Excel Objects (e.g. Sheet1) or Modules (e.g. Module 1)
Double click left mouse button on Module 1 to view code.
To insert a module, selectInsert
–>Module
.
To remove a module, select the module, right click and selectRemove Module
. - Entering Code
In theProject Explorer
Window, double click left mouse on a sheet to view code.
Each Sub and End Sub is a procedure. - Getting help
MsgBox is a VBA built-in function. To get help on MsgBox, in the Code Window, right click mouse button on MsgBox. Select Definition. Left click mouse button on ?. - Adding Comment
Text after (‘) is a comment. - Line Continuation
A statement is on a line.
Continue a statement in the next line by putting a space followed by an underscore(_) at the end.
Variables
- Variables: Named storage.
- Naming Variables: Case insensitive. E.g., “aBC” is the same as “abc”.
Cannot use space( ), period(.), exclamation mark(!) or @, &, $, and #. Also, cannot use key words reversed by VBA (e.g., Dim, Integer, Sub).
Best Practice: Variable names should be descriptive(e.g. SpotPrice, StrikePrice) - Data Types:
- Scope:
- Procedure-Level: Declared within procedure using Dim. Available only within procedure of declaration. Variables not explicitly declared are treated as procedure-level variables.
- Static: Declared within procedure using Static. Available as long as code (project) is running (even after procedure has ended).
- Module-Level: Declared before 1st procedure of module using Dim or Private. Available as long as code (project) is running.
- Project-Level: Declared using Public at the beginning (before 1st procedure) of one of the modules. Available to all procedures as long as code (project) is running.
Constants
- Date Constants
- Color Constants
- MsgBox Constants
- VarType Constants
Operators
- Common Operators
- Comparison Operators
Statements
- If … Then … Else
If condition Then [statements][Else elsestatements]
or
If condition Then [statements][ElseIf condition Then [elseifstatements]...[Else [elsestatements]]End If
- Select Case
Select Case testexpression [Case expressionslist [statements]]... [Case Else [elsestatements]]End Select
- For … Next
For counter = start To end [Step step] [statements] [Exit For] [statements]Next[counter]
- For Each … Next
For Each element In group [statements] [Exit For] [statements]Next[element]
- Do … Loop
Do [{While | Until} condition] [statements] [Exit Do] [statements]Loop
Or
Do [statements] [Exit Do] [statements]Loop [{While | Until} condition]
Array
fixed-size array: known fixed storage
dynamic array:dynamic storage, can be reallocated using ReDim or released using Erase
- Dim
Dim [Preserve] varname(subscripts) [As type][,varname(subscripts)[As type]]...
e.g., fixed-size array
e.g., dynamic array
- ReDim:
ReDim [Preserve] varname (subscripts) [As type][, varname(subscripts) [As type]]...
Preserve: preserve data in an existing array, can resize only the last array dimension, can only change the upper bound.
without Preserve: numeric variable is initialized to 0, variable-length string is initialized to zero-length string (“”), fix-length string is filled with zeros. Variant variable are initialized to Empty, object variable has special value Nothing.
Erase
fixed-size: reinitialize, recover no memory
dynamic: frees memoryArray Copy
- Cannot assign to fixed-size array
- Assign to dynamic OK:
- Assign to Variant OK:
Sub fixedArray() Dim iFA(10) As Integer 'initialized to 0 Dim i As Integer For i = 0 To 10 iFA(i) = i Next i 'ReDim iFA(20) 'uncomment and compile to see error Erase iFA Dim fsA(5) As String * 5 'initialized to 0 Dim b() As Byte b = fsA(0)End Sub
Sub dynamicArray() Dim sDA() As String ReDim sDA(1, 1) 'initialized to "" sDA(0, 0) = "element(0,0)": sDA(0, 1) = "element(0,1)" sDA(1, 0) = "element(1,0)": sDA(1, 1) = "element(1,1)" ReDim Preserve sDA(1, 2) 'ReDim Preserve sDA(2, 2) 'uncomment and run to see error ReDim sDA(2, 2) 'all reinitialized to "" Erase sDA 'release dynamic storageEnd Sub
/// Do not use Variant often, since the complier can not find some type mistake by unknown types
Sub & Function
- Sub
[Private | Public][Static] Sub name [(arglist)] [statements] [Exit Sub] [statements]End Sub
- Function
[Public | Private][Static] Function name [(arglist)][As type] [statements] [name = expression] [Exit Function] [statements] [name = expression]End Function
A sub is a procedure return nothing, a function return something with using [As type].
Built-In Function
In VBA, there are types of build-in function you can use. Press Alt + F11
to go to Microsoft Visual Baisc. In the standard toolbar, left click mouse button on Object Browser
.
- [Slides notes] Financial Modeling using VBA
- [Slides Notes] Financial Modeling using Excel
- [Slides notes] Other notes using Excel
- Credit Risk Modeling using Excel and VBA
- Using ViewPager for Screen Slides
- Financial Modeling with Crystal Ball and Excel
- Some notes about Class Modeling
- VBA中调用Notes
- vba 启动 notes
- VBA Study Notes
- Financial Instrument Pricing Using C++
- adding animations - Using ViewPager for Screen Slides
- [Review Notes] Introduction to Financial Computing
- Structured Finance Modeling with Object-Oriented VBA
- Ubuntu 13.04 Using Notes
- Environmental Variables using VBA
- (OK) Link modeling using ns-3
- Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
- 对话框
- python学习--之dict
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))B. Mischievous Mess Makers(贪心)
- zookeeper集群的安装
- sql的一些特殊用法
- [Slides notes] Financial Modeling using VBA
- HashMap和ConcurrentHashMap的对比
- ios .a静态库和framework静态库的创建以及使用
- muduo网络库脉络分析(1)
- 程序员赴美就业交流活动 2016.3.27 13:00
- Problem H
- 发布“陈氏太极老架”APP到APPSTORE流程 【2016年3月版】
- Android Clipboard(复制/剪贴板)
- 经典算法整理之快速排序