Excel prgramming

27
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications

description

Excel prgramming

Transcript of Excel prgramming

Page 1: Excel prgramming

COMPREHENSIVE

Excel Tutorial 12

Expanding Excel with Visual Basic for

Applications

Page 2: Excel prgramming

XPXPObjectives• 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• Create an input box to retrieve information from

the user

New Perspectives on Microsoft Office Excel 2007 2

Page 3: Excel prgramming

XPXPObjectives• 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 Office Excel 2007 3

Page 4: Excel prgramming

XPXPDeveloping an Excel Application

New Perspectives on Microsoft Office Excel 2007 4

• If the Excel Developer tab is not on the Ribbon, click the Office Button, click the Excel Options button, click Popular in the Excel Options dialog box (if necessary), click the Show Developer tab in the Ribbon check box to insert a check mark, and then click the OK button

Page 5: Excel prgramming

XPXPWorking with the Visual Basic Editor• In the Code group on the Developer tab, click the

Macros button• Click the macro name in the Macro name box, if

necessary, and then click the Edit button

New Perspectives on Microsoft Office Excel 2007 5

Page 6: Excel prgramming

XPXPWorking with the Visual Basic Editor• A project is a collection of macros, worksheets, data-

entry forms, and other items that make up the customized application you’re trying to create

• Project Explorer is the window in the Visual Basic Editor that displays a hierarchical list of all currently open projects and their contents

• The Project Explorer window is dockable• An object is any element within the Excel working

environment such as a worksheet, cell, workbook, or even Excel itself

New Perspectives on Microsoft Office Excel 2007 6

Page 7: Excel prgramming

XPXPWorking with the Visual Basic Editor• A property is an attribute of an object that

defines one of its characteristics, such as its name, size, color, or location on the screen

• You can view a list of properties for any object in the Properties window

• A module is a collection of VBA macros• The Code window displays the VBA macro code

associated with any item in Project Explorer

New Perspectives on Microsoft Office Excel 2007 7

Page 8: Excel prgramming

XPXPWorking with the Visual Basic Editor

New Perspectives on Microsoft Office Excel 2007 8

Page 9: Excel prgramming

XPXPWorking with Sub Procedures• A sub procedure performs an action on your project or

workbook, such as formatting a cell or displaying a chart• A function procedure returns a value• A property procedure is used to create custom

properties for the objects in your project• Syntax refers to the set of rules that specify how you

must enter certain commands so that VBA interprets them correctly

• A comment is a statement that describes the behavior or purpose of a procedure, but does not perform any action

New Perspectives on Microsoft Office Excel 2007 9

Page 10: Excel prgramming

XPXPWorking with Sub Procedures

New Perspectives on Microsoft Office Excel 2007 10

Page 11: Excel prgramming

XPXPReferring to Objects• VBA is an object-oriented programming

language, in which tasks are performed by manipulating objects

New Perspectives on Microsoft Office Excel 2007 11

Page 12: Excel prgramming

XPXPReferring to Objects• Objects are often grouped into collections, which

are themselves objects, called collection objects

New Perspectives on Microsoft Office Excel 2007 12

Page 13: Excel prgramming

XPXPReferring to Objects• VBA organizes objects and object collections in a

hierarchy with the Excel application at the top and the individual cells of a workbook at the bottom

New Perspectives on Microsoft Office Excel 2007 13

Page 14: Excel prgramming

XPXPReferring to Objects

New Perspectives on Microsoft Office Excel 2007 14

Page 15: Excel prgramming

XPXPApplying Methods• A method is an action that can be performed on

an object, such as closing a workbook or printing the contents of a worksheet

New Perspectives on Microsoft Office Excel 2007 15

Page 16: Excel prgramming

XPXPWorking with Variables and Values• A variable is a named element in a program that

can be used to store and retrieve information• Every variable is identified by a unique variable

name• Dim variable as type

New Perspectives on Microsoft Office Excel 2007 16

Page 17: Excel prgramming

XPXPRetrieving Information from the User

New Perspectives on Microsoft Office Excel 2007 17

Page 18: Excel prgramming

XPXPWorking with Conditional Statements

New Perspectives on Microsoft Office Excel 2007 18

Page 19: Excel prgramming

XPXPWorking with Conditional Statements

New Perspectives on Microsoft Office Excel 2007 19

Page 20: Excel prgramming

XPXPWorking with Conditional Statements

New Perspectives on Microsoft Office Excel 2007 20

Page 21: Excel prgramming

XPXPWorking with Conditional Statements

New Perspectives on Microsoft Office Excel 2007 21

Page 22: Excel prgramming

XPXPCreating a Message Box• MsgBox Prompt, Buttons, Title

New Perspectives on Microsoft Office Excel 2007 22

Page 23: Excel prgramming

XPXPCustomizing the Quick Access Toolbar• Click the Office Button, and then click Excel Options• Click Customize in the Excel Options list• Click the Customize Quick Access Toolbar arrow, and select

whether to customize the toolbar for all documents or a specific workbook

• To add a command to the Quick Access Toolbar, click the command in the left list box, and then click the Add button

• To remove a command from the Quick Access Toolbar, click the command in the right list box, and then click the Remove button

• Click the OK button

New Perspectives on Microsoft Office Excel 2007 23

Page 24: Excel prgramming

XPXPCustomizing the Quick Access Toolbar

New Perspectives on Microsoft Office Excel 2007 24

Page 25: Excel prgramming

XPXPCustomizing Excel Screen Elements• Excel screen elements fall into three general

categories with elements that are: (1) part of the Excel program, (2) part of the Excel workbook window, and (3) part of the Excel worksheet

New Perspectives on Microsoft Office Excel 2007 25

Page 26: Excel prgramming

XPXPExcel Customization Options

New Perspectives on Microsoft Office Excel 2007 26

Page 27: Excel prgramming

XPXPUsing the Save As PDF Add-In• PDF (Portable Document Format) is a file format

developed by Adobe Systems that supports all of the elements of a printed document but in an electronic format that is easily shared

• Add-in available from microsoft.com

New Perspectives on Microsoft Office Excel 2007 27