Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.

Slides:



Advertisements
Similar presentations
Introduction to Macro Introduction to Visual Basic for Application Recording a Macro Looking at the code of Recorded Macro.
Advertisements

Naming in Excel. Excel Objects and Names Every Excel object is contained in a workbook A workbook is divided into worksheets (individual spreadsheets);
Creating a User Form. A Better Interface Our programs can use input boxes for input and write on the spreadsheet page for output This works ok but is.
Overview Lesson 1. Objectives Step-by-Step: Start Excel 1.Click the Start menu, and then click All Programs. 2.On the list of programs, click Microsoft.
Using Macros and Visual Basic for Applications (VBA) with Excel
Tutorial 7: Developing an Excel Application
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Copyright © 2008 Pearson Prentice Hall. All rights reserved Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next.
Microsoft Office Excel 2013 Core Microsoft Office Excel 2013 Core Courseware # 3253 Lesson 8: Macros, Importing and Exporting Data.
MACROS CS1100 Computer Science and its Applications CS11001.
Microsoft Excel 2010 Chapter 7
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
Lecture Excel: Multiple Worksheets. Workbook and Worksheets Multiple worksheets in a single workbook. When saved, only a single workbook (XLS) is saved.
Microsoft Excel 2003 Illustrated Complete Excel and Advanced Worksheet Management Customizing.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Macros in Excel Intro to lab 1. Macroinstructions Macro is recorded in VBA module sequence of Excel operations Macros can automate tasks in Excel Macro.
Excel Lesson 14 Creating and Using Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Designing a Classroom Web Site Using NVU Beginning Level.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #2: The Visual Basic Editor The Visual Basic for Applications Programming Language IE 212: Computational.
© Commonwealth of Australia 2009 Using OneNote in the Classroom.
Using Backstage Lesson 2. Objectives Software Orientation: Backstage View Backstage view’s left-side navigation pane (see figure on the next slide) gives.
Macros n Macros are little programs that you can create to automate particular tasks that you may want to execute more easily than having to specify all.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
1 CA201 Word Application Increasing Efficiency Week # 13 By Tariq Ibn Aziz Dammam Community college.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Microsoft Excel Macros & Excel Solver (IENG490)
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Lecture Excel: Macros & Pivot Tables. Macros A macro is a series of commands that are stored and can be run whenever you need to perform the task.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
This is the first sheet of a spreadsheet workbook. The workbook begins initially with 3 work sheets. A spreadsheet is made of columns and rows. The intersection.
Fall 2005 Using FrontPage to Enhance Blackboard - Darek Sady1 Using FrontPage to Enhance Blackboard 1.Introduction 2.Starting FrontPage 3.Creating Documents.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic for Applications Macro Programming For Microsoft Office.
Excel CREATING A WORKSHEET AND CHART. Personal Budget Worksheet We will create a personal budget worksheet that shows you income each month and your expenses.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Lesson 2 Manipulating.
LOGO Chapter II Entering Excel Formulas and Formatting Data Friday, November 20, 2015.
The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Introduction to Microsoft Excel Macros COE 201- Computer Proficiency.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Instructions for using this template. Remember this is Jeopardy, so where I have written “Answer” this is the prompt the students will see, and where.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
Excel Tutorial 8 Developing an Excel Application
International Computer Driving Licence Syllabus version 5.0
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
Outline In this module, the following topics will be covered:
Excel: Macros & Pivot Tables
International Computer Driving Licence Syllabus version 5.0
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Microsoft Excel All editions of Microsoft office.
Exploring Microsoft Excel
Lesson 1 - Automating Tasks
Objectives At the end of this session, students will be able to:
Microsoft Excel 2007 – Level 2
Presentation transcript:

Introduction to VBA

This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need a review, you can get started by going through the Introduction to Excel in the first module

The Idea of Objects VBA is built on the idea that a workbook is a hierarchy of objects An object can contain other objects; for example, a workbook contains worksheets, and the worksheets contain cells An object can have properties, like the value in a cell or the font used in a cell An object can have methods, which are actions that affect the object.

Events Objects can have events: for example, opening a workbook is an event; so is clicking on a cell Our VBA programs will perform tasks when events happen. The spreadsheet or Excel form we are working with will serve as the user interface, and events like clicking a button will trigger the performance of a task.

Macros VBA programs are called macros. The simplest way to get started writing macros is to record one. This feature makes VBA write code based on the actions you perform while recording. We’ll go through an example to illustrate this. This is not a very good way to create macros other than really simple ones, especially if formulas are involved. It is good for getting an idea of what kind of code to write to perform a particular task

