Professional Documents
Culture Documents
Guy Yollin
Instructor, Applied Mathematics
University of Washington
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 1 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 2 / 50
Lecture references
John Walkenbach
Excel 2010 Power Programming with VBA
Sams, 2010
Chapter 7 - 11
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 3 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 4 / 50
VBA
VBA
VBA (Visual Basic for Applications) is a scripting language built into
Microsoft Office applications
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 5 / 50
Usefulness of VBA
Excel is a common tool for quantitative data analysis, review, and storage
VBA can enhance Excel in the following ways:
Automation of labor-intensive tasks
Formatting tables
Creating graphs
Updating data from databases or the web
Advanced analytics
Custom worksheet function development
Analysis requiring sophisticated workflows
Analysis requiring an interface to external software
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 6 / 50
Logistics of VBA programming
VBA Coding :
The VBA code that you write (or record) is stored in a VBA module
VBA modules are stored in an Excel workbook
You use the Visual Basic Editor (VBE) to view and edit VBA code
You can use the Macro Recorder to record a sequence of user actions
and create a VBA procedure to reproduce them
The terms macro and procedure are synonymous in the VBA context
J. Walkenbach, Excel 2010 Power Programming with VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 7 / 50
Logistics of VBA programming
VBA Code :
VBA Code is structured as procedures
A VBA Sub procedure is a series of statements than can be executed
in a number of ways
macro button of the developer tab
Visual Basic Editor
shortcut key
quick access toolbar
another procedure
form control embedded on a worksheet
J. Walkenbach, Excel 2010 Power Programming with VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 8 / 50
First VBA program
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 9 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 10 / 50
The Developer ribbon
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 13 / 50
Adjust column width
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 14 / 50
Add macro to the Quick Access Toolbar
Select macros from the dropdown list, choose the desired macro,
modify the button, and click OK
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 15 / 50
Launch macro from Quick Access Toolbar
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 16 / 50
The recorded macro
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 18 / 50
Typical table formating
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 19 / 50
Macro Recorder workflow
The basic process for getting started with VBA with the aid of the Macro
Recorder is as follows :
Getting Started with VBA in Excel 2010
http://msdn.microsoft.com/en-us/library/ee814737.aspx
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 20 / 50
Border code from macro recorder
S e l e c t i o n . Borders ( xlDiagonalDown ) . L i n e S t y l e = xlNone
S e l e c t i o n . Borders ( xlDiagonalUp ) . L i n e S t y l e = xlNone
With S e l e c t i o n . B o r d e r s ( x l E d g e L e f t )
. LineStyle = xlContinuous
. ColorIndex = xlAutomatic
. TintAndShade = 0
. Weight = x l T h i n
End With
With S e l e c t i o n . B o r d e r s ( x l E d g e T o p )
. LineStyle = xlContinuous
. ColorIndex = xlAutomatic
. TintAndShade = 0
. Weight = x l T h i n
End With
With S e l e c t i o n . B o r d e r s ( x l E d g e B o t t o m )
. LineStyle = xlContinuous
. ColorIndex = xlAutomatic
. TintAndShade = 0
. Weight = x l T h i n
End With
With S e l e c t i o n . B o r d e r s ( x l E d g e R i g h t )
. LineStyle = xlContinuous
. ColorIndex = xlAutomatic
. TintAndShade = 0
. Weight = x l T h i n
End With
With S e l e c t i o n . B o r d e r s ( x l I n s i d e V e r t i c a l )
. LineStyle = xlContinuous
. ColorIndex = xlAutomatic
. TintAndShade = 0
. Weight = x l T h i n
End With
With S e l e c t i o n . B o r d e r s ( x l I n s i d e H o r i z o n t a l )
. LineStyle = xlContinuous
. ColorIndex = xlAutomatic
. TintAndShade = 0
. Weight = x l T h i n
End With
...
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 21 / 50
Significant border code
' box o u t s i d e o f s e l e c t i o n
S e l e c t i o n . Borders ( xlEdgeLeft ) . LineStyle = xlContinuous
S e l e c t i o n . B o r d e r s ( xlEdgeTop ) . L i n e S t y l e = x l C o n t i n u o u s
S e l e c t i o n . B o r d e r s ( xl EdgeBottom ) . L i n e S t y l e = x l C o n t i n u o u s
S e l e c t i o n . Borders ( xlEdgeRight ) . LineStyle = xlContinuous
' box i n t e r i o r o f s e l e c t i o n
S e l e c t i o n . Borders ( x l I n s i d e V e r t i c a l ) . LineStyle = xlContinuous
S e l e c t i o n . Borders ( x l I n s i d e H o r i z o n t a l ) . LineStyle = xlContinuous
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 22 / 50
Title bold/background from macro recorder
Sub TestMacro ( )
'
' TestMacro Macro
'
'
With S e l e c t i o n . I n t e r i o r
. Pattern = x l S o l i d
. PatternColorIndex = xlAutomatic
. ThemeColor = x l T h e m e C o l o r D a r k 1
. TintAndShade = 0.14996795556505
. PatternTintAndShade = 0
End With
S e l e c t i o n . Font . Bold = True
End Sub
Note, top row was already selected for the macro recording
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 23 / 50
Significant title bold/background code
Sub TestMacro ( )
'
' TestMacro Macro
'
'
Selection . I n t e r i o r . Pattern = x l S o l i d
S e l e c t i o n . TintAndShade = 0.14996795556505
S e l e c t i o n . Font . Bold = True
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 24 / 50
Final table formatting procedure
Sub F o r m a t T a b l e ( )
' s e l e c t the current region
Act iveCell . CurrentRegion . Select
' draw b o r d e r a r o u n d a l l c e l l s
S e l e c t i o n . Borders ( xlDiagonalDown ) . L i n e S t y l e = xlNone
S e l e c t i o n . Borders ( xlDiagonalUp ) . L i n e S t y l e = xlNone
S e l e c t i o n . Borders ( xlEdgeLeft ) . LineStyle = xlContinuous
S e l e c t i o n . B o r d e r s ( xlEdgeTop ) . L i n e S t y l e = x l C o n t i n u o u s
S e l e c t i o n . B o r d e r s ( xl EdgeBottom ) . L i n e S t y l e = x l C o n t i n u o u s
S e l e c t i o n . Borders ( xlEdgeRight ) . LineStyle = xlContinuous
S e l e c t i o n . Borders ( x l I n s i d e V e r t i c a l ) . LineStyle = xlContinuous
S e l e c t i o n . Borders ( x l I n s i d e H o r i z o n t a l ) . LineStyle = xlContinuous
' s e l e c t t h e t o p row o f t h e c u r r e n t r e g i o n
S e l e c t i o n . Rows ( 1 ) . S e l e c t
' make column t i t l e s b o l d w i t h a l i g h t g r a y b a c k g r o u n d
S e l e c t i o n . Font . Bold = True
Selection . I n t e r i o r . Pattern = x l S o l i d
S e l e c t i o n . I n t e r i o r . TintAndShade = 0.15
' a d j u s t t h e column w i d t h s
C a l l AutoAdjustColumns
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 25 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 26 / 50
Object-oriented, event-driven paradigm
Properties Objects have properties that you can get and set; properties
are attributes of an object that describe its characteristics
Range("A1").Font.Bold = True
Note that a property can return an object
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 27 / 50
Object-oriented, event-driven paradigm
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 28 / 50
Simplified Excel object model
The Excel VBA object model largely emulates the user interface
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 29 / 50
Referencing items in the hierarchy
parent.child Application.Workbooks("Book1.xlsx")
Workbooks("Book1.xlsx").Worksheets(1)
object.property Worksheets("Sheet1").Range("A1").Value
object.method Range("A1").ClearContents
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 30 / 50
Illustration of VBA object hierarchy
The Excel VBA object model largely emulates the user interface
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 31 / 50
Abbreviated references
Is interpreted as:
A p p l i c a t i o n . A c t i v e W o r k b o o k . A c t i v e S h e e t . Range ( "A1" ) . V a l u e = 42
Because:
The Value property is the default property of the Range object
ActiveSheet is assumed
ActiveWorkbook is assumed
Application is assumed
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 32 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 33 / 50
Important Application object properties (300)
Methods
Select Selects a range
Copy Copy a range
Clear deletes contents and formatting
ClearContents deletes contents but leaves formatting
see Developers Guide to the Excel 2010 Range Object
http://msdn.microsoft.com/en-us/library/office/gg192736
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 35 / 50
Other important properties
Cells Returns a Range object that represents the cells in the specified
range
Worksheets("Sheet2").Cells(2, 3)
Both the Cells and Offset properties are useful in looping statements
where the row and column inputs (offsets) could be based on loop
counters
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 36 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 37 / 50
Object Browser
From the VBE, you can press F2 to open the Object Browser
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 39 / 50
Developer Reference
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 40 / 50
Help on methods, properties, and events
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 41 / 50
The Visual Basic Editor (VBE)
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 42 / 50
Immediate window
From the VBE, you can press Ctrl+G to make the immediate window
visible
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 43 / 50
Outline
1 Introduction to VBA
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 44 / 50
The Colorful Stats project
D. Birnbaum, Excel VBA Programming for the Absolute Beginner
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 45 / 50
Add a control to a worksheet
Click and drag on the worksheet to set the controls size and location
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 46 / 50
Edit control properties
Click the Controls Property button (or double click on the control) to
open the Properties dialog
Click View Code (or double click the control) to open the VBE
Select the type of event to respond to (e.g. click) and edit the code
for the event procedure
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 48 / 50
Colorful Stats source code
P r i v a t e Sub c m d C a l c u l a t e _ C l i c k ( )
'
' Add f o r m u l a s f o r summary s t a t s
'
With A c t i v e S h e e t
' These f o r m u l a s a r e e n t e r e d i n t o t h e new w o r k s h e e t .
. r a n g e ( "D2" ) . F o r m u l a = "=COUNT( " & ActiveWindow . S e l e c t i o n . A d d r e s s & " ) "
. r a n g e ( "D3" ) . F o r m u l a = "=MIN( " & ActiveWindow . S e l e c t i o n . A d d r e s s & " ) "
. r a n g e ( "D4" ) . F o r m u l a = "=MAX( " & ActiveWindow . S e l e c t i o n . A d d r e s s & " ) "
. r a n g e ( "D5" ) . F o r m u l a = "=SUM( " & ActiveWindow . S e l e c t i o n . A d d r e s s & " ) "
. r a n g e ( "D6" ) . F o r m u l a = "=AVERAGE( " & ActiveWindow . S e l e c t i o n . A d d r e s s & " ) "
. r a n g e ( "D7" ) . F o r m u l a = "=STDEV( " & ActiveWindow . S e l e c t i o n . A d d r e s s & " ) "
'
' Add l a b e l s and s t a t s
'
. r a n g e ( " C2 " ) . V a l u e = " Count : "
. r a n g e ( " C3 " ) . V a l u e = " Min : "
. r a n g e ( " C4 " ) . V a l u e = "Max : "
. r a n g e ( " C5 " ) . V a l u e = "Sum : "
. r a n g e ( " C6 " ) . V a l u e = " A v e r a g e : "
. r a n g e ( " C7 " ) . V a l u e = " S t a n Dev : "
. r a n g e ( " C2 : D7" ) . S e l e c t
End With
'
' Format t h e l a b e l s and s t a t s .
'
With S e l e c t i o n
. Font . S i z e = 16
. Font . B o l d = True
. Font . C o l o r = RGB( 2 3 2 , 2 1 1 , 1 6 2 ) ' Husky G o l d
. Font . Name = " A r i a l "
. Columns . A u t o F i t
. I n t e r i o r . C o l o r = RGB( 5 4 , 6 0 , 1 1 6 ) ' Husky P u r p l e
. B o r d e r s . Weight = x l T h i c k
. B o r d e r s . C o l o r = RGB( 2 1 6 , 2 1 7 , 2 1 8 ) ' Husky Gray
End With
r a n g e ( "A1" ) . S e l e c t
End Sub
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 49 / 50
Computational Finance and Risk Management
http://depts.washington.edu/compfin
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 50 / 50