Financial Data Access with SQL, Excel & VBA



Similar documents
Microsoft Excel 2013: Macro to apply Custom Margins, Titles, Gridlines, Autofit Width & Add Macro to Quick Access Toolbar & How to Delete a Macro.

MS Excel. Handout: Level 2. elearning Department. Copyright 2016 CMS e-learning Department. All Rights Reserved. Page 1 of 11

Integrating Microsoft Word with Other Office Applications

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Programming in Access VBA

USC Marshall School of Business Marshall Information Services

Microsoft Excel Basics

Automate tasks with Visual Basic macros

Word 2010: Mail Merge to with Attachments

Excel macros made easy

Microsoft Access 2010 handout

Excel & Visual Basic for Applications (VBA)

Excel 2003 Tutorial I

Spreadsheet - Introduction

3 What s New in Excel 2007

Migrating to Excel 2010 from Excel Excel - Microsoft Office 1 of 1

Microsoft' Excel & Access Integration

Basic Microsoft Excel 2007

Advanced Presentation Features and Animation

Excel Reports User Guide

How to Use Excel for Law Firm Billing

Excel Project Creating a Stock Portfolio Simulation

Advanced Microsoft Excel 2010

Microsoft Excel Training - Course Topic Selections

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

Introduction to Microsoft Access 2003

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

In this session, we will explain some of the basics of word processing. 1. Start Microsoft Word 11. Edit the Document cut & move

Excel 2007 A Beginners Guide

Appendix A How to create a data-sharing lab

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

Microsoft Access 2010 Part 1: Introduction to Access

Analyzing Excel Data Using Pivot Tables

STC: Descriptive Statistics in Excel Running Descriptive and Correlational Analysis in Excel 2013

Task Force on Technology / EXCEL

WEBFOCUS QUICK DATA FOR EXCEL

Tommy B. Harrington 104 Azalea Drive Greenville, NC

EXCEL VBA ( MACRO PROGRAMMING ) LEVEL SEPTEMBER AM-5.00PM MENARA PJ@AMCORP PETALING JAYA

Planning and Managing Projects with Microsoft Project Professional 2013

Excel 2007 Basic knowledge

Basic Excel Handbook

Writing Macros in Microsoft Excel 2003

Macros in Word & Excel

Computer Skills Microsoft Excel Creating Pie & Column Charts

Microsoft Excel Introduction to Microsoft Excel 2007

How To Create A Report In Excel

Getting Started with Excel Table of Contents

ECDL. European Computer Driving Licence. Spreadsheet Software BCS ITQ Level 2. Syllabus Version 5.0

Excel 2010: Create your first spreadsheet

Intro to Excel spreadsheets

INTRODUCTION 5 COLLABORATION RIBBON 5 SELECT THE UPDATING METHOD 6 MAKE YOUR PROJECT COLLABORATIVE 8 PROCESSING RECEIVED TASK UPDATES 9

Excel Reports and Macros

Construction Administrators Work Smart with Excel Programming and Functions. OTEC 2014 Session 78 Robert Henry

Microsoft Excel 2013: Using a Data Entry Form

Import Data to Excel Start Excel.

Excel 2003 A Beginners Guide

Rows & Columns. Workbooks & Worksheets

Windows 10: A Beginner s Guide

Part A: Introduction to Excel VBA

Microsoft Office 2007 Orientation Objective 1: Become acquainted with the Microsoft Office Suite 2007 Layout

EXCEL Tutorial: How to use EXCEL for Graphs and Calculations.

NAVIGATION TIPS. Special Tabs

Microsoft Office Access 2007 Basics

Explore commands on the ribbon Each ribbon tab has groups, and each group has a set of related commands.

Software Application Tutorial

Basic Pivot Tables. To begin your pivot table, choose Data, Pivot Table and Pivot Chart Report. 1 of 18

