Tutorial 12: Enhancing Excel with Visual Basic for Applications

download Tutorial 12: Enhancing Excel with Visual Basic for Applications

If you can't read please download the document

description

Tutorial 12: Enhancing Excel with Visual Basic for Applications. 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. 2. 2. - PowerPoint PPT Presentation

Transcript of Tutorial 12: Enhancing Excel with Visual Basic for Applications

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

Tutorial 12: Enhancing Excel with Visual Basic for ApplicationsMicrosoft Office Excel 2010XPXPXPNew Perspectives on Microsoft Excel 2010222ObjectivesCreate a macro using the macro recorderWork with the Project Explorer and Properties window of the VBA EditorEdit a sub procedureRun a sub procedureWork with VBA objects, properties, and methods2XPXPXPNew Perspectives on Microsoft Excel 20103ObjectivesCreate an input box to retrieve information from the userCreate and run If-Then control structuresWork with comparison and logical operatorsCreate message boxesCustomize the Quick Access ToolbarCustomize ExcelXPXPXP

New Perspectives on Microsoft Excel 20104Visual OverviewXPXPXP

New Perspectives on Microsoft Excel 20105The Visual Basic EditorXPXPXPNew Perspectives on Microsoft Excel 20106Developing an Excel ApplicationExcel applications use Excel commands, tools, and functions to perform an actionStored as an Excel fileCan only be opened from within ExcelExcel macro recorderFastest way to create macrosXPXPXPNew Perspectives on Microsoft Excel 20107Developing an Excel ApplicationMacro buttons provide a quick way to move between worksheets

XPXPXPNew Perspectives on Microsoft Excel 20108Working with the Visual Basic EditorVisual Basic for Applications (VBA)Common programming language used by all Microsoft Office programsUsed to create Excel macros that make it easier to display data on different types of information

XPXPXPNew Perspectives on Microsoft Excel 20109Working with the Visual Basic EditorVisual Basic Editor displays three windows:Project Explore windowProperties windowCode window

XPXPXPNew Perspectives on Microsoft Excel 201010Examining Project ExplorerUse to manage projects (collection of items that make up a customized application)Contains a hierarchical list of all the objectsIs dockableDisplays project components in a tree structure

XPXPXPNew Perspectives on Microsoft Excel 201011Using the Properties WindowTo view a list of properties for any object in alphabetical order and by category

XPXPXPNew Perspectives on Microsoft Excel 201012Naming ModulesGood practice to rename a module (collection of VBA macros) with a descriptive name that describes the type of macros it contains

XPXPXPNew Perspectives on Microsoft Excel 201013Viewing the Code WindowTo view contents of the macros in project modules

XPXPXPNew Perspectives on Microsoft Excel 201014Procedures Supported by Visual BasicSub proceduresPerform an action on a project or workbook(e.g., formatting a cell or displaying a chart)Function proceduresReturn a valueOften used to create custom functions that can be entered in worksheet cellsProperty proceduresUsed to create custom properties for objects in the projectXPXPXPNew Perspectives on Microsoft Excel 201015Working with Sub ProceduresTo create other sub procedures:Use the macro recorderEnter new sub procedures into Code window by:Typing VBA commands directly, orUsing Insert Procedure commandTo create a sub procedure without the macro recorder, proper VBA syntax must be used or Excel cannot run the macro

XPXPXPNew Perspectives on Microsoft Excel 201016Working with Sub ProceduresCommentStatement that describes behavior or purpose of a procedure but does not perform any actionMust begin with an apostrophe ()Appears in a green fontPublic sub proceduresAvailable to other modules in the projectPrivate sub proceduresHidden from other modules to avoid conflicts in procedure namesXPXPXP

New Perspectives on Microsoft Excel 201017Creating a Sub Procedure Using Copy and PasteAdd Procedure dialog box

Inserted sub procedure

XPXPXPNew Perspectives on Microsoft Excel 201018Creating a Sub Procedure Using Copy and PasteEdited sub procedure

Test a macro by running it from the workbook or from within Visual Basic Editor

XPXPXP

New Perspectives on Microsoft Excel 201019Visual OverviewXPXPXP

New Perspectives on Microsoft Excel 201020Visual Basic ObjectsXPXPXP

New Perspectives on Microsoft Excel 201021Visual Basic for ApplicationsImmediate window shows effects of a single commandAs a command is entered, its effects are instantly applied to the workbookIdeal way to learn VBA syntax and debug programsXPXPXPNew Perspectives on Microsoft Excel 201022Visual Basic for ApplicationsVBA: an object-oriented programming languageTasks are performed by manipulating objectsVBA objects in Excel

XPXPXPNew Perspectives on Microsoft Excel 201023Visual Basic for ApplicationsObjects are often grouped into collection objects, which are themselves objects

Examples of object collections

XPXPXPNew Perspectives on Microsoft Excel 201024Visual Basic for ApplicationsObjects and object collections are organized in a hierarchy with Excel at the top and individual cells of a workbook at the bottomOften referred to as the Excel Object Model

XPXPXPNew Perspectives on Microsoft Excel 201025Visual Basic for ApplicationsVBA provides special object names to refer directly to certain objectsSpecial object names

XPXPXPNew Perspectives on Microsoft Excel 201026Modifying PropertiesVBA language alters objects by either:Modifying the objects properties (attributes that characterize the object), orApplying a method to the object

XPXPXPNew Perspectives on Microsoft Excel 201027Modifying PropertiesTo change the property of an object

Examples of changing a propertys value

