Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one.

Slides:



Advertisements
Similar presentations
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
Advertisements

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.
Programming in Visual Basic
How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Sub Programs To Solve a Problem, First Make It Simpler.
String Variables Visual Basic for Applications 4.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Ch 10: More on Variables and Subroutines CP212 Winter 2012.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
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/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
1 Workshop 4 (B): Visual Basic Test Project Mahidol University June 13, 2008 Paul Evenson University of Delaware Bartol Research Institute.
Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”
ME 142 Engineering Computation I Using Subroutines Effectively.
Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
© VBA JETPACK Introduction to VBA.
Macro’s Within excel. Most functionality can be driven from VBA VBA is the programming language that runs inside of excel. It uses visual basic as the.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
ME 142 Engineering Computation I Using Subroutines Effectively.
A Macro to Exchange the Values of Arbitrary Cells.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Programming with Microsoft Visual Basic th Edition
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
How to: Simple User Interface WMCUG 13 th Jan 2010 Gord Vander Vliet Enterprise Tool & Die, LLC Jeff Roark Johnson Controls.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
IE 8580 Module 4: DIY Monte Carlo Simulation
Chapter 4 The If…Then Statement
Spreadsheet-Based Decision Support Systems
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Working with Forms in Visual Basic
Even More VBA IE 469 Spring 2017.
Microsoft Access Illustrated
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
Lesson Objectives Aims Key Words
Tonga Institute of Higher Education
HOW TO CREATE A CLASS Steps:
Variables Title slide variables.
CS285 Introduction - Visual Basic
Tonga Institute of Higher Education
Chapter 8 - Functions and Functionality
Microsoft Office Excel 2003
3.2 Working with Data Scope of variables 29/07/2019.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Scope

Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one written by many people, it is not so easy In fact, we may want to be able to use the same name in different parts of a large program, without conflicts

Scope Recall our family analogy. Normally in a family each person has a different first (given) name (or if not they are distinguished by nicknames) People in different families can have the same name; there are lots of Mary’s and William’s If there is a famous person with a certain name, then most people think of that person when they hear the name (Oprah, for example) But if a person in your family has that name, you think of them first

Scope and Lifetime of Variables Variables can be declared within a subprocedure or function, or at the level of the module A variable that is declared at the level of the module (also called a global variable) can be seen by all the subprocedures and functions in the module, and is in existence as long as the program is running. It is like a famous person that everyone knows by name A variable declared within a subprocedure or function (also called a local variable) can only be seen there, and exists only as long as the subprocedure or function is running 4

An Example (code follows) To see the difference between local and global variables, try running the ScopeIllustration Excel sheet In this sheet, the value of global variable varA is displayed in Cell(1,1) (A1) and the value of local variable varB is displayed in Cell(1,2) (B1)

Global vs Local: The Code Option Explicit '************************************************************************* ' Illustrate scope '************************************************************************* Dim varA As Double 'create a global variable Sub WorkBook_Open() varA = 0 End Sub Sub ChangeValues() Dim varB As Double ‘varB is local varA = varA + 1 varB = varB + 1 Cells(1, 1).Value = varA Cells(1, 2).Value = varB End Sub 6 Each time you click the button, varA and varB are incremented This procedure sets varA to 0 when the workbook opens

What happens… varA exists as long as the workbook is open. Each time we add 1, its value increases varB is created anew each time we press the button, when procedure ChangeValues is called. VBA sets a new variable to 0, so each time the value we see, after adding 1, is 1 This illustrates the difference in lifetime between local and global variables

Tricky Case for Scope Normally a global variable is visible everywhere in the module, but what if a local and a global variable have the same name? It’s as if you have a sister named Oprah. Everyone else thinks Oprah is Oprah Winfrey, but you think first of your sister Likewise, inside the procedure with the local variable named varA, the code cannot see or access the global variable; it uses that name for its local variable 8

Advice on Global Variables Use global variables very sparingly. A program with all variables global easily becomes confusing and error-prone Use them for quantities that have to be used by different procedures If a quantity is only needed within a single procedure, use a local variable for it Give global variables names that are unique; don’t reuse the same name for a local variable! Global constants are fine; just give them unique names 9