Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Office Excel 2010 ® ® Tutorial 12: Enhancing Excel with Visual Basic for Applications.

Similar presentations


Presentation on theme: "Microsoft Office Excel 2010 ® ® Tutorial 12: Enhancing Excel with Visual Basic for Applications."— Presentation transcript:

1 Microsoft Office Excel 2010 ® ® Tutorial 12: Enhancing Excel with Visual Basic for Applications

2 XP New Perspectives on Microsoft Excel Objectives Create a macro using the macro recorder Work with the Project Explorer and Properties window of the VBA Editor Edit a sub procedure Run a sub procedure Work with VBA objects, properties, and methods 2

3 XP New Perspectives on Microsoft Excel Objectives Create an input box to retrieve information from the user Create and run If-Then control structures Work with comparison and logical operators Create message boxes Customize the Quick Access Toolbar Customize Excel

4 XP New Perspectives on Microsoft Excel Visual Overview

5 XP New Perspectives on Microsoft Excel The Visual Basic Editor

6 XP New Perspectives on Microsoft Excel Developing an Excel Application Excel applications use Excel commands, tools, and functions to perform an action – Stored as an Excel file – Can only be opened from within Excel Excel macro recorder – Fastest way to create macros

7 XP New Perspectives on Microsoft Excel Developing an Excel Application Macro buttons provide a quick way to move between worksheets

8 XP New Perspectives on Microsoft Excel Working with the Visual Basic Editor Visual Basic for Applications (VBA) – Common programming language used by all Microsoft Office programs – Used to create Excel macros that make it easier to display data on different types of information

9 XP New Perspectives on Microsoft Excel Working with the Visual Basic Editor Visual Basic Editor displays three windows: – Project Explore window – Properties window – Code window

10 XP New Perspectives on Microsoft Excel Examining Project Explorer Use to manage projects (collection of items that make up a customized application) Contains a hierarchical list of all the objects Is dockable Displays project components in a tree structure

11 XP New Perspectives on Microsoft Excel Using the Properties Window To view a list of properties for any object in alphabetical order and by category

12 XP New Perspectives on Microsoft Excel Naming Modules Good practice to rename a module (collection of VBA macros) with a descriptive name that describes the type of macros it contains

13 XP New Perspectives on Microsoft Excel Viewing the Code Window To view contents of the macros in project modules

14 XP New Perspectives on Microsoft Excel Procedures Supported by Visual Basic Sub procedures – Perform an action on a project or workbook (e.g., formatting a cell or displaying a chart) Function procedures – Return a value – Often used to create custom functions that can be entered in worksheet cells Property procedures – Used to create custom properties for objects in the project

15 XP New Perspectives on Microsoft Excel Working with Sub Procedures To create other sub procedures: – Use the macro recorder – Enter new sub procedures into Code window by: Typing VBA commands directly, or Using Insert Procedure command To create a sub procedure without the macro recorder, proper VBA syntax must be used or Excel cannot run the macro

16 XP New Perspectives on Microsoft Excel Working with Sub Procedures Comment – Statement that describes behavior or purpose of a procedure but does not perform any action – Must begin with an apostrophe (‘) – Appears in a green font Public sub procedures – Available to other modules in the project Private sub procedures – Hidden from other modules to avoid conflicts in procedure names

17 XP New Perspectives on Microsoft Excel Creating a Sub Procedure Using Copy and Paste Add Procedure dialog box Inserted sub procedure

18 XP New Perspectives on Microsoft Excel Creating a Sub Procedure Using Copy and Paste Edited sub procedure Test a macro by running it from the workbook or from within Visual Basic Editor

19 XP New Perspectives on Microsoft Excel Visual Overview

20 XP New Perspectives on Microsoft Excel Visual Basic Objects

21 XP New Perspectives on Microsoft Excel Visual Basic for Applications Immediate window shows effects of a single command – As a command is entered, its effects are instantly applied to the workbook – Ideal way to learn VBA syntax and debug programs

22 XP New Perspectives on Microsoft Excel Visual Basic for Applications VBA: an object-oriented programming language Tasks are performed by manipulating objects VBA objects in Excel

23 XP New Perspectives on Microsoft Excel Visual Basic for Applications Objects are often grouped into collection objects, which are themselves objects Examples of object collections

24 XP New Perspectives on Microsoft Excel Visual Basic for Applications Objects and object collections are organized in a hierarchy with Excel at the top and individual cells of a workbook at the bottom – Often referred to as the Excel Object Model

25 XP New Perspectives on Microsoft Excel Visual Basic for Applications VBA provides special object names to refer directly to certain objects Special object names

26 XP New Perspectives on Microsoft Excel Modifying Properties VBA language alters objects by either: – Modifying the object’s properties (attributes that characterize the object), or – Applying a method to the object

27 XP New Perspectives on Microsoft Excel Modifying Properties To change the property of an object Examples of changing a property’s value

28 XP New Perspectives on Microsoft Excel Modifying Properties List of properties and methods Completed VBA command to set the cell value

29 XP New Perspectives on Microsoft Excel Applying Methods Method: action that can be performed on an object Objects and their methods

