Visual Basic for Applications Macro Programming For Microsoft Office.

Slides:



Advertisements
Similar presentations
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Advertisements

Essence of programming  Branching  Repetitions.
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.
Exploring Microsoft Excel 2002 Chapter 9 Chapter 9 A Professional Application: VBA and Date Functions By Robert T. Grauer Maryann Barber Exploring Microsoft.
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.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
VBA for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
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.
Programming Excel Macros, Visual Basic, and Solving Your Problem.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
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)
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.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Access Tutorial 10 Automating Tasks with Macros
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
Week 1.  Kate Watson  Checked at least every other day. Queries and general comments welcome.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
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.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
© 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.
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-
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Microsoft Excel Macros & Excel Solver (IENG490)
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.
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Intro to Excel - Session 7.31 Tutorial 7 - Session 7.3 Developing an Excel Application.
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.
Chapter 9 Macros And Visual Basic For Applications.
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.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
VBA Programming for Excel Review Excel Objects Excel Methods Identifying Specific Cells Review Functions for Excel Custom Menus.
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.
VBA Excel Macro 1.Create a Macro: To create a macro in Excel VBA, Create a Macro: To create a macro in Excel VBA,Create a Macro: To create a macro.
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
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Microsoft Access Illustrated
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introducing VBA Macros
Exploring Microsoft Excel
Microsoft Office Excel 2003
VBA Programming for Excel
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Excel 2007 – Level 2
Presentation transcript:

Visual Basic for Applications Macro Programming For Microsoft Office

Macros Macros are programs that make changes to a spreadsheet automatically Change a font size or color Move the cursor to another cell Change the contents of a cell Almost anything you can do from the menus can be automated Simple process – record a macro

Why Create Macros? Automate tasks Ones you do repetitively Complex tasks with possibilities for errors Create custom functions Develop complete applications Create macros that perform specific tasks Customize the menus and toolbars Create custom “front-ends” for users

Recording Macros Tools -> Macro ->Record New Stop Toolbar Appears Excel records every stroke until you click the “Stop” button Replay with: Tools ->Macro->Macros

Record a Macro Turn on recorder Name macro “MyFirstMacro” Move to cell C6 Change text color to Blue Enter “Hello” Click “Stop” button Reset cell C6 Run macro

It Works! Where is it? Macro recorder writes VBA code Stored with the spreadsheet When you save the workbook, you save any code as well. Looks like this:

What is VBA Powerful Programming Language Object Oriented Objects: Cells, Worksheets, Menus Range(“C3”) Objects You control the Properties of an Object Range(“C3”).Font.NameProperties Assignment Statements define property state Range(“C3”).Font.Name = “Arial” Assignment Statements

Objects on a Spreadsheet Workbook Column Row Cell Range Worksheet

VBA Objects Usually referred to by a name Cell “B6” is named Range(“B6”) Column(“B:B”) refers to the entire Column Nicknames for current cells, sheets ActiveCell ActiveSheet Objects of same type form a collection Worksheets(…) is the collection Worksheets(“Sheet1”) refers to one object

Object Hierarchy Application CommandBar Name Window Workbooks Others... Worksheet AutoFilter Name PageSetup PivotTable Range Others... Workbook Charts Name Style Window Worksheets Others... A Workbook may contain many sheets Workbooks(“Book1.xls”).Worksheets(“Sheet1”) Active objects used by default

Cell Properties Name - [A4] Font - Comic Sans Alignment – Right Text color - Red Value - 13 Formula: =SUM(A1:A3)

Assignment Statements Assignment statements change an object This statement puts the number 15 into cell A3: Range(“A3”).value = 15 This statement determines the value in cell A3, and puts it in cell C4: Range(“C4”).value = Range(“A3”).value This one adds the value in A3 to what’s in C4: Range(“C4”).value = Range(“C4”).value + _ Range(“A3”).value

VBA Editor Tools -> Macro -> Visual Basic Editor Shortcut: Alt+F11

VBA Editor – Project Window Worksheets Workbook Modules

VBA Editor – Code Window Color Coding Key Words Comments Errors Objects Properties Methods

Subroutines Begin With Sub MyName() End with End Sub Location Modules Worksheets Workbook UserForm

Read your code Move cursor to cell C7 Write the word “Hello” in the active cell Put cursor in cell C6 Change font color to blue Comments: VBA ignores these lines Stop the macro Macro Name

Relative Addresses Defines one cell relative to another Offset(Row, Column) Rows: positive to right Columns: positive means move down Range(“A3”).Offset(2,4) refers to cell E5 Activecell.Offset(1,0).Select moves cursor down one cell. Recording Macros Relative Cell Address Switch

Variables Short-term storage for information Held in RAM, not stored to disk Disappears when program stops running Needs a name Must be unique Can’t be the same as objects, other variables Can’t be a key word to VBA I often use vName Dim vName

Functions Usually accept an argument Returns a statement or value Abs(vNum) Argument: number stored in vNum Function: Abs( ) Determines absolute value of argument Returns that value Can’t stand alone – must pass Use assignment statement vPosNum = Abs(vNum)

InputBox Function Asks user to supply information Returns a text string Inputbox(“Enter your name: ”,”Name”) Prompt Title Cannot stand alone Range(“B3”) = Inputbox(“Enter your name”,”Name”)

MsgBox Function Used to display a message Simple form: MsgBox message MsgBox “Hi, Bob!” Can ask the user for Yes/No feedback Use Help to explore more complex form

Sample Macro Start with macro name Define a variable to store text string. Use InputBox to get name, store in variable. Use MsgBox to display “Hi, “ plus the stored name. Finish with End Sub

What is VBA Event Driven Mouse click, Change in an object, etc. VBA responds only when an event occurs You decide which events start VBA Powerful Programming Language Object Oriented

Events – Running Macros Selecting Menu Items Tools->Macro->Macros Shortcut Keys Ctrl-Z Buttons