Spreadsheet Models for Managers: Session 14 14/1 Copyright © 1994-2011 Richard Brenner Spreadsheet Models for Managers Session 14 Using Macros II Function.

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.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Some computer fundamentals and jargon Memory: Basic element is a bit – value = 0 or 1 Collection of “n” bits is a “byte” Collection of several bytes is.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Spreadsheet Models for Managers: Session 5 5/1 Copyright © Richard Brenner Spreadsheet Models for Managers Session 5 Course Project Proposals.
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.
Spreadsheet Models for Managers: Session 2 2/1 Copyright © Richard Brenner Spreadsheet Models for Managers Session 2 Analysis and Synthesis Array.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Control Statements Making Decision in Your Program.
Spreadsheets With Microsoft Excel ® as an example.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Introduction to a Programming Environment
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Matrix Mathematics in MATLAB and Excel
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Excel VBA University of Chicago Graduate School of Business Introduction to Computer Based Models Bus Mr. Schrage Spring 2003.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
IE 212: Computational Methods for Industrial Engineering
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
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
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
259 Lecture 13 Spring 2013 Advanced Excel Topics – Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.
Spreadsheet Models for Managers: Session 4 4/1 Copyright © Richard Brenner Spreadsheet Models for Managers Session 4 Temporal Response Array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
1 CS 106 Computing Fundamentals II Chapter 75 “Arrays” Herbert G. Mayer, PSU CS Status 7/31/2013 Initial content copied verbatim from CS 106 material developed.
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
ME 142 Engineering Computation I Using Subroutines Effectively.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Advanced Excel Topics – Arrays
User-Written Functions
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Loops BIS1523 – Lecture 10.
Matrix Operations Monday, August 06, 2018.
2-D Lists Taken from notes by Dr. Neil Moore
JavaScript: Functions.
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Repeating Instructions And Advance collection
Arrays.
2-D Lists Taken from notes by Dr. Neil Moore
Spreadsheet Models for Managers
Presentation transcript:

Spreadsheet Models for Managers: Session 14 14/1 Copyright © Richard Brenner Spreadsheet Models for Managers Session 14 Using Macros II Function Macros for Arrays in Visual Basic for Applications Last revised: July 6, 2011

Spreadsheet Models for Managers: Session 14 14/2 Copyright © Richard Brenner Review of last time: Using Macros I Macros reduce maintenance costs, reduce errors, and speed development Two kinds of macros: function macros and command macros Two languages — VBA and XLM Basic VBA macro structure Variable declarations Computations Returning values Objects have properties and methods Methods and properties use postfix syntax Caller, Column, Row, Columns, Rows, Count, Application object

Spreadsheet Models for Managers: Session 14 14/3 Copyright © Richard Brenner Array function macros Last time we discussed scalar function macros — macros that return a single value Array function macros return a rectangular array of values Array function macros are powerful Manage blocks of data at high levels Support array computations Reduce maintenance costs and development time But they require more machinery Iteration Understanding Excel’s object model Dynamic allocation They’re worth it Let’s start with a few simple examples

Spreadsheet Models for Managers: Session 14 14/4 Copyright © Richard Brenner Example 1: Add two 3x3 arrays Returns the sum of two 3x3 ranges This works, but it’s unnecessary: Excel can already do this Let’s see how we built this Function ArraySum(range1 As Range, range2 As Range) As Variant Dim i As Integer, j As Integer 'iteration variables Dim answerArray(3, 3) For i = 1 to 3 For j = 1 to 3 answerArray(i, j) = range1(i, j) + range2(i, j) Next j Next i ArraySum = answerArray End Function

Spreadsheet Models for Managers: Session 14 14/5 Copyright © Richard Brenner ArraySum: How to build it Create a module Insert module options Write a function macro in the module. Start with function statement, which includes the function name and its argument list As usual, the body of the function definition contains two parts: Variable declarations Computations Function ArraySum(range1 As Range, range2 As Range) As Variant End Function Since Variant is the default data type, this is optional

Spreadsheet Models for Managers: Session 14 14/6 Copyright © Richard Brenner Use Dim statement to declare the answer array Use an assignment statement to return the value Function ArraySum(range1 As Range, range2 As Range) As Variant Dim answerArray(3, 3) ArraySum = answerArray End Function ArraySum: Create the answer array

