Name = array(x,y,...,z) the indexing starts at zero, i.e. Name(0) = x. Programming Excel/VBA Part II (A.Fring)



Similar documents
One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Using the For Each Statement to 'loop' through the elements of an Array

Typical Linear Equation Set and Corresponding Matrices

Visual Basic Programming. An Introduction

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

The FTS Interactive Trader lets you create program trading strategies, as follows:

Numerical Methods with Excel/VBA:

Multiple Choice: 2 points each

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

SPSS for Windows importing and exporting data

EXCEL SOLVER TUTORIAL

Numerical Methods with Excel/VBA:

RIT Installation Instructions

The FTS Real Time System lets you create algorithmic trading strategies, as follows:

Chapter 7: Additional Topics

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Excel & Visual Basic for Applications (VBA)

I PUC - Computer Science. Practical s Syllabus. Contents

Here are some examples of combining elements and the operations used:

Chapter 07: Instruction Level Parallelism VLIW, Vector, Array and Multithreaded Processors. Lesson 05: Array Processors

Linear Algebra and TI 89

How To Code In Vba Vb.Io (For Ahem)

Excel supplement: Chapter 7 Matrix and vector algebra

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

Connecting to an Excel Workbook with ADO

Reciprocal Cost Allocations for Many Support Departments Using Spreadsheet Matrix Functions

Excel Basics By Tom Peters & Laura Spielman

Excel & Visual Basic for Applications (VBA)

MS-EXCEL: ANALYSIS OF EXPERIMENTAL DATA

SOAL-SOAL MICROSOFT EXCEL 1. The box on the chart that contains the name of each individual record is called the. A. cell B. title C. axis D.

Solving Systems of Linear Equations Using Matrices

General Sampling Methods

Efficient Data Structures for Decision Diagrams

Project 2: Bejeweled

OpenOffice.org 3.2 BASIC Guide

Introduction to Data Structures

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

NOTES ON LINEAR TRANSFORMATIONS

Final Exam Review: VBA

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

PULSE Automation programming in Visual Basic. From BK training lectures arranged by Jiří Tůma & Radovan Zadražil

5 Arrays and Pointers

Using Loops to Repeat Code

Using Microsoft Project 2000

Answers to Review Questions Chapter 7

3.2 Matrix Multiplication

Data analysis and regression in Stata

ECDL. European Computer Driving Licence. Spreadsheet Software BCS ITQ Level 2. Syllabus Version 5.0

PHP Arrays for RPG Programmers

What is Microsoft Excel?

A brief introduction of PMT, IPMT and PPMT Excel functions

2. The Open dialog box appears and you select Text Files (*.prn,*.txt,*.csv) from the drop-down list in the lower right-hand corner.

Section 4.1 Rules of Exponents

Positional Numbering System

Formulas & Functions in Microsoft Excel

Supplemental Worksheet Problems To Accompany: The Pre-Algebra Tutor: Volume 1 Section 9 Order of Operations

AP Computer Science Java Mr. Clausen Program 9A, 9B

THE DIMENSION OF A VECTOR SPACE

A Comparison of SAS versus Microsoft Excel and Access s Inbuilt VBA Functionality

Chapter 19. General Matrices. An n m matrix is an array. a 11 a 12 a 1m a 21 a 22 a 2m A = a n1 a n2 a nm. The matrix A has n row vectors

Excel: Introduction to Formulas

Importing Data from a Dat or Text File into SPSS

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Using SAS DDE to Control Excel

Creating Datalogging Applications in Microsoft Excel

HTML/OS Tag Reference

LabVIEW Day 6: Saving Files and Making Sub vis

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

BulkSMS Text Messenger Product Manual

Accounts Receivable: Importing Remittance Data

ab = c a If the coefficients a,b and c are real then either α and β are real or α and β are complex conjugates

Lecture 22: C Programming 4 Embedded Systems

OpenOffice.org 3.1 BASIC Guide

arrays C Programming Language - Arrays

Module 2 - Multiplication Table - Part 1-1

Drawing a histogram using Excel

COMPUTER SCIENCE. Paper 1 (THEORY)

Writing Control Structures

1 Description of The Simpletron

Computational Mathematics with Python

What's New in ADP Reporting?

Directions for the Well Allocation Deck Upload spreadsheet

3.Basic Gate Combinations

Step 2: Save the file as an Excel file for future editing, adding more data, changing data, to preserve any formulas you were using, etc.

AWK: The Duct Tape of Computer Science Research. Tim Sherwood UC Santa Barbara

Labels, Labels, and More Labels Stephanie R. Thompson, Rochester Institute of Technology, Rochester, NY

Final Examination Semester 2 / Year 2011

Tutorial Segmentation and Classification

West Virginia University College of Engineering and Mineral Resources. Computer Engineering 313 Spring 2010

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Lecture 18 Regular Expressions

Section IV.1: Recursive Algorithms and Recursion Trees

Transcription:

Arrays/Array functions Arrays are VBA variables which can store more than one item. the items held in an array are all of the same variable type one refers to an item by the array name and a number syntax: declaration: Dim Name(number) usage: Name(x) where 0 x number by default the indexing starts at 0 - Expl.: an array with three items named A declaration: Dim A(2) usage: A(0) = 5 A(1) = 3 A(2) = 6 note: A(3) is not defined 75 You may change the index set from its default value syntax: declaration: Dim Name(x to y) usage: Name(z) where x z y - Expl.: an array with three items named A declaration: Dim A(8 to 10) usage: A(8) = 5 A(9) = 3 A(10) = 6 note: A(6), A(7), A(11), A(12),... are not defined Alternatively you can also use the array function syntax: declaration: usage: Dim Name as variant Name = array(x,y,...,z) the indexing starts at zero, i.e. Name(0) = x 76

Example 1: Sub Example1() Dim A(8 To 10) A(8) = 2 A(9) = 3 A(10) = A(8) + A(9) Range("A10").Value = A(10) End Sub - writes 5 into the cell A10 of the active worksheet Example 2: Sub Example2() Dim B As Variant B = Array(2, 3, 4, 5) Range("A13").Value = (B(0) + B(1)) /B(3) End Sub - writes 1 into the cell A13 of the active worksheet 77 Multidimensional arrays are VBA variables which can hold more than one item related to several index sets (up to 60) e.g. a two dimensional array is a matrix syntax: declaration: Dim Name(num1,num2,num3,...) usage: Name(x,y,z,...) 0 x num1 0 y num2 0 z num3... the change of the index set is analogue to the one dimensional case - Expl.: a 2 by 2 matrix declaration: Dim A(1 to 2,1 to 2) usage: A(1,1) = a A(1,2) = b A(2,1) = c A(2,2) = d 78

Resizable arrays are arrays whose size is not fixed syntax: declaration: Redim Name(x to y)... Redim Name(w to z) the first statement creates a one dimensional resizable array the second statement overwrites the first statement syntax: declaration: Redim Name(x to y)... Redim preserve Name(w to z) w x, z y now the values in the array Name(x to y) will be saved Upper and lower bound function Lbound(RA) gives the lower bound of the array called RA Ubound(RA) gives the upper bound of the array called RA 79 -Expl.: Redim RA(1 to 10) x = Lbound(RA) y = Ubound(RA) Redim RA(12 to 19) x = Lbound(RA) ( x = 1) ( y = 10) (now x = 12) (now y = 19) y = Ubound(RA) Data exchange: Arrays can be used as an efficient way to exchange data between the Excel spreadsheet and the VBA program VBA program Ø spreadsheet Range("A1:B2").Value = A (puts the values of the array A into cells A1:B2) spreadsheet Ø VBA program Dim B As Variant B = Range("A1:B2").Value (assigns the values of cells A1:B2 to the array B) 80

- Expl.: The content of two 2 by 2 matrices in the cells A1:B2 and D1:E2 are read to two arrays A and B. The matrices are multiplied and the result is returned to the cells G1:H2. Sub Matrix() Dim A, B As Variant arrays have to be variants Dim C(1 To 2, 1 To 2) A = Range("A1:B2").Value B = Range("D1:E2").Value For i = 1 To 2 the indexing starts at 1 For j = 1 To 2 C(i, j) = A(i, 1) * B(1, j) + A(i, 2) * B(2, j) Next j Next i Range("G1:H2").Value = C End Sub 81 MMULT is an Excel array function which returns the product of two arrays syntax: MMULT(array name1, array name2) - Expl.: MMULT( A1:B2, D1:E2 ) returns the same product as the previous VBA program - notice that MMULT is an array function, such that you have to prepare for an output bigger than one cell: (recall LINEST) select a range for the output, e.g. 2ä2 cells type the function, e.g. = MMULT(...) complete with Ctrl + Shift + Enter - notice also: MMULT is an Excel function not VBA function 82

The Split Function returns an array consisting of substrings from a string expression in which each substring is separated by a delimiter which can be specified expression syntax: Split(expression [, delimiter] [, limit]) ª a string expression delimiter ª the character which separates the substrings (the default value is space) limit ª the maximum number of substrings to be returned (the default value is 1, that is all substrings) - Expl.: Dim x as variant x = Split( Today is Tuesday ) x(1) = Today x(2) = is x(3) = Tuesday or: x = Split( a,b,c,d,e,f,g,,, 3) x(1) = a x(2) = b x(3) = c,d,e,f,g 83 The Join Function returns a string consisting of the values in a string array separated by a specified delimiter syntax: Join(sourcearray [, delimiter]) sourcearray ª an array containing strings delimiter ª the character which separates the substrings (the default value is space) - Expl.: Dim x(1 to 3) x(1) = Today x(2) = is x(3) = Tuesday y = Join(x) y = Today is Tuesday 84

- similarly: y = Today & is & Tuesday y = Today is Tuesday in addition: Dim x as integer x = 8 y = Today & is & Tuesday the & x & -th of March y = Today is Tuesday the 8-th of March here the individual components do not have to be of string type (8 is an integer) 85