30 XP New Perspectives on Microsoft Excel Applying Methods To apply parameter values to a method Code to apply a method with parameters

31 XP New Perspectives on Microsoft Excel Applying Methods Select and Move methods

32 XP New Perspectives on Microsoft Excel Working with Variables and Values Power of VBA begins when you start using variables Variables are case sensitive

33 XP New Perspectives on Microsoft Excel Declaring a Variable When you declare a variable, allocate storage space for it by “dimensioning” it Can define exactly what type of data can be stored VBA supports a wide range of data types

34 XP New Perspectives on Microsoft Excel Assigning a Value to a Variable After a variable is declared, data can be stored in it Variables can also store objects Variables can be used to create general procedures that apply to several objects

35 XP New Perspectives on Microsoft Excel Assigning a Value to a Variable VBA statements in which variables are assigned values or are used to store objects

36 XP New Perspectives on Microsoft Excel Writing a Sub Procedure Charts and statistics in a workbook are based on defined names rather than cell references To display different data, change definition of the defined name

37 XP New Perspectives on Microsoft Excel Creating a Sub Procedure to Switch Defined Names Defined names are stored in the Names object collection – To modify the definition of a name, use either the Value property or the RefersTo property Create a dialog box with VBA to prompt the user; Excel automatically switches to that type Common types of program errors – Syntax errors – Run-time errors – Logical errors

38 XP New Perspectives on Microsoft Excel Retrieving Information from the User To prompt user for the value of the variable InputBox function

39 XP New Perspectives on Microsoft Excel Visual Overview

40 XP New Perspectives on Microsoft Excel If Statements and Customization

41 XP New Perspectives on Microsoft Excel Working with Conditional Statements An error value means that Excel cannot find the defined name used in the formula A macro can be modified to handle this problem by creating a control structure – Control structures “make decisions” based on the type of information the user enters

42 XP New Perspectives on Microsoft Excel Working with Conditional Statements Control structure for the Change_Type macro

43 XP New Perspectives on Microsoft Excel Using the If Statement Most basic way to run a VBA command in response to a particular condition In this type of control structure, if a certain condition is met, the program executes a specified command When the condition of the If statement is not true, the macro does nothing

44 XP New Perspectives on Microsoft Excel Using the If-Then-Else Control Structure Use when macro needs to run an alternate command when the condition is false

45 XP New Perspectives on Microsoft Excel Using the If-Then-ElseIf Control Structure Use if control structure has several conditions Runs commands in response to each condition

46 XP New Perspectives on Microsoft Excel Using Comparison and Logical Operators Comparison operators – Determine whether the expression used in the condition is true or false – Expression must contain a comparison operator (e.g.,, =, =, <>, and is) Logical operators – Combine expressions within a condition – Most common logical operators: And and Or Other control structures supported by VBA: For- Next, Do-While, and Do-Until

47 XP New Perspectives on Microsoft Excel Using Logical Operators Condition with the And logical operator Condition using the Or logical operator

48 XP New Perspectives on Microsoft Excel Creating a Message Box A dialog box that includes buttons and an informative message for the user Similar to an input box, but does not contain a text box for user to enter values Create with the MsgBox function

49 XP New Perspectives on Microsoft Excel Creating a Message Box Some button styles merely inform, some ask a question, and others provide an alert to a problem

50 XP New Perspectives on Microsoft Excel Customizing the Quick Access Toolbar Makes macros accessible from any sheet in the workbook Add commands to the Quick Access Toolbar – Three commands included by default Save Undo Redo

51 XP New Perspectives on Microsoft Excel Customizing the Quick Access Toolbar Specify whether changes affect all workbooks or a specific workbook; can create a different Quick Access Toolbar for each workbook Each macro button on the toolbar uses the same icon or symbol; different macro names appear in each button’s ScreenTip

52 XP New Perspectives on Microsoft Excel Customizing a Quick Access Toolbar Button Modify buttons so each has a distinct image Enter more descriptive ScreenTip text

53 XP New Perspectives on Microsoft Excel Customizing Excel Screen Elements Three general categories – Those that are part of the Excel program – Those that are part of the Excel workbook window – Those that are part of the Excel worksheet Difference between these categories affects where a screen element can be hidden

54 XP New Perspectives on Microsoft Excel Customizing Excel Screen Elements Workbook with hidden Excel elements

55 XP New Perspectives on Microsoft Excel Customizing Excel Screen Elements Excel customization options

56 XP New Perspectives on Microsoft Excel Saving a Worksheet as a PDF PDF (Portable Document Format) – File format developed by Adobe Systems that supports all elements of a printed document in an electronic format that is easily shared Excel provides two options for publishing PDFs – Standard option optimizes the PDF for use with online publishing and printing – Minimum size option is used strictly for online publishing, but not for printing

57 XP New Perspectives on Microsoft Excel Introducing Custom Functions A function procedure returns a value rather than performing an action (like a sub procedure)


Download ppt "Microsoft Office Excel 2010 ® ® Tutorial 12: Enhancing Excel with Visual Basic for Applications."

Similar presentations


Ads by Google