XPXPXPNew Perspectives on Microsoft Excel 201028Modifying PropertiesList of properties and methods

Completed VBA command to set the cell value

XPXPXPNew Perspectives on Microsoft Excel 201029Applying MethodsMethod: action that can be performed on an objectObjects and their methods

XPXPXPNew Perspectives on Microsoft Excel 201030Applying MethodsTo apply parameter values to a method

Code to apply a method with parameters

XPXPXPNew Perspectives on Microsoft Excel 201031Applying MethodsSelect and Move methods

XPXPXPNew Perspectives on Microsoft Excel 201032Working with Variables and ValuesPower of VBA begins when you start using variablesVariables are case sensitiveXPXPXPNew Perspectives on Microsoft Excel 201033Declaring a VariableWhen 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

XPXPXPNew Perspectives on Microsoft Excel 201034Assigning a Value to a VariableAfter a variable is declared, data can be stored in itVariables can also store objects

Variables can be used to create general procedures that apply to several objects

XPXPXPNew Perspectives on Microsoft Excel 201035Assigning a Value to a VariableVBA statements in which variables are assigned values or are used to store objects

XPXPXPNew Perspectives on Microsoft Excel 201036Writing a Sub ProcedureCharts and statistics in a workbook are based on defined names rather than cell referencesTo display different data, change definition of the defined name

XPXPXPNew Perspectives on Microsoft Excel 201037Creating a Sub Procedure to Switch Defined NamesDefined names are stored in the Names object collectionTo modify the definition of a name, use either the Value property or the RefersTo propertyCreate a dialog box with VBA to prompt the user; Excel automatically switches to that typeCommon types of program errorsSyntax errorsRun-time errorsLogical errorsXPXPXPNew Perspectives on Microsoft Excel 201038Retrieving Information from the UserTo prompt user for the value of the variable

InputBox function

XPXPXP

New Perspectives on Microsoft Excel 201039Visual OverviewXPXPXP

New Perspectives on Microsoft Excel 201040If Statements and CustomizationXPXPXPNew Perspectives on Microsoft Excel 201041Working with Conditional StatementsAn error value means that Excel cannot find the defined name used in the formulaA macro can be modified to handle this problem by creating a control structureControl structures make decisions based on the type of information the user enters

XPXPXPNew Perspectives on Microsoft Excel 201042Working with Conditional StatementsControl structure for the Change_Type macro

XPXPXPNew Perspectives on Microsoft Excel 201043Using the If StatementMost basic way to run a VBA command in response to a particular conditionIn 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

XPXPXPNew Perspectives on Microsoft Excel 201044Using the If-Then-Else Control StructureUse when macro needs to run an alternate command when the condition is false

XPXPXPNew Perspectives on Microsoft Excel 201045Using the If-Then-ElseIf Control StructureUse if control structure has several conditionsRuns commands in response to each condition

XPXPXPNew Perspectives on Microsoft Excel 201046Using Comparison and Logical OperatorsComparison operatorsDetermine whether the expression used in the condition is true or falseExpression must contain a comparison operator (e.g., , =, =, , and is)Logical operatorsCombine expressions within a conditionMost common logical operators: And and OrOther control structures supported by VBA: For-Next, Do-While, and Do-UntilXPXPXPNew Perspectives on Microsoft Excel 201047Using Logical OperatorsCondition with the And logical operator

Condition using the Or logical operator

XPXPXPNew Perspectives on Microsoft Excel 201048Creating a Message BoxA dialog box that includes buttons and an informative message for the userSimilar to an input box, but does not contain a text box for user to enter valuesCreate with the MsgBox function

XPXPXP

New Perspectives on Microsoft Excel 201049Creating a Message BoxSome button styles merely inform, some ask a question, and others provide an alert to a problemXPXPXPNew Perspectives on Microsoft Excel 201050Customizing the Quick Access ToolbarMakes macros accessible from any sheet in the workbookAdd commands to the Quick Access ToolbarThree commands included by defaultSaveUndoRedoXPXPXPNew Perspectives on Microsoft Excel 201051Customizing the Quick Access ToolbarSpecify whether changes affect all workbooks or a specific workbook; can create a different Quick Access Toolbar for each workbookEach macro button on the toolbar uses the same icon or symbol; different macro names appear in each buttons ScreenTipXPXPXPNew Perspectives on Microsoft Excel 201052Customizing a Quick Access Toolbar ButtonModify buttons so each has a distinct image

Enter more descriptive ScreenTip text

XPXPXPNew Perspectives on Microsoft Excel 201053Customizing Excel Screen ElementsThree general categoriesThose that are part of the Excel programThose that are part of the Excel workbook windowThose that are part of the Excel worksheetDifference between these categories affects where a screen element can be hidden

XPXPXPNew Perspectives on Microsoft Excel 201054Customizing Excel Screen ElementsWorkbook with hidden Excel elements

XPXPXPNew Perspectives on Microsoft Excel 201055Customizing Excel Screen ElementsExcel customization options

XPXPXPNew Perspectives on Microsoft Excel 201056Saving a Worksheet as a PDFPDF (Portable Document Format)File format developed by Adobe Systems that supports all elements of a printed document in an electronic format that is easily sharedExcel provides two options for publishing PDFsStandard option optimizes the PDF for use with online publishing and printingMinimum size option is used strictly for online publishing, but not for printingXPXPXPNew Perspectives on Microsoft Excel 201057Introducing Custom FunctionsA function procedure returns a value rather than performing an action (like a sub procedure)

XPXPXP