Introducing VBA Macros

Slides:



Advertisements
Similar presentations
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Advertisements

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.
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
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
Advanced Microsoft Word Hosted by Jared Hoffman Topics Keyboard Shortcuts Customizing Toolbars and Menus Auto Format & Auto Correct Tabs Inserting Pictures.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
Customizing Word Microsoft Office Word 2007 Illustrated Complete.
Using the Visual Basic Editor Visual Basic for Applications 1.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Excel Lesson 14 Creating and Using Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
1 Excel Lesson 3 Organizing the Worksheet Microsoft Office 2010 Introductory Pasewark & Pasewark.
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.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
© 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.
1 CA201 Word Application Increasing Efficiency Week # 13 By Tariq Ibn Aziz Dammam Community college.
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-
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 11 Developing Applications Using Excel Microsoft Office Excel 2003.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
OCC Network Drives  H:\  P:\ 
Key Applications Module Lesson 21 — Access Essentials
Chapter 9 Macros And Visual Basic For Applications.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Excel Macros 1 Macros or, How to Automate Part of Your Spreadsheet or Worksheet.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
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.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Chapter 11 Enhancing an Online Form and Using Macros Microsoft Word 2013.
Dive Into® Visual Basic 2010 Express
Excel Tutorial 8 Developing an Excel Application
Visual Basic.NET Windows Programming
Exploring Excel Chapter 7 Automating Repetitive Tasks: Macros and
Chapter 2: The Visual Studio .NET Development Environment
Outline In this module, the following topics will be covered:
University of technology Department of Materials Engineering
Chapter 2 – Introduction to the Visual Studio .NET IDE
Microsoft Excel A Spreadsheet Program.
Developing an Excel Application
Access Project 8 Using Visual Basic for Applications (VBA) and Creating Multi-Page Forms.
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Microsoft Excel 2003 Illustrated Complete
MS-Office It is a Software Package It contains some programs like
Chapter 7 Advanced Form Techniques
Exploring Microsoft® Excel® 2016 Series Editor Mary Anne Poatsy
Exploring Microsoft Excel
ICT Spreadsheets Lesson 1: Introduction to Spreadsheets
Objectives At the end of this session, students will be able to:
Microsoft Office Excel 2003
Microsoft Excel 2007 – Level 2
Visual Basic for Applications: Introduction
Presentation transcript:

Introducing VBA Macros Applications of Spreadsheets C51BR

Automating A Workbook with Macros A macro enables you to combine multiple commands and keystrokes and execute them as a single command. If you perform a particular task frequently, creating a macro to use can be time-saving and improves the consistency and accuracy of repetitive procedures. C51BR

Automating A Workbook with Macros The macro recorder looks at the results of your keystrokes and creates a subroutine in Visual Basic for Applications (VBA) which produces the same results as your keystrokes. Using a macro, you can execute a sequence of instructions by simply clicking a button, pressing a key combination, or selecting a name from a list box. C51BR

Automating Tasks with Macros Macros are viewed and edited using the Visual Basic Editor. fine-tune the macro code delete lines from incorrect keystrokes create programs from scratch C51BR

Preparing to Record a Macro Before you can record a macro, you need to decide what to name the macro and where to store it. A macro name must start with a letter and can contain numbers, letters and the underscore character – but no spaces! C51BR

Preparing to Record a Macro You may store your macro In the current workbook In a new workbook In the personal macro workbook so that it is available to use in any existing or future workbook. You can assign a shortcut key to your macro. Type a letter in the Shortcut key text box within the Record Macro dialog box. C51BR

Recording a Macro Provide a descriptive one-word name for the macro Provide a shortcut key for executing the macro quickly Select a storage location for the macro. Where you store a macro determines its availability to other workbooks Enter a description specifying the purpose of the macro. Excel 2003 enters a description like this one automatically C51BR

Completing the Record Macro Dialog Box Select a storage location for the macro. Where you store a macro determines its availability to other workbooks. C51BR

Recording & Playing a Macro Once you’ve established a name and storage location for your macro, you can begin the recording process. Once you are recording, just perform the task as you normally would. When finished, stop recording. After a macro has been recorded, you can run (or play) it at any time. C51BR

Recording Cell Formatting Commands Stop Recording toolbar “Recording” status C51BR

More About Macros To run a macro using the shortcut key Press and hold the Control key and press the letter you assigned to the macro. If your letter is uppercase, you need to press both the control key and the shift key and then press the letter. To delete a macro, select the macro from the list in the Macro dialog box and click the Delete button. C51BR

Playing Back a Macro All of the macros stored in the Macros in selection appear in this list box Select which macros to view in the list box A description of the currently selected macro is displayed C51BR

Executing a Macro to Enter a Business Name and Address All of the macros stored in the Macros in selection appear in this list box. C51BR

Protecting Yourself From Macro Viruses Most users will specify the “Medium” security setting if they also have antivirus software installed on their system. If you do not have a virus scanner, consider selecting the High or Very High security For more information about security settings, trusted publishers, and digital signing, click the Help button in the Title bar This computer has a virus scanner installed C51BR

Opening a Workbook Containing Macros C51BR

Introducing the VBA Environment Microsoft Visual Basic for Applications (VBA) is the shared and fully integrated macro development environment available in Microsoft Office System 2003. VBA is itself an independent software program. VBA provides a subset of the features found in the Microsoft Visual Basic programming language. Visual Basic creates stand-alone applications VBA must be run within a host application C51BR

