Tutorial 12: Enhancing Excel with Visual Basic for Applications

Slides:



Advertisements
Similar presentations
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Advertisements

Using Macros and Visual Basic for Applications (VBA) with Excel
Tutorial 7: Developing an Excel Application
Developing an Excel Application
Tutorial 8: Developing an Excel Application
© 2002 ComputerPREP, Inc. All rights reserved. Excel 2000: Customizing Excel and Using Macros.
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
Microsoft Excel 2010 ® ® Tutorial 6: Managing Multiple Worksheets and Workbooks.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 1 1 Microsoft Office Excel 2003.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
Tutorial 7: Using Advanced Functions and Conditional Formatting
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 6 1 Microsoft Office Excel 2003 Tutorial 6 – Working With Multiple Worksheets.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
Customizing Word Microsoft Office Word 2007 Illustrated Complete.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
® Microsoft Office 2010 Excel Tutorial 1: Getting Started with Excel.
Adding Automated Functionality to Office Applications.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Object Variables Visual Basic for Applications 3.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Creating Macros Using VBA. Assigning a Macro to a Button Display the Forms toolbar. Click the Button icon. Click and drag the mouse pointer to specify.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
OV Copyright © 2011 Element K Content LLC. All rights reserved.  Create a Macro with the Macro Recorder  Edit a Macro  Debug a Macro  Customize.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
CHAPTER 9 Introducing Microsoft Office Learning Objectives Start Office programs and explore common elements Use the Ribbon Work with files Use.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Microsoft® Excel Use Insert Function. 1 Key and point to enter functions. 2 Navigate with and create named ranges. 3 Use range names in functions.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Visual Basic for Applications Macro Programming For Microsoft Office.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
OCC Network Drives  H:\  P:\ 
Chapter 9 Macros And Visual Basic For Applications.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Chapter 11 Enhancing an Online Form and Using Macros Microsoft Word 2013.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Object-Oriented Application Development Using VB.NET 1 Chapter 2 The Visual Studio.NET Development Environment.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Microsoft Excel 2007 Noris Bt. Ismail Faculty of Information and Communication Technology Tel : (Ext 8408) BCOMP0101.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Excel Tutorial 8 Developing an Excel Application
Exploring Excel Chapter 7 Automating Repetitive Tasks: Macros and
Developing an Excel Application
Microsoft Access Illustrated
Microsoft Office Illustrated
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Introducing VBA Macros
Exploring Microsoft Excel
Microsoft Office Excel 2003
Microsoft Excel 2007 – Level 2
Presentation transcript:

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 New Perspectives on Microsoft Excel 2010 2 2 2

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 New Perspectives on Microsoft Excel 2010

Visual Overview New Perspectives on Microsoft Excel 2010

The Visual Basic Editor New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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

Naming Modules 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

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

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 New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

Visual Overview New Perspectives on Microsoft Excel 2010

Visual Basic Objects New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

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

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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

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

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

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

Applying Methods Select and Move methods New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

Visual Overview New Perspectives on Microsoft Excel 2010

If Statements and Customization New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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

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

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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

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 New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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

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

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

Customizing Excel Screen Elements Excel customization options New Perspectives on Microsoft Excel 2010

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 New Perspectives on Microsoft Excel 2010

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