Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) 456-7890 University.

Slides:



Advertisements
Similar presentations
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Week 10.  Protecting Code  Protecting Worksheets  Error Handling  Charts.
Exploring Microsoft Excel 2002 Chapter 9 Chapter 9 A Professional Application: VBA and Date Functions By Robert T. Grauer Maryann Barber Exploring Microsoft.
With Microsoft Excel 2007 Comprehensive 1e© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2007 Comprehensive.
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
CSI 1306 PROGRAMMING IN VISUAL BASIC PART 5. Part 5  1. Procedures  2. Sub Procedures  3. Function Procedures.
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
How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”
VBA Modules, Functions, Variables, and Constants
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
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.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Lab 8 Solver In VBA ► Review – Solver Add-in In Excel ► Solver Add-in In VBA.
Using the Select Case Statement and the MsgBox Function (Unit 8)
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
IE 212: Computational Methods for Industrial Engineering
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Spreadsheet-Based Decision Support Systems Chapter 22:
Tutorial 11 Using and Writing Visual Basic for Applications Code
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
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.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 19: Solver Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
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.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
Chapter 19: The Solver Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 5: More on the Selection Structure
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 1# 1 Using Procedures and Functions What is a procedure? What is a sub procedure (subroutine)? Built-in functions in your code What is a function? CS.
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.
Sundara Ram Matta Feb 16 th, Sundara Ram Matta Feb 16 th, 2015
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Controlling Program Flow with Decision Structures.
Object-Oriented Programming: Inheritance and Polymorphism.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Macros in Excel Using VBA Time Required – 5 hours.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
Excel VBA – Creating Function Procedures Dr Joanna Wyrobek 1.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
VBA - Excel VBA is Visual Basic for Applications
Spreadsheet-Based Decision Support Systems
Microsoft Access 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 Excel
Object-Oriented Programming: Inheritance and Polymorphism
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Presentation transcript:

Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name

2 Overview  15.1 Introduction  15.2 Sub Procedures  15.3 Function Procedures  15.4 Public and Private Procedures  15.5 Applications  15.6 Summary

3 Introduction  Calling sub procedures  Executing function procedures to return a value  Pass variables by reference and by value  Private and public procedures  Two applications which work with calling sub procedures and creating function procedures which pass variables

4 Sub Procedures  Calling Other Procedures  Passing Variables in Procedures

5 Organizing Sub Procedures  You should group various actions into several smaller sub procedures rather than having one large sub procedure in which the entire program is written.  The ideal module structure for your program is to have one Main() sub procedure from which other sub procedures are called. –This Main macro will usually be assigned to a “Start” button on the “Welcome” sheet.  To call another sub procedure, we use the command Call followed by the sub procedure name.

6 Organizing Sub Procedures (cont’d)  For example, consider three different sub procedures, called GetInput(), Calculations(), and CreateReport().  We can then call these three sub procedures from the Main() sub procedure as follows. Sub Main() Call GetInput Call Calculations Call CreateReport End Sub  You can also call other sub procedures from these three sub procedures.

7 Function Procedures  Passing Variables in Functions

8 Creating Function Procedures  Function procedures are sub procedures which can pass variables or return values.  A function procedure can be called from any sub procedure or any other function procedure, using the Call statement. Function FunctionName() …. End Function

9 Passing Variables  To pass a variable in VBA, you should insert the variable as an argument/parameter of the function when it is called. –Call FunctionName(variable to be passed)  If the function will receive a variable as input to the function, the function procedure statement must include a variable name as an argument. –Function FunctionName(variable passed)

10 Passing Variables (cont’d)  We create a function which calculates the sum of two values. –Function Sum(a, b) …… End Function  The variable name used as an argument when the function is called and the variable name used in the function procedure statement to not need to be the same. –They must be of the same data type if the data type is specified in the function statement. –Call Sum(x, y)

11 Passing Variables (cont’d)  The variables a and b in this example do not need to be declared. –They will be the variables used throughout the function procedure.  In this case a and b will assume the respective data types assigned to x and y. –If we had defined our function statement with data types in the argument, we would be restricted to only passing variables of that data type. Function Sum(a As Integer, b As Integer)

12 Passing Variables (cont’d)  To return a value from a function in VBA, you should assign a value to the name of the function procedure. Function Sum(a, b) Sum = a + b End Function

13 Public and Private Procedures  A sub procedure, like a variable, can also be defined as Public or Private.  A private sub procedure is declared by putting the word Private before the Sub statement. –Private sub procedures can only be called from procedures in the same module. –Private sub procedures are also not listed when you try to run a macro in Excel.  A public sub procedure can be called from any other procedure. –The word Public can be put in front of the Sub statement, or the Sub statement can be written without a preceding statement.

14 Public and Private Procedures (cont’d)  Consider four small sub procedures.  Two of these procedures are private: Test1() and Test2() –Since they are in the same module, they can call one another. Private Sub Test1() MsgBox "This is Test1" Call Test2 End Sub Private Sub Test2() MsgBox "This is Test2" End Sub

