259 Lecture 13 Spring 2013 Advanced Excel Topics – Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

CS&E 1111 Exfunctions Using Functions in Excel Objectives: Using Excel functions l SUM, MIN, MAX, AVERAGE, COUNT, COUNTA l ROUND l COUNTIF, SUMIF, AVERAGEIF.
Review Ch. 15 – Spreadsheet and Worksheet Basics © 2010, 2006 South-Western, Cengage Learning.
EXCEL.
FORMULAS & FUNCTIONS EXCEL. Input A collection of information Data typed into the spreadsheet Output Worksheet Results.
 Microsoft Excel is an electronic spreadsheet.  As with a paper spreadsheet, you can use Excel to organize your data into rows and columns and to perform.
Excel – Study Guide #2.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Additional Excel features More functions Filtering in Excel Copying Mail merge.
Matrix Mathematics in MATLAB and Excel
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Regression Basics For Business Analysis If you've ever wondered how two or more things relate to each other, or if you've ever had your boss ask you to.
Recall that a square matrix is one in which there are the same amount of rows as columns. A square matrix must exist in order to evaluate a determinant.
Computer Programming (TKK-2144) 13/14 Semester 1 Instructor: Rama Oktavian Office Hr.: M.13-15, W Th , F
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next Generation.
Managing Business Data Lecture 8. Summary of Previous Lecture File Systems  Purpose and Limitations Database systems  Definition, advantages over file.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
ECON 1150 Matrix Operations Special Matrices
Syntax MROUND(number,multiple) Number is the value to round. Multiple is the multiple to which you want to round number. Remark MROUND rounds up, away.
IE 212: Computational Methods for Industrial Engineering
AGB 260: Agribusiness Information Technology Arrays and Array Formulas.
Statistical Analysis with Excel (PREVIEW). Spreadsheet Programs First developed in 70s –VisiCalc Dan Bricklin and Bob Frankston –Operated on Apple II.
 What is a formula in Excel?  A formula is statement written by the user to be calculated. Formulas can be as simple or as complex as the user wants.
Excel Spreadsheet basics. Excel Sheets and Books  Spreadsheet: tool to analyze, chart and manage data for personal, business and financial use Worksheet:
Lecture 2 Using Excel OVERVIEW Complex Formulas Basic Functions Arrays Sorting Data Outlining Data Filtering Data Formatting Tables.
Chapter 8 Arrays and Strings
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Copyright © 2008 Prentice-Hall. All rights reserved. What Can I Do with a Spreadsheet.
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.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
AGB 260: Agribusiness Information Technology Arrays and Array Formulas.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Information Processing Notes for beginning our Excel Unit.
11 Chapter 2: Formulas and Functions Chapter 02 Lecture Notes (CSIT 104) Exploring Microsoft Office Excel 2007.
1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.
Lesson 11-1 Matrix Basics and Augmented Matrices Objective: To learn to solve systems of linear equation using matrices.
Spreadsheet Basics What is a Spreadsheet? A spreadsheet (or worksheet) is an arrangement of cells in columns and rows used to organize, analyze, calculate,
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Excel Spreadsheets Formatting and Functions ICS100 – Spring 2007 D. Pai.
- Professor Kurt Brandquist - IT133 Software Applications Unit 6 Seminar: Formulas and Functions Wednesday, 6/22/11.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next Generation.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
How to Grade with Excel Basics and Formulas. How to Grade with Excel  A cell is the cross-section of row and column  Whatever cell is selected is shown.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Excel Introduction to computers. Excel 2007 Starting the Excel program.
Chapter 2 The Web and Business Applications. Agenda Cell referencing Fill handle Enter a formula using pointing method Comment Formatting Date arithmetic.
Chapter 9 Introduction to Arrays Fundamentals of Java.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Arrays Chapter 7.
Microsoft Excel.
AGB 260: Agribusiness Data Literacy
Advanced Excel Topics – Arrays
MS-Excel Part 1.
Lessons Vocabulary Excel 2016.
Introduction to Excel ICL and MSO.
Lesson 4 Using Basic Formulas
Dr Huw Owens Room B44 Sackville Street Building Telephone Number 65891
POWERPOINT (PPT) KEY Elements: Know these features
Unit G: Using Complex Formulas, Functions, and Tables
Mathematical Formulas and Excel
Presentation transcript:

259 Lecture 13 Spring 2013 Advanced Excel Topics – Arrays

Topics  Arrays  Why Use Arrays?  Declaring an Array  Working with Arrays  Multiple Valued User Defined Functions  Cell Ranges as Arguments  Array Formulas  Array Outputs for User Defined Functions 2

Arrays  Definition: An array is a group of elements of the same type that have a common name.  A specific element in an array is referred to by using the array name and an index number.  Arrays are variables that can store multiple values.  Arrays very closely resemble matrices used in mathematics in both form and operation. 3

