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 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
2 / 50
Lecture references
John Walkenbach Excel 2010 Power Programming with VBA Sams, 2010 Chapter 7 - 11 J. Green, S. Bullen, R. Bovey, M. Alexander Excel 2007 VBA Programmers Reference Wiley, 2007 Chapter 1 Duane Birnbaum and Michael Vine Excel VBA Programming for the Absolute Beginner, 3rd Edition Thomson Course Technology, 2007 Chapter 1
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 3 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
4 / 50
VBA
VBA
VBA (Visual Basic for Applications) is a scripting language built into Microsoft Oce applications
Almost anything you can do with an oce application, you can automate through VBA
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 workows Analysis requiring an interface to external software
Introduction to VBA
6 / 50
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
A VBA Function procedure returns a single value (or an array) and can be called from another VBA procedure or used in a worksheet formula
Introduction to VBA
9 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
10 / 50
Solution
Record a macro to adjust the column widths place the macro in the PERSONAL.XLSB le (this is a place to store macros that can be shared across workbooks) create an button for the macro on the quick launch toolbar
Data Access with SQL, Excel & VBA Introduction to VBA 12 / 50
In the Record Macro dialog, provide a macro name and description, a shortcut key assignment (if desired), and a location to store the macro
Introduction to VBA
13 / 50
Introduction to VBA
14 / 50
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
Macro can also be run via the assigned shortcut key Ctrl+Shift+C for the AutoAdjustColumns macro
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 16 / 50
1 2 3
selects all cells in the worksheet select all columns and autot the width selects cell A1
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 17 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
18 / 50
Introduction to VBA
19 / 50
The basic process for getting started with VBA with the aid of the Macro Recorder is as follows :
1 2 3 4 5
Record the actions that you want to code Review the code and nd the lines that perform those actions Delete the rest of the code Modify the recorded code Add variables, control structures, and other code that the Macro Recorder cannot record
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
Introduction to VBA
21 / 50
remove any d i a g o n a l l i n e 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 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
Introduction to VBA
22 / 50
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
set interior pattern to solid set TintAndShade (-1=darkest, 1=lightest) set Bold property of Font object to True
Introduction to VBA
24 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
26 / 50
Properties
Methods
Objects have methods or actions they can perform Range("A1").ClearContents Events are actions recognized by an object and can be responded to by an event procedure
Events
Introduction to VBA
27 / 50
Object collections
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
If you omit a specic reference to an object, Excel uses the appropriate active object (note: it is the programmers responsibility to make sure an appropriate object is active) To refer to a member in a collection, use the members name or its index number
Introduction to VBA
30 / 50
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
Introduction to VBA
32 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
33 / 50
Methods Select Selects a range Copy Clear Copy a range deletes contents and formatting deletes contents but leaves formatting
Data Access with SQL, Excel & VBA Introduction to VBA 35 / 50
ClearContents
Cells
Returns a Range object that represents the cells in the specied range Worksheets("Sheet2").Cells(2, 3) Returns a Range object that represents a range thats oset from the specied range Range("A1").Oset(1, 2)
Oset
Both the Cells and Oset properties are useful in looping statements where the row and column inputs (osets) could be based on loop counters
Introduction to VBA
36 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
37 / 50
Object Browser
From the VBE, you can press F2 to open the Object Browser
Developer Reference
From the Object Browser, you can click the question mark to open the Developer Reference to the selected section
Introduction to VBA
39 / 50
Developer Reference
Introduction to VBA
40 / 50
Introduction to VBA
41 / 50
Introduction to VBA
42 / 50
Immediate window
From the VBE, you can press Ctrl+G to make the immediate window visible
The Immediate window is extremely useful for executing VBA statements directly and for debugging code
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 43 / 50
Outline
1 2 3 4 5 6 7
Introduction to VBA VBA macro recorder: simple example VBA macro recorder: extending the simple example The Excel VBA object model The Application object, the Range object, the Cells property The VBE, the Object Browser, the Developers Reference The Colorful Stats project
Introduction to VBA
44 / 50
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
Introduction to VBA
49 / 50
http://depts.washington.edu/compfin
Introduction to VBA
50 / 50