Sample- for evaluation purposes only! Advanced Excel. TeachUcomp, Inc. A Presentation of TeachUcomp Incorporated. Copyright TeachUcomp, Inc.

MICROSOFT OFFICE ACCESS NEW FEATURES

To create a histogram, you must organize the data in two columns on the worksheet. These columns must contain the following data:

Advanced Excel Charts : Tables : Pivots : Macros

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Excel 2007: Basics Learning Guide

Microsoft Excel 2010 Training

HOUR 9. Formatting Worksheets to Look Great

Creating Web Pages With Dreamweaver MX 2004

As in the example above, a Budget created on the computer typically has:

Plotting: Customizing the Graph

Overview of sharing and collaborating on Excel data

RIT Installation Instructions

Q&As: Microsoft Excel 2013: Chapter 2

Analyzing Data Using Excel

Microsoft Office Access 2007 which I refer to as Access throughout this book

Content Author's Reference and Cookbook

Working with Macros and VBA in Excel 2007

Sage Intelligence Report Designer Add-In

Highline Excel 2016 Class 26: Macro Recorder

Word 2007: Basics Learning Guide

Microsoft Office Word 2010: Level 1

MS WORD 2007 (PC) Macros and Track Changes Please note the latest Macintosh version of MS Word does not have Macros.

User Services. Intermediate Microsoft Access. Use the new Microsoft Access. Getting Help. Instructors OBJECTIVES. July 2009

Basics. a. Click the arrow to the right of the Options button, and then click Bcc.

Microsoft Word 2010 Prepared by Computing Services at the Eastman School of Music July 2010

2. Building Cross-Tabs in Your Reports Create a Cross-Tab Create a Specified Group Order Filter Cross-Tab by Group Keep Groups Together

This chapter is completely devoid of any hands-on training material. It

Excel for Data Cleaning and Management

Microsoft Excel 2010 Part 3: Advanced Excel

How to Excel with CUFS Part 2 Excel 2010

How To Create A Powerpoint Intelligence Report In A Pivot Table In A Powerpoints.Com

4) How many peripheral devices can be connected to a single SCSI port? 4) A) 8 B) 32 C) 1 D) 100

Instructions for Creating an Outlook Distribution List from an Excel File

Transcription:

Computational Finance and Risk Management Financial Data Access with SQL, Excel & VBA 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 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project 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 J. Green, S. Bullen, R. Bovey, M. Alexander Excel 2007 VBA Programmer s 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 Introduction to VBA 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project 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 MS Office applications that support VBA: Excel Access Word Powerpoint Outlook Almost anything you can do with an office 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 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 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 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 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 10 / 50

The Developer ribbon Make sure you can access the Developer ribbon Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 11 / 50

Macro recording example Problem A common problem is that the column width is not properly adjusted when CSV files are first opened Solution Record a macro to adjust the column widths place the macro in the PERSONAL.XLSB file (this is a place to store macros that can be shared across workbooks) create an button for the macro on the quick launch toolbar Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 12 / 50

Turn on macro recorder Click Record Macro to begin the process In the Record Macro dialog, provide a macro name and description, a shortcut key assignment (if desired), and a location to store the macro Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 13 / 50

Adjust column width Perform Excel tasks Click Stop Recording when finished Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 14 / 50

Add macro to the Quick Access Toolbar Right click the Quick Access Toolbar and select customize 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 Click the button on the Quick Access Toolbar to run macro 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

The recorded macro 1 selects all cells in the worksheet 2 select all columns and autofit the width 3 selects cell A1 Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 17 / 50

Outline 1 Introduction to VBA 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 18 / 50

Typical table formating Bold column names First row shaded background Border around table cells Column width adjusted 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 : 1 Record the actions that you want to code 2 Review the code and find the lines that perform those actions 3 Delete the rest of the code 4 Modify the recorded code 5 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