Spreadsheet Models for Managers: Session 14 14/7 Copyright © Richard Brenner How to use iteration Iteration is a repeat form Many varieties of iteration in VBA; we use a simple one: Iteration can be nested; that’s how we handle two-dimensional arrays You must include variable declarations for iteration variables For = To Next For = To Next

Spreadsheet Models for Managers: Session 14 14/8 Copyright © Richard Brenner Now iterate through the argument array Notice that declarations are needed for the iteration variables Function ArraySum(range1 As Range, range2 As Range) As Variant Dim answerArray(3, 3) Dim i As Integer, j As Integer For i = 1 To 3 For j = 1 To 3 answerArray(i, j) = range1.Cells(i, j) + range2.Cells(i, j) Next j Next i ArraySum = answerArray End Function

Spreadsheet Models for Managers: Session 14 14/9 Copyright © Richard Brenner Example 2: Multiply an array by a constant The macro takes two arguments: array of any size constant It returns an array equal to array * constant Plan of calculation: Create an array to hold the answer Iterate through the argument array: Pick up the array element Multiply it by the constant Insert the result into the corresponding place in the answer array This one is trickier because we don’t know the size of the array This is really useless, because Excel can do this without a macro

Spreadsheet Models for Managers: Session 14 14/10 Copyright © Richard Brenner Now build a macro to do this Create a module as before (if needed) Write a function macro in the module. Start with function statement, which includes the function name and its argument list As usual, the body of the function definition contains two parts: Variable declarations Computations Function Multiply(argRange As Range, factor As Double) As Variant End Function Since Variant is the default data type, this is optional

Spreadsheet Models for Managers: Session 14 14/11 Copyright © Richard Brenner Dynamic arrays We can’t always know the size of an array when we’re writing the program—it can depend on the runtime environment. We need the ability to allocate space for it on the fly: use ReDim ReDim declares the array’s size at execution time To declare a two dimensional array 5x10: Sometimes the size depends on the sizes of arguments: inputRows = argRange.Rows.Count inputColumns = argRange.Columns.Count ReDim answerArray(inputRows,inputColumns) ReDim answerArray(5,10)

Spreadsheet Models for Managers: Session 14 14/12 Copyright © Richard Brenner Create the answer array Steps: At compile time: Declare the array At run time: Extract the sizes of the argument array Resize the answer array Function Multiply(argRange As Range, factor As Double) As Variant Dim inputRows As Integer, inputColumns As Integer Dim answerArray inputRows = argRange.Rows.Count inputColumns = argRange.Columns.Count ReDim answerArray(inputRows, inputColumns) End Function

Spreadsheet Models for Managers: Session 14 14/13 Copyright © Richard Brenner Now iterate through the argument array Notice that declarations are needed for the iteration variables Function Multiply(argRange As Range, factor As Double) As Variant Dim i As Integer, j As Integer Dim inputRows As Integer, inputColumns As Integer Dim answerArray inputRows = argRange.Rows.Count inputColumns = argRange.Columns.Count ReDim answerArray(inputRows, inputColumns) For i = 1 To inputRows For j = 1 To inputColumns answerArray(i, j) = argRange.Cells(i, j) * factor Next j Next i End Function

Spreadsheet Models for Managers: Session 14 14/14 Copyright © Richard Brenner Finally, return the value Function Multiply(argRange As Range, factor As Double) As Variant Dim i As Integer, j As Integer Dim inputRows As Integer, inputColumns As Integer Dim answerArray inputRows = argRange.Rows.Count inputColumns = argRange.Columns.Count ReDim answerArray(inputRows, inputColumns) For i = 1 To inputRows For j = 1 To inputColumns answerArray(i, j) = argRange.Cells(i, j) * factor Next j Next i Multiply = answerArray End Function

Spreadsheet Models for Managers: Session 14 14/15 Copyright © Richard Brenner More useful example You’re writing a plan for producing a product line of hose couplings. You must consider materials requirements and sales projections. The materials requirements are given as an array of Coupling-Type by Diameter [name: BrassContent ] Expected shipments are given as an array of Coupling-Type-and-Size by Month [name: ProjectedSales ] The dimensions of these tables are incompatible. We need to “unwind” the BrassContent range.

Spreadsheet Models for Managers: Session 14 14/16 Copyright © Richard Brenner Hose coupling data

Spreadsheet Models for Managers: Session 14 14/17 Copyright © Richard Brenner Unwinding the brass content data

