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.

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.
Introduction to Excel Chapter 2 Excel Fundamentals Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
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.
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.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #2: The Visual Basic Editor The Visual Basic for Applications Programming Language IE 212: Computational.
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material.
© Commonwealth of Australia 2009 Using OneNote in the Classroom.
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.
© 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-
Microsoft Excel Macros & Excel Solver (IENG490)
Microsoft Excel By: Dr. K.V. Vishwanath Professor, Dept. of C.S.E,
European Computer Driving Licence Syllabus version 5.0 Module 4 – Spreadsheets Chapter 22 – Functions Pass ECDL5 for Office 2007 Module 4 Spreadsheets.
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.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
1 CS 106 Computing Fundamentals II Chapter 4 “Excel Basics for Mac” Herbert G. Mayer, PSU CS status 6/27/2013 Initial content copied verbatim from CS 106.
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.
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.
LOGO Chapter II Entering Excel Formulas and Formatting Data Friday, November 20, 2015.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
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.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
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.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
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:
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Exploring Microsoft® Excel® 2016 Series Editor Mary Anne Poatsy
Microsoft Excel All editions of Microsoft office.
Exploring Microsoft Excel
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Lesson 1 - Automating Tasks
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Microsoft Excel 2007 – Level 2
Presentation transcript:

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 material developed by CS professors: Cynthia Brown & Robert Martin

2 Syllabus Not An Introduction to Excel Not An Introduction to Excel Idea of Objects Idea of Objects Events Events But First: A Warning But First: A Warning Security Settings Security Settings Macro Recording Macro Recording Look at Code Look at Code What is Code? What is Code?

3 Not An Introduction to Excel Assumption: you have a basic familiarity with ExcelAssumption: you have a basic 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 moduleIf you don’t, or you need a review, you can get started by going through the Introduction to Excel in the first module

4 Idea of Objects VBA is built on the idea that a workbook is a hierarchy of objectsVBA 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 cellsAn 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 cellAn 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 objectAn object can have methods, which are actions that affect the object

5 Events Objects can have events: for example, opening a workbook is an event; so is clicking on a cellObjects can have events: for example, opening a workbook is an event; so is clicking on a cell Our VBA programs perform tasks when events happenOur VBA programs 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 taskThe 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

6 Macros VBA programs are called macros. The simplest way to get started writing macros is to record one. This feature lets VBA produce code based on actions you perform while recordingVBA programs are called macros. The simplest way to get started writing macros is to record one. This feature lets VBA produce code based on actions you perform while recording We’ll go through an exampleWe’ll go through an example This is not a proper way to create macros other than really simple ones, especially if formulas are involvedThis is not a proper 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 taskIt is good for getting an idea of what kind of code to write to perform a particular task

7 BUT FIRST: A Warning! Macros can be dangerous! They can change files on your computer, send s to everyone in your address book, and insert spyware; hackers love themMacros can be dangerous! They can change files on your computer, send s to everyone in your address book, and insert spyware; hackers love them Caveat: NEVER run macros from an un-trusted sourceCaveat: 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 themThe default setting on the Macro Security control (in Windows) is a good one: you have to authorize macros before you can run them

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

9 Get to “Trust Center” Window 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

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

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

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

13 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.

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

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

16 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.

17 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

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

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

20 To run Macro Push Macro Button Highlight the macro you want to run and push the run button

21 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

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

23 Click to put Button onto Worksheet Put it about where you want it, but you can always move it laterPut it about where you want it, but you can always move it later The next step is to associate the button with the macroThe next step is to associate the button with the macro The screen on the next page will come up automaticallyThe screen on the next page will come up automatically

24 Highlight Macro Name, Click OK BeforeAfter

25 Click on the button to get a cursor to type meaningful text

26 Final Product I had to make the button longer to hold the text I wanted

27 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.

28 The VBA Window (Windows) Project Window Properti es Windo w Code Windo w Let’s just look at the code for now

29 Code Explanation Sub CopyAtoD() ' ' CopyAtoD Macro ' Copy Column A to Column D '' Columns("A:A").Select Columns("A:A").Select Selection.Copy Selection.Copy Columns("D:D").Select Columns("D:D").Select ActiveSheet.Paste 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

30 The Code Body (1) Columns("A:A").Select Columns("A:A").Select Selection.Copy Selection.Copy Columns("D:D").Select Columns("D:D").Select ActiveSheet.Paste 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

31 The Code Body (2) Columns("A:A").Select Columns("A:A").Select Selection.Copy Selection.Copy Columns("D:D").Select Columns("D:D").Select ActiveSheet.Paste 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

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

33 The Code Body (4) Columns("A:A").Select Columns("A:A").Select Selection.Copy Selection.Copy Columns("D:D").Select Columns("D:D").Select ActiveSheet.Paste 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

34 What Is Code? The code is a series of instructions to ExcelThe code is a series of instructions to Excel The instructions are performed in the order given in the code, so order is critically importantThe instructions are performed in the order given in the code, so order is critically important The idea of coding is to write instructions to make Excel do what you want it to doThe idea of coding is to write instructions to make Excel do what you want it to do

35 Saving the Workbook: Save as Macro-Enabled Workbook!

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

37 One More Macro We’ll do the same macro, but this time with the Use Relative References option selectedWe’ll do the same macro, but this time with the Use Relative References option selected I’m naming this one “Copy4Back”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 we make a button for the new macroSo 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 we make a button for the new macro

38 Here’s what it looks like

39 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.)

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

41 Slight Problem If you look at the code you will see I probably should have named my macro Copy3BackIf you look at the code you will see I probably should have named my macro Copy3Back Relative references can be confusingRelative references can be confusing So can copying formulasSo 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)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) Next we’ll write our own codeNext we’ll write our own code