Border code from macro recorder Selection. Borders ( xldiagonaldown ). LineStyle = xlnone Selection. Borders ( xldiagonalup ). LineStyle = xlnone With Selection. Borders ( xledgeleft ). L i n e S t y l e = x l C o n t i n u o u s. C o l o r I n d e x = x l A u t o m a t i c. TintAndShade = 0. Weight = x l T h i n End With With Selection. Borders ( xledgetop ). L i n e S t y l e = x l C o n t i n u o u s. C o l o r I n d e x = x l A u t o m a t i c. TintAndShade = 0. Weight = x l T h i n End With With Selection. Borders ( xledgebottom ). L i n e S t y l e = x l C o n t i n u o u s. C o l o r I n d e x = x l A u t o m a t i c. 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 ). L i n e S t y l e = x l C o n t i n u o u s. C o l o r I n d e x = x l A u t o m a t i c. 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 ). L i n e S t y l e = x l C o n t i n u o u s. C o l o r I n d e x = x l A u t o m a t i c. TintAndShade = 0. Weight = x l T h i n End With With Selection. Borders ( xlinsidehorizontal ). L i n e S t y l e = x l C o n t i n u o u s. C o l o r I n d e x = x l A u t o m a t i c. 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 ' remove any d i a g o n a l l i n e S e l e c t i o n. B o r d e r s ( xldiagonaldown ). L i n e S t y l e = xlnone S e l e c t i o n. B o r d e r s ( x l D i a g o n a l U p ). 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. B o r d e r s ( x l E d g e L e f t ). 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 ( 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 ( xledgebottom ). 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 ( x l E d g e R i g h t ). L i n e S t y l e = x l C o n t i n u o u s ' 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. B o r d e r s ( x l I n s i d e V e r t i c a l ). 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 ( x l I n s i d e H o r i z o n t a l ). L i n e S t y l e = x l C o n t i n u o u s 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. P a t t e r n = x l S o l i d. P a t t e r n C o l o r I n d e x = x l A u t o m a t i c. ThemeColor = xlthemecolordark1. 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 ' ' S e l e c t i o n. I n t e r i o r. P a t t e r n = 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 set interior pattern to solid set TintAndShade (-1=darkest, 1=lightest) set Bold property of Font object to True Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 24 / 50

