ME 142 Engineering Computation I Using Subroutines Effectively.

Slides:



Advertisements
Similar presentations
1 VBA Introduction. Basic Components 2 VBA LANGUAGE OFFICE OBJECTS EXCEL OBJECTS ACCESS OBJECTS WORD OBJECTS OUTLOOK OBJECTS POWERPOINT OBJECTS.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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
Programming in Visual Basic
VBA Modules, Functions, Variables, and Constants
String Variables Visual Basic for Applications 4.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Modeling using VBA. Using Toolbox This is a label This is a button Using the Toolbox select a GUI element and by mouse-click place it on the frame This.
Adding Automated Functionality to Office Applications.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
IE 212: Computational Methods for Industrial Engineering
Operations 343 Spreadsheet Modeling Week 15 Quiz Review Homework Review Course Review Lab session.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
© 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.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
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.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
 An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Using Visual Basic for Applications (VBA) – Project 8.
A Comparison of SAS versus Microsoft Excel and Access’s Inbuilt VBA Functionality Jozef Tarrant, Amadeus Software Ltd. 1 Copyright © 2011 Amadeus Software.
Visual Basic for Applications Macro Programming For Microsoft Office.
Outline Software and Programming Program Structure Tools for Designing Software Programming Languages Introduction to Visual Basic (VBA)
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
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.
ME 142 Engineering Computation I Debugging Techniques.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
ME 142 Engineering Computation I Exam 2 Review VBA.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
# 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.
Lab 2 Introduction to VBA (II) ► Lab 1 revisited - Sub & Function procedures - Data types and variable declaration ► Recording.
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.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
ME 142 Engineering Computation I Input, Output & Documentation.
ME 142 Engineering Computation I Using Subroutines Effectively.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Programming with Microsoft Visual Basic th Edition
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
ME 142 Engineering Computation I Using Excel Arrays in Functions.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
31/01/ Selection If selection construct.
ME 142 Engineering Computation I Input, Output & Documentation.
Controlling Program Flow with Decision Structures.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Visual Basic Fundamental Concepts
IE 8580 Module 4: DIY Monte Carlo Simulation
A variable is a name for a value stored in memory.
Spreadsheet-Based Decision Support Systems
Final Exam Review Part 4 - VBA
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Visual Basic..
CS285 Introduction - Visual Basic
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

ME 142 Engineering Computation I Using Subroutines Effectively

Key Concepts Functions vs. Subprograms Using Subroutines Effectively Declaring Variables

Functions vs. Subprograms  A Function is a self-contained program that receives input through arguments (or parameters), performs calculations, and returns a result  A Subprogram is similar to a function, except it does not return a result

Functions vs. Subprograms  Functions may be used in formulas or expressions  Excel provides hundreds of built-in functions  Functions return a result but may not write to other cells in the spreadsheet  Subprograms may write directly to cells in the spreadsheet

Functions vs. Subprograms  Functions are launched from a formula  Subprograms may be launched from a button or as any macro  Subprograms offer the most flexibility and power, including the use of custom dialog boxes

Using Subroutines Effectively  Longer programs are commonly split up into smaller programs, sometimes referred to as subroutines  Typical scenario: Main program passes information to a subroutine via argument list The subroutine performs calculations and returns results to main program

Sub MathOp() 'Demonstrates using subroutines 'Key Variables: ' Oper - math operation (* - + / ^) ‘Get input from spreadsheet A = Cells(1, 2) B = Cells(2, 2) Oper = Cells(2, 1) 'Call subroutine to perform calculation Call Calculate(A, B, Oper, C) 'Output results to spreadsheet Cells(3, 2) = C End Sub Sub Calculate(x, y, Oper, z) 'subroutine to perform math operation If Oper = "*" Then z = x * y ElseIf Oper = "-" Then z = x - y ElseIf Oper = "+" Then z = x + y ElseIf Oper = "/" Then z = x / y ElseIf Oper = "^" Then z = x ^ y Else MsgBox "Invalid Operation" z = "Error" End If End Sub

Declaring Variables  VBA supports a variety of Data Types, the most common being: Integer Single precision real Double precision real String Variant

Declaring Variables  If you don’t declare the data type for a variable, VBA uses the default data type of variant  Data stored as variant changes types depending on what you do with it  With variant data types, VBA automatically handles conversion  Trade-off is you sacrifice speed and memory

Why Declare Variables  Declaring variables makes your program run faster and use memory more efficiently  You may “force” yourself to declare all variables by including “Option Explicit” as the first statement of your VBA module Option Explicit  When this statement is present, you won’t be able to run your code if it contains any undeclared variables

Variable Declaration Examples  Dim x As Integer  Dim y As Single  Dim z As Double  Dim q As String  Dim a As Single, b As Single, c As Single

Life of Variables  Variables may declared at 3 levels Procedure Module All Modules  For our purposes, Procedure variables will be sufficient

Procedure Variables  Most efficient because VBA frees up the memory they use when the procedure ends  To declare, simply use the Dim statement within the procedure Dim x as Single

Static Variables  Static variables retain their value when the procedure ends Static B as Integer  Could be used to keep a running total or track the number of times a program is called  Variables are purged/reset when The workbook is closed or reopened The reset toolbar button is clicked When “end” statement/error message is executed