Excel & Visual Basic for Applications (VBA)



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

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

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

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

Word 2010: Mail Merge to with Attachments

Excel macros made easy

EXCEL FINANCIAL USES

Programming in Access VBA

What is Microsoft Excel?

Macros in Word & Excel

Pivot Tables & Pivot Charts

Automate tasks with Visual Basic macros

Excel & Visual Basic for Applications (VBA)

Analyzing Excel Data Using Pivot Tables

Presentations and PowerPoint

RIT Installation Instructions

Working with Macros and VBA in Excel 2007

13 Managing Devices. Your computer is an assembly of many components from different manufacturers. LESSON OBJECTIVES

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

Microsoft Excel 2013 Tutorial

Introduction to MS WINDOWS XP

Excel 2003: Ringtones Task

Microsoft Excel 2013: Using a Data Entry Form

Instructions for creating a data entry form in Microsoft Excel

Part A: Introduction to Excel VBA

Excel & Visual Basic for Applications (VBA)

Computer Skills Microsoft Excel Creating Pie & Column Charts

Introduction to Microsoft Access 2003

Excel 2007 Basic knowledge

Context-sensitive Help Guide

Create a Poster Using Publisher

Microsoft Access 2010 handout

Getting Started with Excel Table of Contents

Microsoft Access 2010 Part 1: Introduction to Access

Microsoft Excel Introduction to Microsoft Excel 2007

Working with Excel in Origin

Excel 2007 A Beginners Guide

EXCEL PIVOT TABLE David Geffen School of Medicine, UCLA Dean s Office Oct 2002

Excel 2007: Basics Learning Guide

SECTION 5: Finalizing Your Workbook

How to test and debug an ASP.NET application

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

Merging Labels, Letters, and Envelopes Word 2013

Using Microsoft Office to Manage Projects

Excel Database Management Microsoft Excel 2003

Managing Contacts in Outlook

Excel 2003 A Beginners Guide

Introduction to Microsoft Excel 2010

Access 2007 Creating Forms Table of Contents

Highline Excel 2016 Class 26: Macro Recorder

WINDOWS 7 EXPLORE INTERNET EXPLORER 8

Microsoft Migrating to Word 2010 from Word 2003

Step Sheet: Creating a Data Table and Charts

Financial Data Access with SQL, Excel & VBA

Tommy B. Harrington 104 Azalea Drive Greenville, NC

Advanced Excel Charts : Tables : Pivots : Macros

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

SPSS: Getting Started. For Windows

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

Microsoft Access Basics

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

warpct.com Working with MS Excel 2003 Workbook courseware by WARP! Computer Training

Task Force on Technology / EXCEL

Creating a Distribution List from an Excel Spreadsheet

Microsoft Migrating to PowerPoint 2010 from PowerPoint 2003

Microsoft Office Access 2007 Basics

Excel Formatting: Best Practices in Financial Models

Windows XP Pro: Basics 1

Using Microsoft Word. Working With Objects

InfoEd erm Project Instructions for obtaining Research Information Spreadsheets from InfoEd

Excel Tutorial. Bio 150B Excel Tutorial 1

APPLYING BENFORD'S LAW This PDF contains step-by-step instructions on how to apply Benford's law using Microsoft Excel, which is commonly used by

Excel Spreadsheets: Getting Started

Spreadsheet - Introduction

Chapter 4 Displaying and Describing Categorical Data

BIGPOND ONLINE STORAGE USER GUIDE Issue August 2005

Advanced Presentation Features and Animation

Plots, Curve-Fitting, and Data Modeling in Microsoft Excel

Introduction to SPSS 16.0

Intro to Excel spreadsheets

Staying Organized with the Outlook Journal

Excel Using Pivot Tables

HOW TO ORGANIZE PICTURES

Working together with Word, Excel and PowerPoint 2013

Call Centre Helper - Forecasting Excel Template

MAS 500 Intelligence Tips and Tricks Booklet Vol. 1

CATIA Basic Concepts TABLE OF CONTENTS

To add a data form to excel - you need to have the insert form table active - to make it active and add it to excel do the following:

Adobe Dreamweaver CC 14 Tutorial

Excel Level Two. Introduction. Contents. Exploring Formulas. Entering Formulas

Excel Companion. (Profit Embedded PHD) User's Guide

Introduction to Microsoft Excel 2007/2010

Basic Microsoft Excel 2007

Excel Reports and Macros

Introduction to ProphetX Mini User Guide

Courses never cancelled: guaranteed Last minute rescheduling 24 months access to Microsoft trainers 12+ months schedule UK wide delivery

Toad for Data Analysts, Tips n Tricks

Advanced Excel 10/20/2011 1

Excel Using Pivot Tables

Transcription:

Excel & Visual Basic for Applications (VBA) The VBA Programming Environment Recording Macros Working with the Visual Basic Editor (VBE) 1 Why get involved with this programming business? If you can't program, you will be limited by Excel's built-in capabilities. These are many, but Excel will be dictating to you what you can do. Programming lets you turn the tables. If you can't program, your use of Excel will be inefficient. Programming allows you to automate repetitive activities and cut down on your work time. Time is money, etc. We live in a competitive world. If you can't program in Excel, someone else will be able to. Their work will be better than yours. They will beat you out. 2 1

