Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exploring Microsoft Excel

Similar presentations


Presentation on theme: "Exploring Microsoft Excel"— Presentation transcript:

1 Exploring Microsoft Excel
Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber Exploring Microsoft Excel Chapter 8

2 Exploring Microsoft Excel 2002 Chapter 8
Objectives (1 of 2) Define a macro Record and run a macro; view and edit a simple macro Use the InputBox and MsgBox statements Use a keyboard shortcut or customized toolbar to execute a macro Exploring Microsoft Excel Chapter 8

3 Exploring Microsoft Excel 2002 Chapter 8
Objectives (2 of 2) Describe the function of the Personal Macro workbook Use the Step Into command to execute a macro one statement at a time Use Copy and Paste to duplicate an existing macro Use Visual Basic IF and DO statements to make decisions Exploring Microsoft Excel Chapter 8

4 Exploring Microsoft Excel 2002 Chapter 8
Overview Use macros to automate repetitious tasks Macro is a set of instructions for Excel Macro instructions are written in the Visual Basic for Applications (VBA) programming language Use the macro recorder to create macros Create more powerful macros Exploring Microsoft Excel Chapter 8

5 Introduction to Macros
Macro recorder stores Excel commands Commands are written in Visual Basic for Applications Macros can be displayed with Visual Basic Editor (VBE) Use VBE to create, edit, run, and debug (fix) Excel macros Use Project Explorer in left-hand pane to locate macro modules Statements appear in Code window Exploring Microsoft Excel Chapter 8

6 Exploring Microsoft Excel 2002 Chapter 8
Macro Statements Sub statement, followed by the name of the macro Sub is short for subroutine Comments begin with apostrophe (’) Comments provide useful information about the purpose of the macro With and End With statements Perform multiple actions on the same object End Sub statement Defines the end of the macro Exploring Microsoft Excel Chapter 8

7 Exploring Microsoft Excel 2002 Chapter 8
Other Macro Features Statements are color-coded Visual Basic toolbar has seven buttons Customize the Visual Basic toolbar Step Into Command helps you debug a macro Opening a file with a macro will prompt a question about viruses Exploring Microsoft Excel Chapter 8

8 Exploring Microsoft Excel 2002 Chapter 8
A Macro Visual Basic Editor: Project Explorer upper left; VBA code on right Exploring Microsoft Excel Chapter 8

9 Exploring Microsoft Excel 2002 Chapter 8
Hands-On Exercise 1 Objective: To record, run, view, and edit a simple macro; to establish a keyboard shortcut to run a macro Create a Macro Record the Macro Test the Macro Start the Visual Basic Editor Edit the Macro Create the Erase Macro Shortcut Keys Step through the Macro Print the Module Exploring Microsoft Excel Chapter 8

10 Exploring Microsoft Excel 2002 Chapter 8
Record Macro Exploring Microsoft Excel Chapter 8

11 Exploring Microsoft Excel 2002 Chapter 8
Testing the Macro Exploring Microsoft Excel Chapter 8

12 Exploring Microsoft Excel 2002 Chapter 8
Cell References (1 of 2) Relative versus Absolute References Critical to specify whether cell references are absolute or relative Absolute cell addresses are constant Macro refers to that cell every time the macro is run Relative cell addresses change VBA uses offset to indicate space from active cell every time the macro is run Exploring Microsoft Excel Chapter 8

13 Exploring Microsoft Excel 2002 Chapter 8
Cell References (2 of 2) Relative references ActiveCell.Offset(1,0).Range(“A1”).Select Relative reference above means the cell one row below the active cell Offset of (0,1) means the cell one column to the right of the active cell Negative numbers are used for cells above or to the left of the active cell Absolute references Range(“A1”).Select Always refers to cell A1 Exploring Microsoft Excel Chapter 8

14 Personal Macro Workbook
A workbook that opens automatically whenever Excel is opened Any macro stored in this workbook can be used by any open workbook Exploring Microsoft Excel Chapter 8