Why Use Arrays? (1)Arrays make it possible to solve problems using “Matrices” and “Linear Algebra” techniques. (2)Arrays make it possible for user defined functions to work with cell ranges. For example use the formula =MySum(A1:A5) instead of the formula =MySum(A1,A2,A3,A4,A5). (3)Arrays make it possible for a user defined function to return multiple values. For example: =BestFitLine() returns the “Slope” of the best-fit line for a set of N ordered pairs of data, {(x1, y1), (x2, y2), …, (xN, yN)}. =BestFitLine() returns the “YIntercept” of the best-fit line for a set of N ordered pairs of data, {(x1, y1), (x2, y2), …, (xN, yN)}. =BestFitLine() returns the “Equation” of the best-fit line for a set of N ordered pairs of data, {(x1, y1), (x2, y2), …, (xN, yN)}. =BestFitLine() returns the slope, y-intercept, and equation for the best-fit line for a set of N ordered pairs of data, {(x1, y1), (x2, y2), …, (xN, yN)} in an array. (4)Arrays often simplify VBA code. 4

Declaring Arrays  Just like any other variable, arrays should be declared before they are used.  To declare an array, both the size of the array and the data type of the values that the array will hold must be specified. 5

Declaring Arrays  Here are some examples of array declarations: ‘Declare a “1-dimensional” array named “A” that has 5 elements. ‘Think of A as a matrix with 1 row and 5 columns. Dim A(1 To 5) As Integer ‘Declare a “1-dimensional” array named “Names” that has 10 elements. ‘Think of Names as a matrix with 1 row and 10 columns. Dim Names(1 To 10) As String ‘Declare a “2-dimensional” array named “Table” that has 24 elements. ‘Think of Table as a matrix with 12 rows and 2 columns. Dim Table(1 To 12, 1 To 2) As Double 6

Working with Arrays  Remember that a specific element in an array is referred to by using the array name and index number.  For example, A(4) is the fourth element in 1-dimensional array A, or equivalently, the element from Row 1, Column 4 of array A. Names(1) is the first string in 1-dimensional array Names, or equivalently, the element from Row 1, Column 1 of array Names. Table(2,3) is the element from Row 2, Column 3 of 2-dimensional array Table. 7

Working with Arrays  Example 1: Set every element in the array “A” to the value of 1. ‘Assuming that array “A” has already been declared as well as other variables. For K = 1 To 5 A(K) = 1 Next K 8

Working with Arrays  Example 2: Set every element in the array “Table” to a random number in the interval (0,1). ‘Assuming that array “Table” has already been declared as well as other variables For Col = 1 To 2 For Row = 1 To 12 Table(Row,Col) = Rnd() Next Row Next Col 9

Working with Arrays  Example 3: Add up all of the elements in the array “A” pre-loaded with random numbers. ‘Assuming that array “A” has already been declared as well as other variables ‘Also assuming that array A has already been pre- loaded Total = 0 For K = 1 To 5 Total = Total + A(K) Next K 10

Working with Arrays  Example 4: Increment every element in the array “A” by 1. ‘Assuming that array “A” has already been declared as well as other variables ‘Also assuming that array A has already been pre- loaded For K = 1 To 5 A(K) = A(K) + 1 Next K 11

Working with Arrays  Example 5: Determine the frequency of each letter A-Z contained in the phrase “In any right-angled triangle, the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares whose sides are the two legs” without using arrays. ‘Assume that variables have been previously declared ‘Set counters A-Z to zero Count_A = 0 Count_B = 0 Count_C = 0 ‘ etc., … Phrase = “In any right-angled triangle, the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares whose sides are the two legs” Phrase = UCase(Phrase) For K = 1 To Len(Phrase) Ch = Mid(Phrase,K,1) If Ch <> “ “ Then If Ch = “A” Then Count_A = Count_A + 1 If Ch = “B” Then Count_B = Count_B + 1 If Ch = “C” Then Count_C = Count_C + 1 ‘ etc., … End If Next K ‘Frequencies for letters A-Z are contained in Count_A, Count_B, …, Count_Z 12

Working with Arrays  Example 6: Determine the frequency of each letter A-Z contained in the phrase “In any right-angled triangle, the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares whose sides are the two legs” using arrays. Dim Count(1 to 26) As Integer ‘Assume that all other variables have been previously declared ‘Set counters for A-Z to zero For K = 1 To 26 Count(K) = 0 Next K Phrase = “In any right-angled triangle, the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares whose sides are the two legs” Phrase = UCase(Phrase) For K = 1 To Len(Phrase) Ch = Mid(Phrase,K,1) If Ch <> “ “ Then Index = Asc(Ch) - 64 Count(Index) = Count(Index) + 1 End If Next K ‘Frequencies for letters A-Z are now contained in Count(1-26) ABCDEFGHIJKLMNOPQRSTUVWXYZ

