Professional Documents
Culture Documents
Vbasql PDF
Vbasql PDF
Guy Yollin
Instructor, Applied Mathematics
University of Washington
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 1 / 45
Outline
1 Introduction to QueryTables
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 2 / 45
Outline
1 Introduction to QueryTables
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 3 / 45
Section references
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 4 / 45
The QueryTables collection
Use the Add method to create a new query table and add it to the
QueryTables collection
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 5 / 45
The QueryTables.Add Method
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 6 / 45
The QueryTable.Refresh Method
The Refresh method causes Excel to connect to the data source of the
QueryTable object, execute the SQL query, and return data to the range
that is based on the QueryTable object
Syntax:
expression.Refresh(BackgroundQuery)
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 7 / 45
Properties and methods of the QueryTable object
Properties
BackgroundQuery True if queries for the query table are
performed asynchronously (in the background)
CommandText Returns or sets the command string for the
specified data source
CommandType The CommandType describes the value of the
CommandText property
TextFileParseType Gets/sets text file column format
TextFileCommaDelimiter True if the comma is the delimiter when you
import a text file
Methods
Refresh Updates an external data range (QueryTable)
see QueryTable Obect (Excel)
http://msdn.microsoft.com/en-us/library/office/ff198271.aspx
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 8 / 45
Steps to retrieve data from an external data source
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 9 / 45
Outline
1 Introduction to QueryTables
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 10 / 45
Section references
Simon Beginninga
Financial Modeling, 3rd Edition
Massachusetts Institute of Technology, 2008
Chapter 41
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 11 / 45
Browser initiated download
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 12 / 45
Browser initiated download
Since only the ticker symbol and no date range was specified, all available
data is downloaded
Parameter Description
s ticker symbol
a fromMonth - 1
b fromdat (2 digits)
c fromYear
d toMonth - 1
e toDay (2 digits)
f toYear
g d=day, m=month
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 14 / 45
VBA code to download Yahoo data
Sub G e t P r i c e s V e r 1 ( )
Dim Ws As W o r k s h e e t
Dim Wq As Q u e r y T a b l e
Dim U r l As S t r i n g
Url = _
"URL ; " & _
" h t t p : / / i c h a r t . f i n a n c e . yahoo . com/ t a b l e . c s v ? " & _
" s=^GSPC&" & _
" g=d&" & _
" i g n o r e =. c s v "
S e t Ws = A c t i v e S h e e t
S e t Wq = Ws . Q u e r y T a b l e s . Add ( _
C o n n e c t i o n := U r l , _
D e s t i n a t i o n :=Ws . Range ( "A1" ) )
Wq. R e f r e s h BackgroundQuery := F a l s e
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 16 / 45
VBA code to download Yahoo data
Sub G e t P r i c e s V e r 2 ( )
Dim Ws As W o r k s h e e t
Dim Wq As Q u e r y T a b l e
Dim U r l As S t r i n g
Url = _
"TEXT ; " & _
" h t t p : / / i c h a r t . f i n a n c e . yahoo . com/ t a b l e . c s v ? " & _
" s=^GSPC&" & _
" g=d&" & _
" i g n o r e =. c s v "
S e t Ws = A c t i v e S h e e t
S e t Wq = Ws . Q u e r y T a b l e s . Add ( _
C o n n e c t i o n := U r l , _
D e s t i n a t i o n :=Ws . Range ( "A1" ) )
Wq. T e x t F i l e P a r s e T y p e = x l D e l i m i t e d
Wq. T e x t F i l e C o m m a D e l i m i t e r = True
Wq. R e f r e s h BackgroundQuery := F a l s e
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 18 / 45
Outline
1 Introduction to QueryTables
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 19 / 45
Section references
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 20 / 45
Data access plan
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 21 / 45
ODBC Setup
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 22 / 45
Read from ODBC Access database
Sub g o a t H i t t e r s ( )
Dim varC on n As S t r i n g
Dim varSQL As S t r i n g
Range ( "A1" ) . C u r r e n t R e g i o n . C l e a r C o n t e n t s
varSQL = _
"SELECT " & _
" M a s t e r . n a m e F i r s t , M a s t e r . nameLast , SUM(H) AS H i t s " & _
"FROM B a t t i n g , M a s t e r " & _
"WHERE M a s t e r . p l a y e r I D=B a t t i n g . p l a y e r I D " & _
"GROUP BY M a s t e r . p l a y e r I D , M a s t e r . nameLast , M a s t e r . n a m e F i r s t " & _
"HAVING SUM(H) > 3000 " & _
"ORDER BY SUM(H) DESC ; "
With A c t i v e S h e e t . Q u e r y T a b l e s . Add ( _
C o n n e c t i o n := varConn , _
D e s t i n a t i o n := Range ( "A1" ) )
. CommandText = varSQL
. R e f r e s h B a c k g r o u n d Q u e r y := F a l s e
End With
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 23 / 45
Read from ODBC Access database
Sub goatHomers ( )
Dim va rConn As S t r i n g
Dim varSQL As S t r i n g
Cells . Clear
varSQL = _
"SELECT " & _
" n a m e F i r s t , nameLast , y e a r I D , HR " & _
"FROM B a t t i n g , M a s t e r " & _
"WHERE M a s t e r . p l a y e r I D=B a t t i n g . p l a y e r I D " & _
"AND HR>51 " & _
"ORDER BY HR DESC ; "
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 24 / 45
Read from SQLite database via ODBC
Sub g o a t S t e a l e r s ( )
Dim varC on n As S t r i n g
Dim varSQL As S t r i n g
Cells . Clear
varSQL = _
"SELECT " & _
" M a s t e r . n a m e F i r s t , M a s t e r . nameLast , SUM( SB ) AS S t o l e n B a s e s " & _
"FROM B a t t i n g , M a s t e r " & _
"WHERE M a s t e r . p l a y e r I D=B a t t i n g . p l a y e r I D " & _
"GROUP BY M a s t e r . p l a y e r I D " & _
"HAVING SUM( SB ) > 600 " & _
"ORDER BY SUM( SB ) DESC ; "
With A c t i v e S h e e t . Q u e r y T a b l e s . Add ( _
C o n n e c t i o n := varConn , _
D e s t i n a t i o n := Range ( "A1" ) )
. CommandText = varSQL
. R e f r e s h B a c k g r o u n d Q u e r y := F a l s e
End With
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 25 / 45
Read from Access database via ADO
Sub goatHomersADO ( )
' R e q u i r e s r e f e r e n c e t o M i c r o s o f t A c t i v e X Data O b j e c t s xx L i b r a r y
Cells . Select
Selection . Clear
Range ( "A1" ) . S e l e c t
' Set d e s t i n a t i o n
S e t L o c a t i o n = [ A1 ]
' Set source
MyConn = "C : \ P r o j e c t s \VBA\ lahman591 . mdb"
' Create query
varSQL = _
"SELECT " & _
" M a s t e r . n a m e F i r s t , M a s t e r . nameLast , SUM(HR) AS HomeRuns " & _
"FROM B a t t i n g , M a s t e r " & _
"WHERE M a s t e r . p l a y e r I D=B a t t i n g . p l a y e r I D " & _
"GROUP BY M a s t e r . p l a y e r I D , M a s t e r . n a m e F i r s t , M a s t e r . nameLast " & _
"ORDER BY SUM(HR) DESC ; "
...
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 26 / 45
Read from Access database via ADO
...
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 27 / 45
Outline
1 Introduction to QueryTables
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 28 / 45
Section references
John Walkenbach
Excel 2010 Power Programming with VBA
Sams, 2010
Chapter 8
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 29 / 45
Black-Scholes pricing formulas
Where:
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 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
d1 = ( Log ( S / X) + ( r + v ^ 2 / 2 ) T) / ( v Sqr (T ) )
d2 = d1 v Sq r (T)
End F u n c t i o n
Source: http://www.espenhaug.com/black_scholes.html
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 31 / 45
Polynomial approximation to normal CDF
1 N (x )(a1 k + a2 k 2 + a3 k 3 + a4 k 4 + a5 k 5 ) when x 0
( 0
N(x ) =
1 N(x ) when x < 0
where
1
k= 1+x
= 0.2316419
a1 = 0.319381530
a2 = 0.356563782
a3 = 1.781477937
a4 = 1.821255978
a5 = 1.330274429
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 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
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
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 33 / 45
Black-Shoales option pricing
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 34 / 45
Calling worksheet functions from VBA
You can use many (but not all) of Excels worksheet functions in your
VBA code
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 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
d1 = ( Log ( S / X) + ( r + v ^ 2 / 2 ) T) / ( v Sqr (T ) )
d2 = d1 v Sq r (T)
End F u n c t i o n
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 36 / 45
Outline
1 Introduction to QueryTables
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 37 / 45
Section references
John Walkenbach
Excel 2010 Power Programming with VBA
Sams, 2010
Chapter 18
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 38 / 45
Record column chart creation process
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) Data Access with SQL, Excel & VBA Data access with VBA 40 / 45
First modification to recorded macro
Sub RecordedMod1 ( )
Dim DataRange As Range
S e t DataRange = S e l e c t i o n
A c t i v e S h e e t . S h a p e s . AddChart . S e l e c t
A c t i v e C h a r t . S e t S o u r c e D a t a S o u r c e := DataRange
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
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 C h a r t . Legend . S e l e c t
Selection . Delete
End Sub
To-Do:
align x-axis labels
add a y-axis label
change the title
change chart position
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 41 / 45
Finished Plot Returns procedure
Sub P l o t R e t u r n s ( )
Dim MyChart As C h a r t
Dim DataRange As Range
S e t MyChart = A c t i v e S h e e t . S h a p e s . AddChart ( x l C o l u m n C l u s t e r e d , 3 7 5 , 5 , 3 5 0 , 2 5 0 ) . C h a r t
With MyChart
. C h a r t T i t l e . Text = " D a i l y S t o c k R e t u r n s "
. Axes ( x l C a t e g o r y ) . T i c k L a b e l s . O r i e n t a t i o n = 90
. Axes ( x l C a t e g o r y ) . T i c k L a b e l P o s i t i o n = x l L o w
. HasLegend = F a l s e
. Axes ( x l V a l u e , x l P r i m a r y ) . H a s T i t l e = True
. Axes ( x l V a l u e , x l P r i m a r y ) . A x i s T i t l e . C h a r a c t e r s . Text = " Log R e t u r n (%) "
End With
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 42 / 45
Alternative plotting method using ChartObjects
Sub P l o t P r i c e s ( )
Dim MyChart As C h a r t
Dim DataRange As Range
S e t DataRange = S e l e c t i o n
S e t MyChart = A c t i v e S h e e t . C h a r t O b j e c t s . Add ( 3 7 5 , 5 , 3 5 0 , 2 5 0 ) . C h a r t
With MyChart
. S e t S o u r c e D a t a S o u r c e := DataRange
. ChartType = x l L i n e M a r k e r s
. C h a r t T i t l e . Text = " D a i l y S t o c k P r i c e "
. Axes ( x l C a t e g o r y ) . T i c k L a b e l s . O r i e n t a t i o n = 90
. HasLegend = F a l s e
. Axes ( x l V a l u e , x l P r i m a r y ) . H a s T i t l e = True
. Axes ( x l V a l u e , x l P r i m a r y ) . A x i s T i t l e . C h a r a c t e r s . Text = " P r i c e ( $ ) "
End With
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 43 / 45
Plotting candle stick charts
Sub PlotOHLC ( )
Dim MyChart As C h a r t
S e t MyChart = A c t i v e S h e e t . S h a p e s . AddChart ( xlStockOHLC , 3 7 5 , 5 , 3 5 0 , 2 5 0 ) . C h a r t
With MyChart
. ChartStyle = 4
. H a s T i t l e = True
. C h a r t T i t l e . Text = " D a i l y S t o c k P r i c e "
. Axes ( x l C a t e g o r y ) . T i c k L a b e l s . O r i e n t a t i o n = 90
. Axes ( x l C a t e g o r y ) . T i c k L a b e l P o s i t i o n = x l L o w
. HasLegend = F a l s e
. Axes ( x l V a l u e , x l P r i m a r y ) . H a s T i t l e = True
. Axes ( x l V a l u e , x l P r i m a r y ) . A x i s T i t l e . C h a r a c t e r s . Text = " P r i c e ( $ ) "
End With
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 44 / 45
Computational Finance and Risk Management
http://depts.washington.edu/compfin
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 45 / 45