+ All Categories

VBASQL

Date post: 04-Jun-2018
Category:
Upload: hassan-mehmood
View: 213 times
Download: 0 times
Share this document with a friend

of 45

Transcript
  • 8/13/2019 VBASQL

    1/45

    Computational Finance and Risk Management

    Financial Data Access

    with SQL, Excel & VBA

    Guy YollinInstructor, Applied Mathematics

    University of Washington

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 1 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    2/45

    Outline

    1 Introduction to QueryTables

    2 Downloading equity data from Yahoo Finance

    3 Read data from SQL databases

    4 Implementing VBA worksheet formulas

    5 VBA chart creation

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 2 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    3/45

    Outline

    1 Introduction to QueryTables

    2 Downloading equity data from Yahoo Finance

    3 Read data from SQL databases

    4 Implementing VBA worksheet formulas

    5 VBA chart creation

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 3 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    4/45

    Section references

    J. Green, S. Bullen, R. Bovey, M. Alexander

    Excel 2007 VBA Programmers ReferenceWiley, 2007

    Chapter 21

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 4 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    5/45

    The QueryTables collection

    The QueryTables object is a collection of QueryTable objects

    Use the Add method to create a new query table and add it to the

    QueryTables collection

    Each QueryTable object represents a worksheet table built from datareturned from an external data source

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 5 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    6/45

    The QueryTables.Add Method

    The QueryTables.Add Method creates a new query table

    Syntax:

    expression .Add(Connection, Destination, Sql)

    Connection The data source for the query table

    "ODBC;""URL;""TEXT;"

    Destination The cell in the upper-left corner of the query table

    destination range

    Sql An optional SQL query string for an ODBC data source

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 6 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    7/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 rangethat is based on the QueryTable object

    Syntax:

    expression.Refresh(BackgroundQuery)

    BackgroundQuery Used only with QueryTables that are based on theresults of a SQL query

    True to return control to the procedure as soon

    as a database connection is made and the queryis submittedFalse to return control to the procedure onlyafter all data has been fetched to the worksheet

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 7 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    8/45

    Properties and methods of the QueryTable object

    Properties

    BackgroundQuery True if queries for the query table areperformed asynchronously (in the background)

    CommandText Returns or sets the command string for thespecified data source

    CommandType The CommandType describes the value of theCommandText 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.aspxGuy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 8 / 45

    http://msdn.microsoft.com/en-us/library/office/ff198271.aspxhttp://www.r-programming.org/http://www.r-programming.org/http://msdn.microsoft.com/en-us/library/office/ff198271.aspx
  • 8/13/2019 VBASQL

    9/45

    Steps to retrieve data from an external data source

    Most data fetches will follow this basic procedure:

    1 Define the connection string

    2

    Define the destination3 Call QueryTables.Add to create the query table object

    4 Set the command (i.e. specify the data to be retrieved)

    5 Call QueryTable.Refresh to get the data

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 9 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    10/45

    Outline

    1 Introduction to QueryTables

    2 Downloading equity data from Yahoo Finance

    3 Read data from SQL databases

    4 Implementing VBA worksheet formulas

    5 VBA chart creation

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 10 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    11/45

    Section references

    Simon BeginningaFinancial Modeling, 3rd Edition

    Massachusetts Institute of Technology, 2008

    Chapter 41

    J. Green, S. Bullen, R. Bovey, M. AlexanderExcel 2007 VBA Programmers Reference

    Wiley, 2007

    Chapter 21

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 11 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    12/45

    Browser initiated download

    It is extremely easy to download historic equity data from Yahoo Finance

    directly into ExcelJust enter the following URL into a broswer:

    http://ichart.yahoo.com/table.csv?s=^GSPC

    the parameter "s=" specifies the Yahoo ticker symbol

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 12 / 45

    http://ichart.yahoo.com/table.csv?s=^GSPChttp://www.r-programming.org/http://www.r-programming.org/http://ichart.yahoo.com/table.csv?s=^GSPC
  • 8/13/2019 VBASQL

    13/45

    Browser initiated download

    Since only the ticker symbol and no date range was specified, all availabledata is downloaded

    S&P 500 Index from Jan-1950 to most recent trading day

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 13 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    14/45

    Full Yahoo finance download string

    Download S&P 500 index for 2011:http://ichart.yahoo.com/table.csv?s=^GSPC&a=0&b=01&c=2011&d=11&e=31&f=2011&g=d

    Parameter Description

    s ticker symbol

    a fromMonth - 1b 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

    http://ichart.yahoo.com/table.csv?s=^GSPC&a=0&b=01&c=2011&d=11&e=31&f=2011&g=dhttp://www.r-programming.org/http://www.r-programming.org/http://ichart.yahoo.com/table.csv?s=^GSPC&a=0&b=01&c=2011&d=11&e=31&f=2011&g=d
  • 8/13/2019 VBASQL

    15/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 orksheetDim Wq As Q u e r y T a b l eDim U r l As S t r i n gU r l = _

    "URL ; " & _" h t t p : / / i c h a r t . f i n a n c e . y a ho o . 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 tS 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 B ackgroundQuery := F a l s e

    End Sub

    adapted from S. Beginninga, Financial Modeling, 3rd Ed.Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 15 / 45

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    16/45

    Unformatted yahoo download results

    The VBA query inserts the CSV data as one-cell-per-row

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 16 / 45

    VBA d d l d Y h d

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    17/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 orksheetDim Wq As Q u e r y T a b l eDim U r l As S t r i n gU r l = _

    "TEXT; " & _" h t t p : / / i c h a r t . f i n a n c e . y a ho o . 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 tS 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 dWq.T e x t F i l e C o mm a D e l i m i t e r = TrueWq.R e f r e s h B ackgroundQuery := F a l s e

    End Sub

    adapted from S. Beginninga, Financial Modeling, 3rd Ed.Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 17 / 45

    P l f d h d l d l

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    18/45

    Properly formatted yahoo download results

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 18 / 45

    O li

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    19/45

    Outline

    1 Introduction to QueryTables

    2 Downloading equity data from Yahoo Finance

    3 Read data from SQL databases

    4 Implementing VBA worksheet formulas

    5 VBA chart creation

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 19 / 45

    S ti f

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    20/45

    Section references

    J. Green, S. Bullen, R. Bovey, M. AlexanderExcel 2007 VBA Programmers Reference

    Wiley, 2007

    Chapter 21

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 20 / 45

    D t l

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    21/45

    Data access plan

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 21 / 45

    ODBC Setup

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    22/45

    ODBC Setup

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 22 / 45

    Read from ODBC Access database

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    23/45

    Read from ODBC Access database

    Su b g o a t H i t t e r s( )Dim varConn 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

    varConn = "ODBC; DSN=la hman "

    varSQL = _

    "SELECT " & _" M a s t e r . n a m e F i r s t , M a s t e r . n am eL as t , 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 Ma s ter . p l a y er ID , Ma s ter . n a meL as t , Ma s ter . n a me Fi rs t " & _"HAVING SUM(H) > 300 0 " & _"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 Ba ck g ro u n d Qu ery := F a l s e

    End WithGuy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 23 / 45

    Read from ODBC Access database

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    24/45

    Read from ODBC Access database

    Su b goatHomers( )Dim varConn As S t r i n gDim varSQL As S t r i n g

    C e l l s.C l e a r

    varConn = "ODBC;DBQ=C: \ Pr oj ec ts \VBA\lahman591 . mdb; Dr iv er ={M ic ro so ft Acc ess Dr iv er ( .mdb)}"

    varSQL = _"SELECT " & _

    " n a m e F i r s t , n am eL a st , y e a r I D , HR " & _"FROM B a t t i n g , Ma s t e r " & _

    "WHERE Ma 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; "

    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 BackgroundQuery := F a l s e

    End With

    End Sub

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 24 / 45

    Note DSN-less ODBC connection

    Read from SQLite database via ODBC

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    25/45

    Read from SQLite database via ODBC

    Su b g o a t S t e a l e r s( )

    Dim varConn As S t r i n g

    Dim varSQL As S t r i n gC e l l s .C l e a r

    varConn = "ODBC; DSN=la hm an SQ Li t e "

    varSQL = _"SELECT " & _

    " M a s t e r . n a m e F i r s t , M a s t e r . n am eL as t , 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 Mast er . 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 Ba ck g ro u n d Qu ery := F a l s eEnd With

    End S ubGuy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 25 / 45

    Read from Access database via ADO

    http://www.r-programming.org/http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    26/45

    Read from Access database via ADO

    Su b goatHomersADO ( )

    R e qu i re s r e f e r e n c e t o M i c ro s of t A ct iv eX Data O bj e ct s xx L i b r a r y

    Dim Cn As ADODB.C o n n e c t i o n , Rs As ADODB.R e c o r d s e tDim MyConn , varSQL As S t r i n g

    Dim Rw As Long, C o l As Long, c As LongDim MyField, L o c a t i o n As Range

    C e l l s.S e l e c t

    S e l e c t i o n. C l e a rRange ( "A1") .S e l e c tS et d e s t i n a t i o nS e t L o c a t i o n = [ A1 ]S et s o u rc e

    MyConn = "C: \ Pr o j e c t s \VBA\la hman591 .mdb"C r e a t e q u e ryvarSQL = _"SELECT " & _" Master . nam eFi rst , Master . nameLast , SUM(HR) AS HomeRuns " & _"FROM B a t t i n g , Ma s t e r " & _"WHERE Ma 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 Master . pl ay er ID , Master . nam eFi rst , Master . 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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    27/45

    Read from Access database via ADO

    . . .

    C r e a t e R e c o rd S e tS e t Cn = New ADODB.C o n n e c t i o nWith Cn

    .P r o v i d e r = " Mi cr os of t . Je t .OLEDB. 4 . 0 "

    . Open MyConnS e t Rs = .E x e c u t e( varSQL )

    End With

    W ri te R ec or dS et t o r e s u l t s a re a

    Rw = L o c a t i o n. RowC o l = L o c a t i o n.Columnc = C o lDo U n t i l Rs. EOF

    F or Ea ch M yF i el d I n Rs. F i e l d sC e l l s( Rw, c ) = My F i e l dc = c + 1

    Next My F i e l dRs.MoveNext

    Rw = Rw + 1c = Co l

    LoopS e t L o c a t i o n = N o t h i n gS e t Cn = N o t h i n gEnd Sub

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 27 / 45

    Outline

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    28/45

    Outline

    1 Introduction to QueryTables

    2 Downloading equity data from Yahoo Finance

    3 Read data from SQL databases

    4 Implementing VBA worksheet formulas

    5 VBA chart creation

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 28 / 45

    Section references

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    29/45

    Section references

    John WalkenbachExcel 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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    30/45

    Black Scholes pricing formulas

    Price of a call option:

    c=S0N(d1) XerTN(d2)

    Price of a put option:

    p=XerTN(

    d2)

    S0N(

    d1)

    Where:

    d1 = ln(S0

    X) + (r+2/2)T

    T

    d2 =d1

    T

    N The cumulative normal distribution function

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 30 / 45

    S0 =spot price

    X =strike price=volatilityT =time to expirationr=risk-free rate

    Black-Shoales formula

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    31/45

    Black Shoales formula

    P u b l i c F un c ti 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 in 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 " ThenB l a c k S c h o l e s = S CND( d1 ) X Exp( r T) CND( d2 )

    E l s e I f C a l l P u t F l a g = " p " ThenB l a c k S c h o l e s = X Exp( r T) CND(d2 ) S CND(d1 )

    End I f

    End F u n c t i o n

    Source: http://www.espenhaug.com/black_scholes.htmlGuy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 31 / 45

    Polynomial approximation to normal CDF

    http://www.espenhaug.com/black_scholes.htmlhttp://www.r-programming.org/http://www.r-programming.org/http://www.espenhaug.com/black_scholes.html
  • 8/13/2019 VBASQL

    32/45

    y pp

    N(x) =

    1 N

    (x)(a1k+a2k2 +a3k3 +a4k4 +a5k5) when x 01 N(x) when x

  • 8/13/2019 VBASQL

    33/45

    // 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 un c ti o n CND(X As Double ) As Double

    Dim L As Double, K As DoubleC onst a1 = 0 . 3 1 9 3 8 1 5 3 : C onst a2 = 0.356563782:C onst a3 = 1 . 7 8 1 4 7 7 9 3 7 : C o n s t a4 = 1.821255978:C onst a5 = 1 . 3 3 0 2 7 4 4 2 9

    L = Abs (X)K = 1 / ( 1 + 0 .2 31 64 19 L )CND = 1 1 / Sqr ( 2A 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 ThenCND = 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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    34/45

    p p g

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 34 / 45

    Calling worksheet functions from VBA

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    35/45

    g

    You can use many (but not all) of Excels worksheet functions in yourVBA code

    The WorksheetFunction object, which is contained in the

    Application object, holds all the worksheet functions that you cancall from your VBA procedures

    To use a worksheet function in a VBA statement, just precede thefunction name with the object reference:

    Application.WorksheetFunction

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 35 / 45

    Black-Shoales formula

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    36/45

    P u b l i c F un c ti 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 in 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 " ThenB 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) Data Access with SQL, Excel & VBA Data access with VBA 36 / 45

    Outline

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    37/45

    1 Introduction to QueryTables

    2 Downloading equity data from Yahoo Finance

    3 Read data from SQL databases

    4 Implementing VBA worksheet formulas

    5 VBA chart creation

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 37 / 45

    Section references

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    38/45

    John WalkenbachExcel 2010 Power Programming with VBA

    Sams, 2010

    Chapter 18

    Duane Birnbaum and Michael VineExcel VBA Programming for the Absolute Beginner, 3rd Edition

    Thomson Course Technology, 2007

    Chapter 9

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 38 / 45

    Record column chart creation process

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    39/45

    1 Select A1:A17, H1:H17

    2 Click record macro

    3 Insert clustered columns chart

    4 Right click and Format Axis

    6 Change Axis Labels to Low

    7 Align text to rotate 270

    8 Delete legend

    9 Click stop recordingGuy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 39 / 45

    Recorded macro

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    40/45

    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 tA c t i v e C h a r t.S e t S o u r c e D a t aS 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.C hartT ype = x l C o l u m n C l u s t e r e dA 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 tA c t i v e S h e e t.C h a r t O b j e c t s( " C ha rt 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 = xlL owA c t i v e S h e e t.C h a r t O b j e c t s( " C ha rt 10 ") .A c t i v a t eA c t i v e C h a r t.Legend. S e l e c tS e l e c t i o n.D e l e t e

    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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    41/45

    Sub RecordedMod1( )Dim DataRange As Range

    S e t DataRange = S e l e c t i o nA c t i v e S h e e t.S h a p e s.AddChart. S e l e c tA c t i v e C h a r t.S e t S o ur c e D a ta S o u r c e := DataRangeA c t i v e C h a r t.C hartT ype = x l C o l u m n C l u s t e r e dA 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 tS 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 = xlL ow

    A c t i v e C h a r t.Legend. S e l e c tS e l e c t i o n.D e l e t e

    End Sub

    To-Do:

    align x-axis labelsadd 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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    42/45

    Su b P l o t R e t u r n s( )Dim MyChart As C h a r tDim DataRange As RangeS 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 75 , 5 , 3 50 , 2 5 0 ).C h a r tWith MyChart

    .C h a r t T i t l e. Text = " D a i l y S t oc 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 = xlLow

    .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 = " L og 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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    43/45

    Su b P l o t P r i c e s( )Dim MyChart As C h a r tDim DataRange As RangeS e t DataRange = S e l e c t i o nS 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 75 , 5 , 3 50 , 2 5 0 ) .C h a r t

    With MyChart.S e t S ou r c e Da t a S o u r ce := 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 to 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

    http://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    44/45

    Su b PlotOHLC ( )Dim MyChart As C h a r tS e t MyChart = A c t i v e S h e e t. S h a p e s.AddChart ( xlStockOHLC, 3 75 , 5 , 3 50 , 2 5 0 ).C h a r tWith MyChart

    .C h a r t S t y l e = 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 to 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 = xlLow

    .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://www.r-programming.org/http://www.r-programming.org/
  • 8/13/2019 VBASQL

    45/45

    http://depts.washington.edu/compfin

    Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 45 / 45

    http://depts.washington.edu/compfinhttp://www.r-programming.org/http://www.r-programming.org/http://depts.washington.edu/compfin