Spreadsheet Models for Managers: Session 14 14/18 Copyright © Richard Brenner Now build a macro to do this Create a module as before (if needed) Insert module options Write a function macro in the module. Start with function statement, which includes the function name and its argument list As usual, the body of the function definition contains two parts: Variable declarations Computations Function VLineUp(argRange As Range) As Variant End Function Since Variant is the default data type, this is optional

Spreadsheet Models for Managers: Session 14 14/19 Copyright © Richard Brenner Overall approach Receive the BrassContent array as an argument (3x3) Assemble a 9x1 array by stacking the columns of BrassContent Return the 9x1 array We will iterate through the cells of BrassContent, inserting its values into the result

Spreadsheet Models for Managers: Session 14 14/20 Copyright © Richard Brenner Planning the iteration Where will the (i, j) element of the input end up? (j-1) * 3 + i This isn’t quite it, though: it’s a 1x9 array. We have to transpose it Function VLineUp(argRange As Range) as Variant Dim answerArray(9), i As Integer, j As Integer For j = 1 To 3 For i = 1 To 3 answerArray((j - 1) * 3 + i) = argRange.Cells(i, j) Next i Next j VLineUp = answerArray End Function

Spreadsheet Models for Managers: Session 14 14/21 Copyright © Richard Brenner Finally Function VLineUp(argRange As Range) as Variant Dim answerArray, i As Integer, j As Integer For j = 1 To 3 For i = 1 To 3 answerArray((j - 1) * 3 + i) = argRange.Cells(i, j) Next i Next j VLineUp = Application.WorksheetFunction.Transpose(answerArray) End Function

Spreadsheet Models for Managers: Session 14 14/22 Copyright © Richard Brenner Limitations of VLineUp It assumes that the input is 3x3 It can produce only a 1x9 In real problems, it’s much more useful if you can avoid assumptions about array sizes Things might change You might want to use the macro for another project A more useful unwinder wouldn’t assume 3x3 It would unwind any square range into a single column How can we do that? Use dynamic arrays

Spreadsheet Models for Managers: Session 14 14/23 Copyright © Richard Brenner Dynamic unwinder Function VLineUp(argRange As Range) As Variant Dim answerArray, i As Integer, j As Integer Dim inputColumns As Integer, inputRows As Integer inputRows = argRange.Rows.Count inputColumns = argRange.Columns.Count ReDim answerArray(inputRows*inputColumns) For j = 1 To inputColumns For i = 1 To inputRows answerArray((j - 1) * inputRows + i) = argRange.Cells(i, j).Value Next i Next j VLineUp = answerArray End Function

Spreadsheet Models for Managers: Session 14 14/24 Copyright © Richard Brenner Resize and Offset The functionality of the worksheet function OFFSET is available in VBA in a slightly different form Offset is a Range method that works like the worksheet function OFFSET, but it takes only the first two arguments. Resize is a Range method that returns a range with a different shape and size, but 0,0 offset. You can chain them together: theRange.Offset(i, j) theRange.Resize(i, j) theRange.Offset(3,2).Resize(1,4)

Spreadsheet Models for Managers: Session 14 14/25 Copyright © Richard Brenner A tip about arrays Excel worksheet functions (mostly) work for ranges as well as individual cells. When you use them inside a VBA macro, you don’t need to iterate (see previous slide). If you assign their return value to the function name, you’re done

Spreadsheet Models for Managers: Session 14 14/26 Copyright © Richard Brenner The main points Iteration Dynamic arrays Using the Set statement for objects

Spreadsheet Models for Managers: Session 14 14/27 Copyright © Richard Brenner Reference readings Rob Bovey, Stephen Bullen, John Green, Robert Rosenberg, Excel 2002 VBA Programmers Reference. Birmingham, UK: Wrox Limited. This is a whole lot more than you need for this course. Don’t even think of looking at this unless you want to dive into programming. But if you want to, it’s a solid reference. On line help for VB takes some getting used to, but it is serviceable. Readings: Excel Macros in Visual Basic for Applications

Spreadsheet Models for Managers: Session 14 14/28 Copyright © Richard Brenner Preview of next time: Spreadsheet Tools for Managers Rough out your command macros using the macro recorder Many commands can’t be recorded Separate the presentation function from the maintenance function Using the Set statement for objects Use templates to collect data from the organization Avoiding putting macros in templates Distribute macro collections as add-ins