15 Exploring Microsoft Excel 2002 Chapter 8
Hands-On Exercise 2 Objective: To create and store a macro in the Personal Macro workbook; to assign a toolbar button to a macro; to use the Visual Basic InputBox statement The Personal Macro Workbook Record with Relative References The Visual Basic Editor Edit the Macro Test the Revised Macro Add a Custom Button Test the Custom Button Exploring Microsoft Excel Chapter 8

16 Exploring Microsoft Excel 2002 Chapter 8
Hands-On Exercise 3 Objective: To create a data management function; to create a custom button to execute a macro Data Management Functions The Create Name Command The Go To Command Record the Macro (Edit Clear Command) Record the Macro (Advanced Filter Command) View the Macro Assign the Macro Test the Macro Exploring Microsoft Excel Chapter 8

17 Visual Basic for Applications
VBA is a subset of Visual Basic Macros are converted to VBA programs Programs known as procedures Also called subroutines; thus the word Sub at the beginning of the macro VBA allows you to modify macros you record with the Macro Recorder Exploring Microsoft Excel Chapter 8

18 Exploring Microsoft Excel 2002 Chapter 8
Common VBA statements MsgBox statement Displays information to the user while the macro is executing InputBox function Accepts information from the user while the macro is executing Information is stored in a cell for use later in the procedure Exploring Microsoft Excel Chapter 8

19 Exploring Microsoft Excel 2002 Chapter 8
The MsgBox Statement MsgBox statement includes the text to be displayed in the message box. The message box that appears when the macro is run is displayed below. Exploring Microsoft Excel Chapter 8

20 Exploring Microsoft Excel 2002 Chapter 8
The InputBox Function InputBox statement includes the text to be displayed in the input box. The input box that appears when the macro is run is displayed below. Exploring Microsoft Excel Chapter 8

21 Exploring Microsoft Excel 2002 Chapter 8
Hands-on Exercise 4 Objective: To duplicate an existing macro, then modify the copied macro to create an entirely new macro Enable Macros Copy the Chicago Macro Create the Manager Macro Run the Manager Macro Assign a Button Create the Chicago Manager Macro The MsgBox Statement Exploring Microsoft Excel Chapter 8

22 Hands-on Exercise 4 (cont.)
Test the Chicago Manager Macro Create the Any City Any Title Macro Test the Any City Any Title Macro Change the Button Properties Exploring Microsoft Excel Chapter 8

23 Loops and Decision Making
Including IF and Do statements allows for testing IF statement tests a condition Condition must be evaluated as true or false Includes a series of commands to execute if the condition is true Includes an optional else clause with commands to execute if the condition is false DO statement repeats a block of statements until a condition becomes true Commonly called a loop Exploring Microsoft Excel Chapter 8

24 Exploring Microsoft Excel 2002 Chapter 8
Hands-On Exercise 5 Objective: To implement loops and decision making through the Do and If statements The ClearColor Macro Record the Highlight Manager Macro View the Macro Test the Macro Add the IF Statement An Endless Loop Complete the Macro Exploring Microsoft Excel Chapter 8

25 Exploring Microsoft Excel 2002 Chapter 8
Summary (1 of 2) A macro automates a repetitive task Macros are recorded using the Macro Recorder Record using either absolute or relative references Macros are written in Visual Basic for Applications programming language Generic macros should be stored in the Personal Macro workbook They will always be available to any workbook Exploring Microsoft Excel Chapter 8

26 Exploring Microsoft Excel 2002 Chapter 8
Summary (2 of 2) Macros are run with a toolbar, keystroke or a button Comments contain reminders to the programmer MsgBox and InputBox VBA statements make macros interactive More powerful Excel macros can be programmed with IF and DO VBA statements Exploring Microsoft Excel Chapter 8


Download ppt "Exploring Microsoft Excel"

Similar presentations


Ads by Google