University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)

Slides:



Advertisements
Similar presentations
Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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
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.
Writing VBA Macros Record a new Macro, recording only the name, shortcut and description Stop recording Open macro for editing and enter VB code Test.
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.
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)
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
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.
Week 1.  Kate Watson  Checked at least every other day. Queries and general comments welcome.
Macros n Macros are little programs that you can create to automate particular tasks that you may want to execute more easily than having to specify all.
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.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
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.
VBA for Excel. What is a spreadsheet? u An Excel spreadsheet is a set of worksheets  Each worksheets is made up of rows and columns of cells  Rows are.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
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.
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.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
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.
Week 2.  Macros revisited  The VBA Editor  The object model  Using variables  If statements.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-2 1 Lecture Outline Mathematical Expressions String Concatenation Built-in.
# 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute.
Chapter 9 Macros And Visual Basic For Applications.
Visual Basic  Is the language for programming in Word, Access,& Excel  Is the “Environment” in which the programming is done (called the Integrated Development.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
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.
VBA Navigation, Conditionals, and Boxes. VBA Navigation.
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.
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.
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
Outline In this module, the following topics will be covered:
VBA - Excel VBA is Visual Basic for Applications
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introduction to VBA IE 469 Spring 2018.
Exploring Microsoft Excel
VBA Programming for Excel
Microsoft Excel 2007 – Level 2
Presentation transcript:

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE) Objects Assignment Statements Absolute vs Relative Reference Variables and Constants Functions: MsgBox InputBox

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 2 Macros = VBA programs within Office applications for example … Word: change font colour Excel: move cursor to another cell anything you can do from a menu!

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 3 Why create a Macro? Automate tasks: tasks you do repeatedly complex tasks that are error prone Customized functions/applications: perform specific tasks customize menus and toolbars create custom ‘front-ends’ for users

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 4 Macro example #1 Word Add your name at end of MS Word document >Tools >Macro >Record New Macro… go to end of document leave 2 blank lines indent a little type name select change colour change font size

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 5 Macro example #2 Excel Change cell D5 to contain the words “Hi there” in blue text turn on recorder name the macro move to cell D5 set text colour blue type “Hi there” stop recording

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 6 Where is the macro? Macro recorder writes VBA code and stores it with the spreadsheet When workbook is saved, code is too to see the macro code: >Tools >Macros >Visual Basic Editor (or Alt-F11)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 7 VBA Editor (IDE)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 8 VBA Editor – Project Window Worksheets Workbook Modules Forms

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 9 VBA Editor Properties Window Properties of selected object

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 10 VBA Editor Code Window Key Words Comments Code Errors Code: Objects Properties Methods

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 11 What is VBA? powerful programming language object-oriented Objects: cells, worksheets, menus, etc E.g., Range(“D5”) Properties: you control properties of objects E.g., Range(“D5”).Font.Name Assignment Statements: define property state E.g., Range(“D5”).Font.Name = “Arial” event-driven responds only when an event occurs

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 12 Spreadsheet Objects Workbook Worksheet Column Row Cell Range

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 13 VBA Objects Usually referred to by a name Cell B6 is named Range(“B6”) Column B is named Column(“B:B”) Nicknames for current cell/sheet ActiveCell ActiveSheet Objects of same type form a collection Worksheets is the collection name Worksheets(“Sheet1”) refers to one object

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 14 Object Hierarchy A Workbook may contain many sheets Workbooks(“Book1.xls”).Worksheets(“Sheet1”) Workbooks(“Book1.xls”).Worksheets(“Sheet2”) Active objects used by default Workbooks(“Book1.xls”).Worksheets(“Sheet1”).Range(“B6”) ActiveSheet.Range(“B6”) Range(“B6”)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 15 Cell Properties Name: [A4] Font: Comic Sans Alignment: Right Text color: Green Value: 13 Formula: =SUM(A1:A3)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 16 Assignment Statements Used to change an object Put number 15 into cell A3: Range(“A3”).Value = 15 Determine value in cell A3, and put in C4: Range(“C4”).Value = Range(“A3”).Value Add value in A3 to what is in C4: Range(“C4”).Value = Range(“C4”).Value + _ Range(“A3”).Value Increment B5 (add 1): Range(“B5”).Value = Range(“B5”).Value + 1

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 17 Subroutines begin with Sub MyName() end with End Sub Location Modules Worksheets Workbook UserForm

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 18 VBA code Sub ChangeCell() ' ChangeCell Macro ' Macro recorded 07/08/2007 by Andria Hunter ' ' Keyboard Shortcut: Ctrl+m ' Range("D5").Select Selection.Font.ColorIndex = 5 ActiveCell.FormulaR1C1 = "Hi there" Range("D6").Select End Sub

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 19 Stepping through code >View >Toolbars >Debug click in the code window >Step Into to run each line of code watch what happens in the worksheet Make sure your Excel worksheet is visible.

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 20 Relative Reference Defines one cell relative to another Offset(Row, Column) Row: positive moves to right Column: positive moves downward Examples: Range(“A3”).Offset(2,4) – refers to cell E5 Activecell.Offset(1,0).Select – moves down one cell Recording Macros Relative Reference button

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 21 Relative Reference: code Sub ChangeRelative() ' ChangeRelative Macro ' Macro recorded 07/08/2007 by Andria Hunter ' ' Keyboard Shortcut: Ctrl+n ' ActiveCell.Offset(-6, -1).Range("A1").Select Selection.Font.ColorIndex = 5 ActiveCell.FormulaR1C1 = "Hi there" ActiveCell.Offset(1, 0).Range("A1").Select End Sub

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 22 Variables Short-term storage for information held in RAM while program running Examples: Dim vInfo Dim dNum As Double Dim iNum As Integer Dim sText As String

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 23 Constants Similar to variables, but cannot be modified while program is running Const vINFO = 0.25 Const iSIZE As Integer = 10 Const dHEIGHT As Double = 6.5 Const sWORD As String = “Hello” Built-in: vbYes, vbOKOnly, vbRed, etc.

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 24 Functions Abs (number)  returns number vPosNum = Abs(-55)  55 vNum = -2.3 vPosNum = Abs(vNum)  2.3 Range(“B3”) = Abs(vNum)  2.3 MsgBox Abs(vNum)  2.3

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 25 InputBox Function InputBox(message, title) sName = InputBox(“Enter your name:”,”Name”) Range(“B3”) = InputBox(“Enter your name:”,”Name”)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 26 MsgBox Function MsgBox “Hi, Snipe!” MsgBox vPosNum MsgBox “Howdy!”, vbOKOnly, “Greeting”

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 27 Macro from scratch Insert  Module and type code Sub GreetMe() 'This macro asks the user to enter a name, and then 'it uses a message box to display the name. Dim sName As String 'To store user's name 'Ask user for name, and store in variable. sName = InputBox("What's your name?") 'Displays "Hi, " plus the stored name. MsgBox "Hi, " & sName End Sub