Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) 456-7890 University Name.

Slides:



Advertisements
Similar presentations
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Advertisements

Microsoft Office XP Microsoft Excel
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.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Using Microsoft Office Excel 2007
Chapter 5 Multitable Forms
Chapter 5 Creating, Sorting, and Querying a Table
Chapter 6: Pivot Tables Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Microsoft Excel 2010 Chapter 7
Chapter 1 Databases and Database Objects: An Introduction
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
Customizing Word Microsoft Office Word 2007 Illustrated Complete.
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.
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Access Tutorial 10 Automating Tasks with Macros
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
Chapter 2 Querying a Database
LOGO Chapter V Formattings 1. LOGO Overview  Conditional formatting  Working with tables  Filtering  Sorting  Freeze panes  Pivot tables  How to.
Chapter 3 Maintaining a Database
Saeed Ghanbartehrani Summer 2015 Lecture Notes #2: The Visual Basic Editor The Visual Basic for Applications Programming Language IE 212: Computational.
Working with a Database
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.
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-
11 Exploring Microsoft Office Excel 2007 Chapter 4: Working with Large Worksheets and Tables Chapter 04 - Lecture Notes (CSIT 104)
Chapter 1 Databases and Database Objects: An Introduction
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
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.
Automating Database Processing
© 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 15 Advanced Tables.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Access Project 7 Advanced Report and Form Techniques.
Microsoft Access 2010 Chapter 10 Administering a Database System.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
CREATING DATABASE Presenter: Jolanta Soltis. When to use Excel Use Excel when you: –Require a flat or non-relational view of your data (you do not need.
Overview Lesson 1 Miami Beach Senior High School Academy of Information Technology 1.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 2 ® Building Queries.
Microsoft Excel Prepared by the Academic Faculty Members of IT.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
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.
Chapter 7 Creating Templates, Importing Data, and Working with SmartArt, Images, and Screen Shots Microsoft Excel 2013.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Chapter 8 Using Document Collaboration, Integration, and Charting Tools Microsoft Word 2013.
Chapter 11 Enhancing an Online Form and Using Macros Microsoft Word 2013.
Excel Tutorial 8 Developing an Excel Application
Outline In this module, the following topics will be covered:
Computer Fundamentals
Microsoft Excel 2003 Illustrated Complete
Chapter 7 Advanced Form Techniques
Chapter 1 Databases and Database Objects: An Introduction
Lesson 1 - Automating Tasks
Microsoft Excel 2007 – Level 2
Presentation transcript:

Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name

2 Overview  12.1 Introduction  12.2 Macros  12.3 Customizing the Ribbon and Quick Access Toolbar  12.4 Summary

3 Introduction  Recording a macro.  Writing simple VBA procedures.  Creating event procedures.  Assigning macros to Form Control objects in Excel.  Customizing the Ribbon and Quick Access Toolbar by assigning macros.

4 Macros  Recording Macros  The VBA Code  Event Procedures

5 Recording Macros  Macros are technically defined as units of VBA code.  Macros can be thought of as a way to automate a series of actions in a spreadsheet application.  Macros can either be created directly with VBA code in the Visual Basic Editor, or recorded in Excel.  To record a macro, one must know exactly the actions to perform and then use the Developer > Code > Record Macro command on the Ribbon.

6 Figure 12.1  Let us record a macro which copies and pastes data.  A dealer wants to place the data highlighted in yellow in a newspaper advertisement.

7 Preparation  First, review/practice the steps that one should take when recording the macro –Select the first three columns (Make, Model, and Year) of the Used Cars table. While pressing the CTRL key, select the last column (Price) –Either right-click and choose Copy from the shortcut menu, or use the shortcut key CTRL-C to copy the selected data. –Place the cursor in the first cell of the Newspaper Information table (cell C19). –Paste the data either by right-clicking and choosing Paste from the shortcut menu, or by using the shortcut key CTRL-V.

8 Record  Now we are ready to record the macro. Click on: Developer > Code > Record Macro command on the Ribbon  When the Record Macro dialog box appears, –Enter a name for the macro. –Define its location. –Determine any shortcut keys that you wish to associate with it.

9 Stop and Play  Once you begin recording, notice that the Record Macro button transforms to a Stop Recording button.  After finishing the steps needed to copy and paste the information, you can stop recording. Click on: Developer > Code > Stop Recording command.  Once recorded, play the macro to ensure it works correctly. –Click on: Developer > Code > Macros command. –Select the macro from the list and click Run.

10 Result  We can see that the macro works correctly.

11 VBA Code  Since we can study the VBA code generated when we record a macro, we can easily learn how to create a similar macro directly from VBA by copying the code generated.  We will now learn how to use Sort and Filter functions in VBA by first recording a macro and then using the generated VBA code to create similar macros starting in VBA.

12 Figure 12.5  Suppose there is a database for the Miami Airport which lists Flight Destinations, Number of Stops, Class and Price.  We want to filter this information so that we can do the following: –View flights to Beijing –View flights to Hong Kong –View all flights  We also want to sort the data by: –Number of Stops –Price

13 Filtering  Begin by recording three macros to filter the data for specific flight destinations.  To prepare for recording this macro, let review the steps that should be performed: –Highlight the entire table (cells B3:E15) –Select Data > Sort & Filter > Filter command on the Ribbon. –Click on the filter drop-down button of the “Destination” column. –Select Beijing, Hong Kong, or All respectively from the filter drop-down list:  1 st Macro: “ViewBeijingFlights.”  2 nd Macro: “ViewHongKongFlights”  3 rd Macro: “ViewAllFlights”

14 Excel to VBE  We can check this recorded macro to ensure it works.  Now we can go to VBE to view the code generated.

15 VBA Code  The first line of code for all three macros, Range(“B3:E15”).  Select is an example of the Select Method for the Range Object.  The second line of code, Selection.AutoFilter, uses the Selection as the object and AutoFilter as the method.  You can view a list of methods and properties associated with the Selection object by typing Selection and a period.  The third line of code, Selection.AutoFilter Field:=1, Criteria1:=…, is an example of an argument of a method. –The Field is the column of data in the selected range that we want to filter from. –The Criteria is the value of the filter drop-down arrow next to this field.  The last line of code selects a cell to de-highlight the data.

16 Figure 12.7  You will often find that some coding redundancies will happen with the Macro Recorder. –You can actually modify these macros directly in the VBA code by deleting the first Selection.AutoFilter statement from each sub procedure.

17 Sorting  We will now record a macro to sort the data by number of stops.  To prepare for recording a macro to sort the data table, we review the steps we will perform –Highlight all data (B3:E15) –Click on: Data > Sort & Filter > Sort command. –Specify the sorting criteria and order in the Sort dialog box. –SortByStops Macro:  Select “Stops” from the “Sort by” list, select “Values” from the “Sort on” list, and select “Largest to Smallest” from the “Order” list, then press OK to close the Sort dialog box. –SortByPrice Macro:  Select “Price” from the “Sort by” list, select “Values” from the “Sort on” list, and select “Largest to Smallest” from the “Order” list, then press OK to close the Sort dialog box.

18 Figure 12.8  We can now test these macros and return to VBE to view the code

19 VBA Code  The first line of code for these macros selects the range we are interested in sorting.  The second line of code clears all the SortFields objects in the active worksheet.  The third line of code creates a new sort field and returns a SortFields object.  The forth line of code uses the With construct to set several properties of object Sort.  The last line of code selects a cell to de-highlight the data.

20 Figure 12.9  In the updated code, we see that the Key is specified by the range name of the column that we are sorting by.  The Order is specified by “xlAscending” or “xlDescending.”

21 Figure  Let us try creating the macro originally from the VBA code and then we can view its performance in Excel.  Suppose that we also want to filter the Business or Economy Class flights.

22 Event Procedures  Event Procedures connect events or actions of an Excel object to a specific macro of VBA code. –Click –Change –Activate  For each Excel object selected in the Project Explorer, there is a list of associated objects in a drop-down menu in the upper-left hand corner of the Code Window.  After selecting an object from this list, you can view corresponding events in the drop-down menu in the upper-right hand corner of the Code Window.

23 Event Procedures (cont’d)  For example, the Activate event can be used on the Workbook object.  Once an object and event are selected, a sub procedure will be initiated.

24 Event Procedures (cont’d)  An event procedure is just like a sub procedure, except that it is associated with a particular action of an Excel object.  Also, sub procedures can have almost any name while event procedures have the name structure object_event.  Event procedures are necessary when building user interfaces.  Suppose we want to have some buttons on the screen that will perform corresponding macros when clicked. To do this, we first create a button and then use a Click event procedure.

25 Form and ActiveX Controls  To create a button in Excel, click on: Developer > Controls > Insert drop- down list.  There are several objects that can be placed on the spreadsheet: command buttons, scroll bars, list boxes, etc.  Let’s choose the Command Button object by clicking on its icon at the ActiveX Controls dialog box

26 Control Properties  Each control has its own set of properties which can be modified –Name = name of the control, this will be used to identify it in VBA (ex: cmdButton) –Caption = text which appears on the control (ex:“Click Here”)  We can also change its font, size, and color from the list of properties.  When we work with ActiveX Controls, the Developer > Controls > Design Mode button is activated.  To view the Properties window, click on the Developer > Controls > Properties command.  The Click event procedure can now be associated with this new object by titling the sub procedure –Sub cmdButton_Click() 

27 Command Button  Let us add a command button to perform one of the filtering macros we previously wrote in VBA and set the following properties.  For example: –Name = cmdHongKong_Click() –Caption = “View Hong Kong Flights”

28 Event Procedure  We can now copy and paste the code for this macro to a new sub procedure as follows.  This title is automatically generated when we select the command button cmdHongKong from the list of objects and then select Click from the list of events.

29 Figure  Return to Excel to test the functionality of this button.  Make sure that we are no longer in Design View. To activate or deactivate the Design View, click on Developer > Controls > Design View command.  Click “View Hong Kong Flights” and see that the filtering works correctly.

30 Figure  Similar Command Buttons can be created for the other macros we have previously recorded.  Let now use the Form Controls listed on the Insert drop-down dialog box.  Click on the Button object, and then click somewhere on the spreadsheet.  The Assign Macro dialog box appears.

31 Figures and  Right-click on the Button to activate the shortcut menu. Use the Edit Text option to edit the caption of this button.  Select the Format Control… option on the shortcut menu to modify the format of this Button.

32 Buttons  Assign a macro to each new button respectively.

33 Customizing the Ribbon and Quick Access Toolbar  We can create new tabs and groups on the Ribbon with icons designated for each macro.  We can also add macros to the Quick Access Toolbar to reach faster to macros that we frequently use.  To customize the Ribbon, right-click on the Ribbon, and then select Customize the Ribbon from the list of options presented.

34 Figure  Excel gives the options to: –Add groups of command buttons to an existing tab of the Ribbon. –Create a new tab.

35 Creating a New Tab  Press the New Tab button in the bottom right side of Excel Options dialog box.  Select the New Tab (Custom) from the Main Tabs list, and then click-on the Rename button.  Title the tab “MyMacros”.

36 Figure  To add two new Groups under this tab: click on the New Group button.  Select the New Group (Custom) listed under MyMacros, and click on the Rename button to title these groups.

37 Figure  We can also assign an icon to each command using the selection provided by Excel.  Use the Move up arrow of Excel Options dialog box to relocate the new tab right after the Home tab in the Ribbon.

38 Figure  Customize the Quick Access Toolbar. –For example, to add the “ViewAllFFlights” macro, right click on MyMacros > Filter > “All” command. –Select the Add to Quick Access Toolbar from the list of options presented.

39 Summary  Macros are technically defined as units of VBA code.  Macros can either be created directly with VBA code in the Visual Basic Editor, or recorded in Excel.  Steps to Record a Macro: –1. Click on: Developer tab > Code group > Record Macro button. –2. Perform a sequence of actions in Excel. –3. Click on: Developer tab > Code group > Stop Recording button.  Events are actions that can take place in the Excel window that cause an action to occur in the VBA code.  An event procedure is just like a sub procedure, except that it is associated with a particular action of an Excel object.  Creating buttons as a user interface can greatly aid a user unfamiliar with macros to perform desired functions.  The user can customize the Ribbon or the Quick Access Toolbar by adding icons of macros that are frequently used.

40 Additional Links  (place links here)