Professional Documents
Culture Documents
Vbasql PDF
Vbasql PDF
Guy Yollin
Instructor, Applied Mathematics
University of Washington
Guy Yollin (Copyright 2012)
1 / 45
Outline
Introduction to QueryTables
2 / 45
Outline
Introduction to QueryTables
3 / 45
Section references
4 / 45
5 / 45
Destination
Sql
6 / 45
7 / 45
CommandText
CommandType
TextFileParseType
TextFileCommaDelimiter
Methods
Refresh
8 / 45
9 / 45
Outline
Introduction to QueryTables
10 / 45
Section references
Simon Beginninga
Financial Modeling, 3rd Edition
Massachusetts Institute of Technology, 2008
Chapter 41
J. Green, S. Bullen, R. Bovey, M. Alexander
Excel 2007 VBA Programmers Reference
Wiley, 2007
Chapter 21
11 / 45
12 / 45
13 / 45
Parameter
Description
ticker symbol
fromMonth - 1
fromdat (2 digits)
fromYear
toMonth - 1
toDay (2 digits)
toYear
d=day, m=month
Data Access with SQL, Excel & VBA
14 / 45
15 / 45
16 / 45
17 / 45
18 / 45
Outline
Introduction to QueryTables
19 / 45
Section references
20 / 45
21 / 45
ODBC Setup
22 / 45
23 / 45
24 / 45
(Copyright 2012)
25 / 45
26 / 45
27 / 45
Outline
Introduction to QueryTables
28 / 45
Section references
John Walkenbach
Excel 2010 Power Programming with VBA
Sams, 2010
Chapter 8
29 / 45
S0 = spot price
X = strike price
= volatility
T = time to expiration
r = risk-free rate
p = Xe rT N(d2 ) S0 N(d1 )
Where:
ln( SX0 ) + (r + 2 /2)T
d2 = d1 T
d1 =
30 / 45
Black-Shoales formula
P u b l i c F u n c t i o n B l a c k S c h o l e s ( C a l l P u t F l a g As S t r i n g , _
S As Double , X As Double , T As Double , r As Double , _
v As Double ) As Double
Dim d1 As Double , d2 As Double
d1 = ( Log ( S / X) + ( r + v ^ 2 / 2 ) T) / ( v Sqr (T ) )
d2 = d1 v Sq r (T)
I f CallPutFlag = "c"
BlackScholes = S
E l s e I f CallPutFlag =
BlackScholes = X
End I f
Then
CND( d1 ) X Exp( r T) CND( d2 )
" p " Then
Exp( r T) CND(d2 ) S CND(d1 )
End F u n c t i o n
Source: http://www.espenhaug.com/black_scholes.html
Guy Yollin (Copyright 2012)
31 / 45
N(x ) =
1 N (x )(a1 k + a2 k 2 + a3 k 3 + a4 k 4 + a5 k 5 ) when x 0
1 N(x )
when x < 0
0
where
k=
1
1+x
= 0.2316419
a1 = 0.319381530
a2 = 0.356563782
a3 = 1.781477937
a4 = 1.821255978
a5 = 1.330274429
Guy Yollin (Copyright 2012)
32 / 45
Black-Shoales formula
' // The c u m u l a t i v e n o r m a l d i s t r i b u t i o n f u n c t i o n
P u b l i c F u n c t i o n CND(X As Double ) As Double
Dim L
Const
Const
Const
As
a1
a3
a5
Double , K As Double
= 0 . 3 1 9 3 8 1 5 3 : C o n s t a2 = 0. 35 65 6 3 78 2:
= 1 . 7 8 1 4 7 7 9 3 7 : C o n s t a4 = 1. 82 12 55 97 8:
= 1.330274429
L = Abs (X)
K = 1 / (1 + 0.2316419 L)
CND = 1 1 / Sqr ( 2 A p p l i c a t i o n . P i ( ) ) Exp(L ^2/2) _
( a1 K + a2 K^2 + a3 K^3 + a4 K^4 + a5 K^5)
I f X < 0 Then
CND = 1 CND
End I f
End F u n c t i o n
33 / 45
34 / 45
You can use many (but not all) of Excels worksheet functions in your
VBA code
The WorksheetFunction object, which is contained in the
Application object, holds all the worksheet functions that you can
call from your VBA procedures
To use a worksheet function in a VBA statement, just precede the
function name with the object reference:
Application.WorksheetFunction
35 / 45
Black-Shoales formula
P u b l i c F u n c t i o n B l a c k S c h o l e s V e r 2 ( C a l l P u t F l a g As S t r i n g , _
S As Double , X As Double , T As Double , r As Double , _
v As Double ) As Double
Dim d1 As Double , d2 As Double
d1 = ( Log ( S / X) + ( r + v ^ 2 / 2 ) T) / ( v Sqr (T ) )
d2 = d1 v Sq r (T)
I f C a l l P u t F l a g = " c " Then
B l a c k S c h o l e s V e r 2 = S W o r k s h e e t F u n c t i o n . NormSDist ( d1 ) _
X Exp( r T) W o r k s h e e t F u n c t i o n . NormSDist ( d2 )
E l s e I f C a l l P u t F l a g = " p " Then
B l a c k S c h o l e s V e r 2 = X Exp( r T) _
W o r k s h e e t F u n c t i o n . NormSDist(d2 ) _
S W o r k s h e e t F u n c t i o n . NormSDist(d1 )
End I f
End F u n c t i o n
Guy Yollin (Copyright 2012)
36 / 45
Outline
Introduction to QueryTables
37 / 45
Section references
John Walkenbach
Excel 2010 Power Programming with VBA
Sams, 2010
Chapter 18
Duane Birnbaum and Michael Vine
Excel VBA Programming for the Absolute Beginner, 3rd Edition
Thomson Course Technology, 2007
Chapter 9
38 / 45
Delete legend
39 / 45
Recorded macro
Sub R e c o r d P l o t R e t u r n s ( )
A c t i v e S h e e t . S h a p e s . AddChart . S e l e c t
ActiveChart . SetSourceData
S o u r c e := Range ( " ' msft ' ! $A$1 : $A$17 , ' msft ' ! $H$1 : $H$17 " )
A c t i v e C h a r t . ChartType = x l C o l u m n C l u s t e r e d
A c t i v e C h a r t . Axes ( x l C a t e g o r y ) . S e l e c t
A c t i v e S h e e t . C h a r t O b j e c t s ( " C h a r t 10 " ) . A c t i v a t e
S e l e c t i o n . T i c k L a b e l P o s i t i o n = xlLow
A c t i v e S h e e t . C h a r t O b j e c t s ( " C h a r t 10 " ) . A c t i v a t e
A c t i v e C h a r t . Legend . S e l e c t
Selection . Delete
End Sub
Hard-coded range
Hard-coded chart name (will not be correct the next time)
Nothing recorded regarding the text alignment
Guy Yollin (Copyright 2012)
40 / 45
To-Do:
align x-axis labels
add a y-axis label
change the title
change chart position
Guy Yollin (Copyright 2012)
41 / 45
42 / 45
43 / 45
44 / 45
http://depts.washington.edu/compfin
45 / 45