BUT FIRST: A Warning! Macros can be extremely dangerous! They can change files on your computer, send s to everyone in your address book, and insert spyware; hackers love them You should NEVER run macros from an un- trusted source The default setting on the Macro Security control (in Windows) is a good one: you have to authorize macros before you can run them

Here’s the control for the security settings (Windows version) Macro Security control

The “Trust Center” Window: Get here by clicking the Macro Security control This default setting is the correct one for us You will have to check a box to enable macros to run whenever you open a workbook with macros in it. You will also have to save your workbook as a “Macro-enabled Workbook” with extension.xlsm

Macro Security (Mac version) Get this window by clicking Preferences under the Excel menu

Be sure that the “warn before opening a file that contains macros” box is checked

Recording a Macro (Windows) There are two macro recording buttons as shown by the arrows. You can push either one to begin recording.

Relative References Control The “Use Relative References” button lets you choose whether to use relative or absolute references when recording your macro. The default is to use absolute references.

Macro Recording on the Mac Macro recording button Relative references control Note these are on the Developer tab

Let’s record a super simple macro This macro is in the workbook CopyCols.xlsm posted on the website Using absolute references, we’ll copy one column to another The example is shown in Windows; doing it on the Mac is quite similar

The Worksheet Before We Record I used R1C1 in Row 1 Col 1 etc. so you can see where data comes from when we copy it.

Here’s the screen we get when we push the Record Macro Button I gave it a descriptive name. If you give it a shortcut key, make sure it doesn’t conflict with an important one Be sure to describe what it does Our usual option After you push OK, recording begins

You can see the Mac version has basically the same interface as the Windows version

In the middle of recording… Click here when done [On the Mac, just click the record button again to stop]

To run the macro use the Macros button (second from left in Developer ribbon) Highlight the macro you want to run and push the run button

Add a Button to Run the Macro (Windows) On the developer tab, under Insert, click the little triangle. The icon for a button is at the upper left under Form Controls

Add a Button to Run the Macro (Mac) On the Mac, the controls you can add are in the ribbon

Click to put the button on the worksheet Put it about where you want it, but you can always move it later The next step is to associate the button with the macro The screen on the next page will come up automatically

Highlight the macro name and click OK BeforeAfter

Click on the button to get a cursor to type a more meaningful text on it

The final product I had to make the button longer to hold the text I wanted

Let’s look at the code! Click the Macros button at the upper left of the developer tab and then click the Edit button. This will take you to the VBA Editor.

The VBA Window (Windows) Project Window Properties Window Code Window Let’s just look at the code for now

Code Explanation Sub CopyAtoD() ' ' CopyAtoD Macro ' Copy Column A to Column D ' Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste End Sub A “Sub” is a working piece of code, a Macro. This is the code for our Macro CopyAtoD A line that starts with a ‘ is a comment. It is not part of the code. VBA used our description for this comment. The editor colors comments green The End Sub closes off the code for this subroutine. Sub and End Sub are key words for the editor; it colors them blue

The Code Body (1) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste A range of columns (which here is just one column, A) is an object. It has a method Select. This code activates the method to select that column

The Code Body (2) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste A selection is also an object. It has a method called Copy. This line copies the selection (column A) and puts it on the clipboard

The Code Body (3) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste Change the selection from Column A to Column D

The Code Body (4) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste Here the sheet is the object when we do a paste. This pastes the information on the clipboard to the selected place on the active worksheet

What Code Is The code is a series of instructions to Excel The instructions are performed in the order given in the code, so the order is VERY important The idea of coding is to write instructions to make Excel do what you want it to do. This can be much more efficient than doing some boring task over and over again.

Saving the Workbook: It Must be Saved as a Macro-Enabled Workbook!

When you reopen, click the Options button and choose Enable this Content

Let’s do one more macro… We’ll do the same macro, but this time with the Use Relative References option selected I’m naming this one “Copy4Back” So I will click the relative references option, click record macro, and give the macro a name. Then I will select Column D, select Column A, copy Column A to Column D, and stop recording. Then I will make a button for my new macro.

Here’s what it looks like

After Clicking Copy A toD If I select Column D and click Copy 4 Back, I get the same thing. But if I select Column E and click Copy 4 Back, I get the next picture. (I erased Column D first.)

Column B was Copied! … because I had relative references turned on

Slight problem… If you look at the code you will see I probably should have named my macro Copy3Back Relative references can do confusing things. So can copying formulas In general, the best use of macro recording is for very simple repetitive tasks, or for finding out what kind of code goes with an action (record a macro, then look at the code) Starting next time, we’ll write our own code