15 Public and Private Procedures (cont’d)  The third sub procedure called Test3() is public but in another module.  We are not allowed to call either of the private sub procedures in the original module. –That is, Test3() cannot contain the code: Call Test1 or Call Test2.  However, we can call this public procedure from one of our private procedures. Private Sub Test1() MsgBox "This is Test1" Call Test3 End Sub

16 Public and Private Procedures (cont’d)  The fourth sub procedure called Test4() is also public and in the same module as Test1() and Test2().  Even though Test4() is public it can still call the private procedures since they are in the same module. Sub Test4() MsgBox “This is Test4” Call Test1 End Sub

17 Applications  Navigating Procedures  Derived Math Functions

18 Applications  Many functions are already available to us through Excel formulas and VBA math functions; however, there may be specific needs depending on the program or DSS you are developing that may require a customized function. –Navigating Functions –Derived Math Functions

19 Navigating Functions  In many of the case studies we develop, and in general good GUI design, there are several buttons in our workbook used to navigate through the different spreadsheets. –“Continue”, “Next”, “Back”, “Previous”, etc.  Each time one of these buttons is clicked, we want to close the current worksheet, that is hide it, and make the next appropriate worksheet visible.

20 Navigating Functions (cont’d)  Consider a workbook with several worksheets. –“Input”, “Step 1”, “Step 2”, and “Optimization” –These worksheets should be viewed by the user in the order listed. –If we click a “Next” button on the “Input” worksheet we want to hide the “Input” sheet and make the “Step 1” sheet visible Sub NexSheett() Worksheet(“Step 1”).Visible = True Worksheet(“Input”).Visible = False End Sub

21 Navigating Functions (cont’d)  We cannot assign this same macro to the “Next” button found on the “Step 1” worksheet. –If we press “Next” on the “Step 1” sheet, we want to make the “Step 2” sheet visible and hide the “Step 1” sheet. Sub NexSheett() Worksheet(“Step 2”).Visible = True Worksheet(“Step 1”).Visible = False End Sub

22 Navigating Functions (cont’d)  Our solution is to create a function procedure which passes a worksheet name as its variable; we call this the NextSheet() function.  We can capture the desired worksheet name in each unique sub procedure associated with the buttons on each sheet and then call a function to make this worksheet visible and hide the current worksheet. Public name As String Sub InputNext() name = “Step 1” Call NextSheet(name) End Sub Function NextSheet(name) Worksheets(name).Visible = True ActiveSheet.Visible = False End Function

23 Figure 15.1  We use this function structure above to assign related macros to the “Next” button on each of the sheets in our workbook.

24 Navigating Functions (cont’d)  We have a particular sheet that is usually hidden but which can be shown at some point; this is an “Example” worksheet which the user may refer to at times while using our program.  To view this “Example” sheet, there may be a “View Example” button on all other sheets in the workbook. –If this button is clicked, we want to close the current sheet and show the “Example” sheet.  On the “Example” sheet we may have a “Return to Program” button which should re-open the previously visible sheet.

25 Figure 15.2  To perform these actions, we need to capture the original worksheet name in which we first click the “Example” button so that we know which sheet to re- open when the “Return to Program” button is clicked. Public ws As Worksheet Sub ViewExample() Worksheets(“Example”).Visible = True Call CloseCurrent() End Sub Function CloseCurrent() Set ws = ActiveSheet ws.Visible = False End Function Sub ReturnToProgram() Ws.Visible = True Worksheets(“Example”).Visible = False End Sub

26 Derived Math Functions  As we saw in Chapter 13, the pre-defined VBA math functions and trigonometric functions can be used to derive new functions.  To actually create these derived functions in Excel, we create a function procedure and both pass a variable and return some value.

27 Derived Math Functions (cont’d)  To create the Log base n derived math function, we could create the following function. Function LogBaseN(x, n) LogBaseN = Log(x) / Log(n) End Function

28 Derived Math Functions (cont’d)  The values for x and n would need to have been assigned in the sub procedure which calls this function prior to calling the function.  In these functions we may not directly call the function using the Call statement; we can instead just refer to the function name, such as in a Message Box or another function. Sub FindLog() Dim x, n As Integer x = InputBox(“Enter x value of Log base n of x function: “) n = InputBox(“Enter n value of Log base n of x function: “) MsgBox “The value of Log base “ & n & “ of “ & x & “ is: “ & LogBaseN(n, x) End Sub

29 Summary  The ideal module structure for a program is to have one Main() sub procedure from which other sub procedures are called.  Function procedures are similar to sub procedures and follow this basic structure: Function FunctionName() …. End Function  To call another sub or function procedure, use the command Call followed by the sub procedure name.  To pass a variable in VBA, you should insert the variable as an argument/parameter of the function when it is called. Use the following structure to call a function: Call FunctionName(variable to be passed)  A sub procedure, like a variable, can also be defined as Public or Private.

30 Additional Links  (place links here)