What is VBA? It is the programming language embedded in Excel. What isn t VBA? It is not Visual Basic. Visual Basic is a separate, software package that is used for developing stand-alone Windows applications. It is very different from VBA. How long has VBA been around? Microsoft introduced VBA with Excel Version 5.0 in late 1993, but, only recently, has begun to be used by everyday users. Isn t VBA a toy language when compared to C/C++ and Fortran 90? No way. VBA is a full-featured, object-oriented language. We can only give a solid introduction to VBA in this course. 3 What do we use VBA for? to automate repetitive activities in Excel and streamline the use of Excel for you or for users of your spreadsheet projects to expand Excel's capabilities to have it do what you need it to do, rather than you having to do what it is built to do to open windows of access to other software programs to allow for convenient input and output ("I/O") of information to Excel spreadsheet projects 4 2

So, what do you have to learn? That depends. If you already have a good programming background in another computer language [ C/C++, Fortran, Pascal, Basic ], there is good and bad news. Good news: the programming fundamentals you have learned will transfer nicely over to VBA Bad news: VBA has some curve balls for you that may cause you to re-learn certain things If you have little or no programming background, there is also good and bad news. Bad news: you have a lot to learn Good news: you don t have any bad habits to break You have to learn the fundamentals of programming, including data structure, algorithm structure, modular construction and I/O. 5 Data structure data types: integer, real, string, logical structures: arrays objects: classifications, properties and methods Algorithm structure syntax, sequence, selection, repetition modular programming: procedures and functions Input/Output and Special Features user forms event handlers 6 3

How does VBA work with Excel? Excel workbook VBA Project Option A OK Option B Option C Option A OK Option B Option C worksheets with cells chart sheets with graphs modules with procedures and functions userforms with associated VBA code Excel Application Spreadsheet environment Spreadsheet world VBA environment VBA world 7 The Visual Basic Editor (VBE) is used to manage the VBA environment launching the VBE Alt-F11 shortcut The Visual Basic Editor opens in a separate window. 8 4

Project Explorer Properties Window Code Window VBE Window 9 How to switch back to the Excel workbook window? Alt-F11 shortcut switches back and forth click on the Excel button on the toolbar click on the Excel button on the Windows taskbar at the bottom of the screen once the VBE is open, you can switch back to VBE using its button on the Windows taskbar 10 5

How does VBA code get created? 1) you write it from scratch with the VBE 2) Excel writes VBA code automatically when you run the macro recorder then you usually use the VBE to clean up and modify the automatically-written code The macro recorder can be used with no knowledge of VBA programming, but you need to know a little bit to examine, clean up and modify the recorded code. We ll illustrate the use of the macro recorder to create a shortcut for a common procedure. 11 Create a shortcut command that does the following with the contents of one or more selected cells: Why do this? because center horizontally you notice you re doing it center vertically a lot step-by-step and wrap the text within the cell want to save time. Enter a long label in a cell to use as a basis for recording the macro: Start the macro recorder from the Tools Macro menu 12 6

The Record Macro dialog box enter a name for the macro: CenterWrap decide where you want the macro to reside enter any commentary about the macro, if you want to enter a shortcut key combination, if you want the macro to have one a special workbook for storing general purpose shortcut macros [we ll use it later] typically, you will store the macro with the current workbook 13 With settings made, click OK Then, you should see the small Stop Recording toolbar somewhere on the spreadsheet and you should see the word Recording in the Status bar at the lower left of the window You can dock it at the top or bottom of the Excel window, if you want The recorder is now running and any actions you take will be written into a VBA code module 14 7

Now, you carry out the procedure to be captured by the macro recorder: Start by right-clicking on the selected cell to get the context-sensitive menu and select Format Cells... 15 Set desired format options set both horizontal and vertical fields to center set checkbox for Wrap text then click OK 16 8

click the Stop Recording button result of procedure now, try out the macro to see whether it works: enter another long label in a cell and select that cell Press Shift-Ctrl-W result 17 Will it work on several selected cells? Yes! Ctrl-Shift-W widen column B manually autoadjust row height by double-clicking between row 1 and row 2 labels 18 9

Now, let s look at the VBA code that was written [ Alt-F11 ] Select the current workbook project in the Project Explorer window Expand that branch by clicking on the + Expand the Modules folder for this project double-click Module1 to open up code 19 VBA Code Window for CenterWrap macro comments are in green VBA keywords are in blue a macro procedure is called a Sub, short for subprocedure or subroutine 20 10

VBA code is fairly easy to read and interpret (reverse-engineer) this code gets done what we wanted this is extra code that can be removed clean-up Note: VBA code is not that easy to write from scratch! 21 Use the VBE to clean up the code select the four lines and press the Delete key you can use the mouse to select the lines or a combination of the Shift and arrow keys cleaned-up macro 22 11

A little bit on the VBA syntax Name of macro created by recorder procedure starts with Sub and finishes with End Sub Comments automatically created by macro recorder With... End With is an object structure With <object>.property1 = value1.property2 = value2 End With The object here is the selection, those cells selected when the macro is run the properties and values are self-evident 23 What more can we do with the recorded macro? 1) create a button on one of the toolbars to run the macro 2) create a button directly on the spreadsheet to run the macro 3) move (or copy) the macro module to the Personal Macro workbook so it is available to all workbooks 4) move (or copy) the macro module to other workbooks as required A lesson to remember Since VBA code is difficult to write (and easy to read), you should use the macro recorder whenever your code needs to represent something you can do manually in the Excel spreadsheet environment. Recorded code modules can be incorporated into larger VBA modules. 24 12

Where to from here? We need to learn more about the VBA language: Excel objects and properties Modular elements: procedures and functions Language elements: the nuts and bolts Interchanging information between Excel and VBA Algorithm structure: sequence, selection & repetition Events and automatic procedures Custom user interfaces 25 13