Final table formatting procedure Sub FormatTable ( ) ' s e l e c t the c u r r e n t r e g i o n A c t i v e C e l l. C u r r e n t R e g i o n. S e l e c t ' draw b o r d e r around a l l c e l l s S e l e c t i o n. B o r d e r s ( xldiagonaldown ). L i n e S t y l e = xlnone S e l e c t i o n. B o r d e r s ( x l D i a g o n a l U p ). L i n e S t y l e = xlnone 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 ). 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 ( 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 ( xledgebottom ). 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 ( x l E d g e R i g h t ). 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 ( x l I n s i d e V e r t i c a l ). 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 ( x l I n s i d e H o r i z o n t a l ). 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 the top row o f the 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 with a l i g h t g r a y background S e l e c t i o n. Font. Bold = True S e l e c t i o n. I n t e r i o r. P a t t e r n = 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 the 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 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 26 / 50

Object-oriented, event-driven paradigm Excel VBA is based on an object-oriented, event-driven paradigm Objects Properties Methods Events Object represents elements of an application a worksheet, a chart, a range 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 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 Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 27 / 50

Object-oriented, event-driven paradigm Object hierarchy Object collections Excel object classes are arranged in a hierarchy Application Workbooks collection Workbook Worksheets collection Worksheet Range Objects of the same class are grouped together in an collection object Workbooks Worksheets Charts Sheets QueryTables 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 To reference items in the object hierarchy, a period (dot) is used as a separator character parent.child object.property object.method Application.Workbooks("Book1.xlsx") Workbooks("Book1.xlsx").Worksheets(1) Worksheets("Sheet1").Range("A1").Value Range("A1").ClearContents If you omit a specific 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 member s name or its index number 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 Note the following line of code: Range ( "A1" ) = 42 Is interpreted as: A p p l i c a t i o n. ActiveWorkbook. A c t i v e S h e e t. Range ( "A1" ). Value = 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 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 33 / 50

Important Application object properties ( 300) ActiveCell ActiveSheet ActiveChart ActiveWorkbook ActiveWindow RangeSelection Selection Returns a Range object that represents the active cell Returns an object that represents the active sheet (the sheet on top) Returns a Chart object that represents the active chart Returns a Workbook object that represents the workbook in the active window (the window on top) Returns a Window object that represents the active window (the window on top) Returns a Range object that represents the selected cells Returns the selected object in the active window (Range object or chart object) see Developer s Guide to the Excel 2010 Application Object http://msdn.microsoft.com/en-us/library/office/gg192737 Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 34 / 50

Important properties and methods of the Range object Properties Value Text Formula Address Font Methods Select Copy Clear Get/set cell values (default property for Range object) Returns formatted text in a cell Get/Set formula in a cell Returns text string of cell address Returns a Font object Selects a range Copy a range deletes contents and formatting ClearContents deletes contents but leaves formatting see Developer s 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 Offset Returns a Range object that represents the cells in the specified range Worksheets("Sheet2").Cells(2, 3) Returns a Range object that represents a range that s offset from the specified range Range("A1").Offset(1, 2) 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 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project 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 Search or browser for classes, methods, properties, and events Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 38 / 50

Developer Reference From the Object Browser, you can click the question mark to open the Developer Reference to the selected section 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 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 Introduction to VBA 2 VBA macro recorder: simple example 3 VBA macro recorder: extending the simple example 4 The Excel VBA object model 5 The Application object, the Range object, the Cells property 6 The VBE, the Object Browser, the Developers Reference 7 The Colorful Stats project Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 44 / 50

The Colorful Stats project The Colorful Stats project is an introductory VBA exercise developed in chapter 1 of Excel VBA Programming for the Absolute Beginner Colorful Stats is implemented as an event procedure Event procedures are self-contained blocks of code that require some type of stimulus in order to run. The stimulus often comes directly from the user (for example, a mouse click), but may also result from another piece of code 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 the Insert Controls button on the developer ribbon Select a control (in this case an ActiveX command button) Note the Design Mode button is now depressed Click and drag on the worksheet to set the control s 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 Edit the desired properties like Caption, Name, etc. Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Introduction to VBA 47 / 50

Edit event procedure 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 Exit Design Mode and test the application 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 cmdcalculate_click ( ) ' ' 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 End ' These formulas are e nte red i n t o the new worksheet.. range ( "D2" ). Formula = "=COUNT( " & ActiveWindow. Selection. Address & " ) ". range ( "D3" ). Formula = "=MIN( " & ActiveWindow. Selection. Address & " ) ". range ( "D4" ). Formula = "=MAX( " & ActiveWindow. Selection. Address & " ) ". range ( "D5" ). Formula = "=SUM( " & ActiveWindow. Selection. Address & " ) ". range ( "D6" ). Formula = "=AVERAGE( " & ActiveWindow. Selection. Address & " ) ". range ( "D7" ). Formula = "=STDEV( " & ActiveWindow. Selection. Address & " ) " ' ' Add l a b e l s and s t a t s '. r a n g e ( "C2" ). Value = " Count : ". r a n g e ( "C3" ). Value = " Min : ". r a n g e ( "C4" ). Value = "Max : ". r a n g e ( "C5" ). Value = "Sum : ". r a n g e ( "C6" ). Value = " Average : ". r a n g e ( "C7" ). Value = " Stan Dev : ". r a n g e ( "C2 : D7" ). S e l e c t With ' ' Format the 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. Bold = True. Font. Color = RGB(232, 211, 162) ' Husky Gold. 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, 60, 116) ' Husky P u r p l e. Borders. Weight = xlthick. Borders. Color = RGB(216, 217, 218) ' 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