Tutorial 12: Tutorial 12: Enhancing Excel with Visual Basic for

advertisement
Tutorial 12: Enhancing Excel with
Visual Basic for Applications
Microsoft Office Excel 2010
®
®
Objectives
XP
• 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
New Perspectives on Microsoft Excel 2010
2
Objectives
XP
• 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
New Perspectives on Microsoft Excel 2010
3
Visual Overview
New Perspectives on Microsoft Excel 2010
XP
4
The Visual Basic Editor
New Perspectives on Microsoft Excel 2010
XP
5
Developing an Excel Application
XP
• 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
New Perspectives on Microsoft Excel 2010
6
Developing an Excel Application
XP
• Macro buttons provide a quick way to move
between worksheets
New Perspectives on Microsoft Excel 2010
7
Working with the Visual Basic Editor XP
• 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
New Perspectives on Microsoft Excel 2010
8
Working with the Visual Basic Editor XP
• Visual Basic Editor displays three windows:
– Project Explore window
– Properties window
– Code window
New Perspectives on Microsoft Excel 2010
9
Examining Project Explorer
XP
• 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
New Perspectives on Microsoft Excel 2010
10
Using the Properties Window
XP
• To view a list of properties for any object in
alphabetical order and by category
New Perspectives on Microsoft Excel 2010
11
Naming Modules
XP
• Good practice to rename a module (collection
of VBA macros) with a descriptive name that
describes the type of macros it contains
New Perspectives on Microsoft Excel 2010
12
Viewing the Code Window
XP
• To view contents of the macros in project
modules
New Perspectives on Microsoft Excel 2010
13
Procedures Supported by Visual BasicXP
• 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
New Perspectives on Microsoft Excel 2010
14
Working with Sub Procedures
XP
• 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
New Perspectives on Microsoft Excel 2010
15
Working with Sub Procedures
XP
• 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
New Perspectives on Microsoft Excel 2010
16
Creating a Sub Procedure Using CopyXP
and Paste
• Add Procedure dialog box
• Inserted sub procedure
New Perspectives on Microsoft Excel 2010
17
Creating a Sub Procedure Using CopyXP
and Paste
• Edited sub procedure
• Test a macro by running it from the workbook
or from within Visual Basic Editor
New Perspectives on Microsoft Excel 2010
18
Visual Overview
New Perspectives on Microsoft Excel 2010
XP
19
Visual Basic Objects
New Perspectives on Microsoft Excel 2010
XP
20
Visual Basic for Applications
XP
• 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
New Perspectives on Microsoft Excel 2010
21
Visual Basic for Applications
XP
• VBA: an object-oriented programming
language
• Tasks are performed by manipulating objects
• VBA objects in Excel
New Perspectives on Microsoft Excel 2010
22
Visual Basic for Applications
XP
• Objects are often grouped into collection
objects, which are themselves objects
• Examples of object collections
New Perspectives on Microsoft Excel 2010
23
Visual Basic for Applications
XP
• 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
New Perspectives on Microsoft Excel 2010
24
Visual Basic for Applications
XP
• VBA provides special object names to refer
directly to certain objects
• Special object names
New Perspectives on Microsoft Excel 2010
25
Modifying Properties
XP
• VBA language alters objects by either:
– Modifying the object’s properties (attributes that
characterize the object), or
– Applying a method to the object
New Perspectives on Microsoft Excel 2010
26
Modifying Properties
XP
• To change the property of an object
• Examples of changing a property’s value
New Perspectives on Microsoft Excel 2010
27
Modifying Properties
XP
• List of properties and methods
• Completed VBA command to set the cell value
New Perspectives on Microsoft Excel 2010
28
Applying Methods
XP
• Method: action that can be performed on an
object
• Objects and their methods
New Perspectives on Microsoft Excel 2010
29
Applying Methods
XP
• To apply parameter values to a method
• Code to apply a method with parameters
New Perspectives on Microsoft Excel 2010
30
Applying Methods
XP
• Select and Move methods
New Perspectives on Microsoft Excel 2010
31
XP
Working with Variables and Values
• Power of VBA begins when you start using
variables
• Variables are case sensitive
New Perspectives on Microsoft Excel 2010
32
Declaring a Variable
XP
• 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
New Perspectives on Microsoft Excel 2010
33
Assigning a Value to a Variable
XP
• 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
New Perspectives on Microsoft Excel 2010
34
Assigning a Value to a Variable
XP
• VBA statements in which variables are
assigned values or are used to store objects
New Perspectives on Microsoft Excel 2010
35
Writing a Sub Procedure
XP
• 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
New Perspectives on Microsoft Excel 2010
36
Creating a Sub Procedure to Switch
Defined Names
XP
• 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
New Perspectives on Microsoft Excel 2010
37
Retrieving Information from the UserXP
• To prompt user for the value of the variable
• InputBox function
New Perspectives on Microsoft Excel 2010
38
Visual Overview
New Perspectives on Microsoft Excel 2010
XP
39
If Statements and Customization
New Perspectives on Microsoft Excel 2010
XP
40
Working with Conditional StatementsXP
• 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
New Perspectives on Microsoft Excel 2010
41
Working with Conditional StatementsXP
• Control structure for the Change_Type macro
New Perspectives on Microsoft Excel 2010
42
Using the If Statement
XP
• 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
New Perspectives on Microsoft Excel 2010
43
Using the If-Then-Else Control
Structure
XP
• Use when macro needs to run an alternate
command when the condition is false
New Perspectives on Microsoft Excel 2010
44
Using the If-Then-ElseIf Control
Structure
XP
• Use if control structure has several conditions
• Runs commands in response to each condition
New Perspectives on Microsoft Excel 2010
45
Using Comparison and Logical
Operators
XP
• 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: ForNext, Do-While, and Do-Until
New Perspectives on Microsoft Excel 2010
46
Using Logical Operators
XP
• Condition with the And logical operator
• Condition using the Or logical operator
New Perspectives on Microsoft Excel 2010
47
Creating a Message Box
XP
• 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
New Perspectives on Microsoft Excel 2010
48
Creating a Message Box
XP
• Some button styles merely inform, some ask a
question, and others provide an alert to a
problem
New Perspectives on Microsoft Excel 2010
49
Customizing the Quick Access ToolbarXP
• Makes macros accessible from any sheet in
the workbook
• Add commands to the Quick Access Toolbar
– Three commands included by default
• Save
• Undo
• Redo
New Perspectives on Microsoft Excel 2010
50
Customizing the Quick Access ToolbarXP
• 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
New Perspectives on Microsoft Excel 2010
51
Customizing a Quick Access Toolbar
Button
XP
• Modify buttons so each has a distinct image
• Enter more descriptive ScreenTip text
New Perspectives on Microsoft Excel 2010
52
XP
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
New Perspectives on Microsoft Excel 2010
53
XP
Customizing Excel Screen Elements
• Workbook with hidden Excel elements
New Perspectives on Microsoft Excel 2010
54
XP
Customizing Excel Screen Elements
• Excel customization options
New Perspectives on Microsoft Excel 2010
55
Saving a Worksheet as a PDF
XP
• 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
New Perspectives on Microsoft Excel 2010
56
Introducing Custom Functions
XP
• A function procedure returns a value rather
than performing an action (like a sub
procedure)
New Perspectives on Microsoft Excel 2010
57
Download