User Defined Functions and Arrays  Recall that in addition to simplifying VBA code, other advantages of using “Arrays” were to (1) make it possible for user defined functions to work with cell ranges, and (2) make it possible for a User Defined Function to return multiple values. 14

Cell Ranges as Arguments  It is possible to pass a range of worksheet cells into a User Defined Function as an argument to the function.  A range of worksheet cells passed into a User Defined Function will behave like an “Array”. 15

Cell Ranges as Arguments  Example 7: A function that takes entries from a range of column values as well as two numbers as inputs! Function Sample(Q As Variant, X As Double, Y As Double) As Variant ‘etc., … End Function  To call the function, use =Sample(A2:A7,12,15.2) 16 AB

Cell Ranges as Arguments  Example 8: A function that takes more than one array as an argument. Function Sample2(Q As Variant, R As Variant, Z As Variant) As Variant ‘etc., … End Function  To call this function, use =Sample2(B6:B8,C1:D2,E4:G4) 17 ABCDEFGH

Cell Ranges as Arguments  Example 8: Create a user defined function MyDet22() that will return the determinant of a 2x2 matrix given a range of 2x2 cell range.  For example, MyDet22(C2:D3) should return a value of -2. Function MyDet22(Mat As Variant) As Double ‘Returns the “Determinant” of a 2x2 matrix ‘The 2x2 matrix is passed into this function as a cell range MyDet22 = Mat(2,2)*Mat(1,1)- Mat(2,1)*Mat(1,2) End Function 18 ABCDE

Array Formulas  When working with arrays, it is useful to know how to work with array formulas!  An array formula is a formula that can perform multiple calculations on one or more of the items in an array.  Array formulas can return either multiple results or a single result.  For example, an array formula can be placed in a range of cells, such as a column or row, and used to calculate a column or row of subtotals.  An array formula can also be placed in a single cell and be used to calculate a single amount.  An array formula that resides in multiple cells is called a multi-cell formula, and an array formula that resides in a single cell is called a single-cell formula. 19

Array Formulas  Example 9: Go to the help file web page for Guidelines and examples of array formulas.Guidelines and examples of array formulas  Try the examples Create a multi-cell array formula Create a single-cell array formula 20

Array Formulas  Ex. 9 (cont.): Create a multi-cell array formula 1.Open a new, blank workbook. 2.Copy the example worksheet data, and then paste it into the new workbook starting at cell A1. 3.To multiply the values in the array (the cell range C2 through D11), select cells E2 through E11, and then enter the following formula in the formula bar: =C2:C11*D2:D11 4.Press CTRL+SHIFT+ENTER.  Excel surrounds the formula with braces ({ }) and places an instance of the formula in each cell of the selected range.  What appears in column E is the total sales amount for each car type for each salesperson. 21

Array Formulas  Ex. 9 (cont.): Create a single-cell array formula 1.In cell A13 of the workbook, type Total Sales. 2.In cell B13, type the following formula, and then press CTRL+SHIFT+ENTER =SUM(C2:C11*D2:D11)  In this case, Excel multiplies the values in the array (the cell range C2 through D11) and then uses the SUM function to add the totals together.  Note that the single-cell formula (in cell B13) is completely independent of the multi-cell formula (the formula in cells E2 through E11). 22

Array Formulas  For the most part, array formulas use standard formula syntax.  All array formulas begin with an equal sign, and any of the built-in Excel functions can be used in array formulas.  The key difference is that when using an array formula, CTRL+SHIFT+ENTER must be pressed to enter the array formula.  When this is done, Excel surrounds the array formula with braces — if the braces are typed manually, the formula will be converted to a text string, and will not work. 23

Array Formulas  When working with multi-cell formulas, one needs to follow these rules: Select the range of cells to hold output results before entering the formula. The contents of an individual cell in an array formula cannot be changed. An entire array formula can be moved or deleted, but part of an array formula cannot be moved or deleted. To “shrink” an array formula, first delete the existing formula, then start over. To delete an array formula, select the entire formula press DELETE, and then press CTRL+SHIFT+ENTER. Blank cells cannot be inserted into or deleted from a multi-cell array formula. 24

Array Outputs for User Defined Functions  For multiple values output as an array for a user defined function, use the idea of array formulas!  For example, try the following function, Sample(): Function Sample(A As Variant, B As Variant) As Variant Dim K As Integer, C(1 To 4, 1 To 2) As Integer For K = 1 To 4 C(K, 1) = A(K) + B(K) C(K, 2) = A(K) + A(K) Next K Sample = C End Function 25

26 References  Notes on Arrays – John Albers  Array Formulas (pp ) – Excel Help Files (Guidelines and examples of array formulas) us/excel-help/guidelines-and- examples-of-array-formulas- HA aspx us/excel-help/guidelines-and- examples-of-array-formulas- HA aspx