Visual Basic Editor - VBE C51BR

Touring the Visual Basic Editor Project Explorer window Properties window Code window Work area C51BR

The Project Explorer Window Project file for the “VBA Tour” workbook Microsoft Excel 2003 workbook and worksheet objects VBA code module object C51BR

The Properties Window Click a tab to display the properties in alphabetical order or grouped by category Displays the name and type of the selected object Double-click a property box to set its value Select a property by clicking its name C51BR

The Code Window Macro name Object box Procedure box View buttons C51BR

Tiling Code Windows in the VBE C51BR

Editing a Recorded Macro Macro name Code window Comment The Code window contains the VBA programming code C51BR

Editing VBA Programming Code C51BR

Stepping Through a Macro The yellow arrow and code highlighting indicate the line to be executed next Margin Indicator bar C51BR

Setting a Breakpoint The red circle and highlighting indicate that a breakpoint has been set Margin Indicator bar C51BR

Printing Your Macros Specify whether to print the selected code, the entire module, or all the modules in the current project When printing a custom form, you can specify whether to print its image, in addition to its code C51BR

Creating a Macro Button in a Workbook Excel provides four methods to run a macro choosing a menu sequence entering the assigned shortcut keystrokes clicking a macro button embedded in a worksheet clicking a macro button added to a new or existing toolbar. C51BR

Working with ActiveX Controls Formerly known as OLE controls, ActiveX controls are prebuilt, reusable software components that you can use to add interface elements and functionality to your workbooks. command buttons drop-down list boxes, etc. You place these controls directly on a worksheet or display them in a custom user form. There are thousands of ActiveX controls, produced by Microsoft and third-party developers, that you can take advantage of in Excel 2003. C51BR

The Control Toolbox Toolbar Design Mode Properties View Mode Check Box Text Box Command Button Option Button List Box Combo Box Toggle Button Spin Button Scroll Bar Label Image More Controls C51BR

Creating a Macro Button in a Workbook Clicking a macro button in a worksheet executes the macro assigned to the button. You can resize a macro button by using its handles. If they are not in view, press the control key while left clicking the button. Change the characteristics of a macro button by right-clicking the button and selecting Format Control from the shortcut menu. C51BR

Creating a Macro Button in a Workbook Move the macro button by right-clicking the button and dragging it by its shaded border to the new location. Remove a macro button in the worksheet by right-clicking it and selecting Cut from the shortcut menu. If you embed a macro button in a worksheet, the macro assigned to that button can only be applied to that worksheet. Create a macro button on a new or existing toolbar so that it can be applied to any workbook. C51BR

Placing a Command Button On the Worksheet C51BR

Placing a Command Button On the Worksheet Displaying the Properties window for the selected control The Command Button ActiveX control is selected in Design mode C51BR

Running a Macro Procedure in the Command Button’s Click Event C51BR

Running the Macro Procedure from the Command Button C51BR

Linking Controls and Cells C51BR

Testing a Combo Box Control Values displayed in this combo box control are stored in the MonthNames range on the Lists worksheet C51BR

Understanding the VBA Language The declaration line specifies the name and type of the procedure Comments appear green in the Code window and are preceded by an apostrophe Keywords appear blue among the program statements C51BR

Excel 2003’s Object Model C51BR

Declaring & Assigning Values to Variables Declaring variables and data types Assigning values to variables C51BR

Writing VBA Code Adding a procedure to a module You must specify the type of procedure that you want to create The code module’s Name property has been changed to “MyCode” C51BR

Using AutoList to Enter Code When you type a period in the With structure, the Editor displays a list of the properties and methods that apply to the object C51BR

The Toggle Display Sub Procedure C51BR

Running the Toggle Display Sub Procedure The display of gridlines and row and column headings is toggled off C51BR

Working with Excel Objects C51BR

Maximizing the Code Window The Procedure box displays the active procedure’s name The insertion point is positioned in the active procedure Click the Procedure View button to view the active procedure only C51BR

Displaying the Application Object’s AutoList Window C51BR

Completing the AppInfo SubProcedure C51BR

Controlling Your Procedures One of the greatest benefits of writing your own procedures is the ability to control how, when, and what code executes. VBA provides several control structures that allow you to test conditions for processing and to perform looping operations, which run the same lines of code repeatedly. These structures let you work within a dynamic environment, engage the user, and make processing decisions within your code. C51BR

Making Decisions with IF…THEN C51BR

Creating the “CalcPayment” Sub Procedure A Sub procedure always begins with “Sub” and ends with “End Sub” Inserting a new code module into the project file The Code window is maximized in the work area C51BR

The Completed Calcpayment Sub Procedure C51BR

Making Decisions with Select…Case C51BR

Looping with For…Next The “c” represents each cell in the rTasks range object C51BR

Looping with Do…While C51BR

Interacting with the User VBA provides two functions for use in programming user interaction. MsgBox InputBox These functions enable you to display and collect information using built-in dialog box forms. C51BR

Using the MSGBOX Function C51BR

Using the MSGBOX Function C51BR

Running the DisplayMessage Sub Procedure msgText msgTitle msgButtons Concatenated expression C51BR

Using the INPUTBOX Function C51BR

Using the INPUTBOX Function C51BR

Running the YearBorn Sub Procedure Assuming that the current year is 2004, this dialog box appears when “24” is entered C51BR