Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.

Slides:



Advertisements
Similar presentations
1 Overview Introduction to VBA The Visual Basic Editor (VBE) –First Program Getting Started with VBA –Subroutines –Declaring variables –Input boxes and.
Advertisements

Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
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.
Programming with Microsoft Visual Basic 2005, Third Edition
VBA Modules, Functions, Variables, and Constants
String Variables Visual Basic for Applications 4.
An Introduction to Programming with C++ Fifth Edition
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Arrays.
Chapter 7: Working with Arrays
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Adding Automated Functionality to Office Applications.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
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.
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
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.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Mouse Events. Mouse Driven Events Unlike control_click() which is passed no arguments (unless it's an index for an array), MouseDown, MouseUp and MouseMove.
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.
Array Processing.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 9 Macros And Visual Basic For Applications.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Lab 2 Introduction to VBA (II) ► Lab 1 revisited - Sub & Function procedures - Data types and variable declaration ► Recording.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
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.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
© Stefano Grazioli - Ask for permission for using/quoting: Source: Excel VBA Programming by John Walkenbach.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Unit 2 Technology Systems
Introduction to Computer Programming IT-104
Presentation transcript:

Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise

Case Construction All conditions can only be related to one identical independent variable Must give that variable following ‘Select Case’ key word Do not use any variables following other single ‘Case’ key word Does not support more than 1 equation following ‘Case’ key word Use ‘To’ and ‘Is’ key words to assist you

Array Concept – Chpt 9 An array is a group of data elements of the same data type that have a common name (array name). Arrays are used primarily to deal with a list of data. An Array can have as many as 60 dimensions in Visual Basic; the most commonly used arrays are one-dimensional and two-dimensional. A one-dimensional array is simply a row (or column) of data. A two-dimensional array resembles a table with rows and columns..

Why Arrays? Computer programs usually need loops for performing repeated tasks. Arrays are basically lists, with each element of the list working just like normal variables, except that you have an indexed list of them. Such indexing property of arrays makes them particularly amenable to looping. So, loops are often accompanied by arrays. Programmers use arrays to store related data in the y internal memory of the computer. Data stored inside the computer can be written and read much faster than data stored in a file on a disk.

One-dimensional Array Declaration One-dimensional array –Dim ArrayName(NumberOfElements) As DataType E.g., Dim State(5) As String Then, the array State has five elements (with each being a String type), which are: State(0), State(1), State(2), State(3), and State(4). –By default the index starts from 0 not 1 (0-based indexing). If you prefer 1-based indexing, you may do either of the following: Place a Option Base line at the top of your module(s), e.g.,Option Explicit Option Base 1 Dim State(5) As String … … Indicate explicitly how you want a particular array to be indexed, e.g., Dim State(1 To 5) As String (Then, its elements include State(1), State(2), State(3), State(4), and State(5))

One-Dimensional Array Nebraska New JerseyNew Mexico Tennessee Texas Nebraska New Jersey New Mexico Tennessee Texas Array name: State This array contains 5 elements, which represent different states To refer to an array element, you use the array name and an index number: State(3), State(5).

Two-Dimensional Array How can you store the sales data of 5 sales people in the past 12 months? You need a two-dimensional array –Use the first dimension to indicate the different sales person –Use the second dimension to record sales amount of each month Q: what are the sizes of the first dimension and the second dimension?

Two-dimensional Array Declaration Two-dimensional array –Dim ArrayName(NumberOfElements1, NumberOfElements2) As DataType E.g., create an array to store 12-month sales of 5 sales people Dim Sales(4,11) As Double Then, the array Sales now has 60 elements (with each being a Double type), which are: Sales(0,0), Sales(0,1), Sales(0,2), … Sales(0, 11) Sales(1,0), Sales(1,1), State(1,2), …. Sales(1,11) … Sales(4,0), Sales(4,1), State(4,2), …. Sales(1,11) To change the default 0-based indexing to 1-based, you can do the same thing as we specified for one-dimensional array: add the line: Option Base 1 or explicitly specify - Dim Sales(1 To 5, 1 To 12) As Double

Dynamic Array Declaration When? You need array(s) in your program. However, when you are writing the code, you have no way of knowing how many elements it/they will contain. Example Dim Employee( ) As String, NEmployees As Integer NEmployees = InputBox("How many employees are there in " _ & "your company?") ReDim Employee(NEmployees) Another Example Dim Employee() As String, NEmployees As Integer With Range("A2") NEmployees = Range(.Offset(1,0),.End(xlDown)).Rows.Count End with ReDim Employee(NEmployees)

Dynamic Array Declaration (2) You can use ReDim statement many times in a sub to readjust the size of the array. However, the problem is that when you use the ReDim to change array size, all of the previous contents of the array (if any) will be deleted. Preserve can be used to deal with this problem, e.g., Dim Employee() As String, NEmployees As Integer NEmployees = InputBox("How many employees are there in" _ & "your company?") ReDim Employee(NEmployees) … NEmployees = NEmployees + 1 ReDim Preserve Employee(NEmployees)

Array Example Calculate the Average. 1) Ask the user to input 10 numbers; 2) Store these numbers in an array named Num; 3) Calculate the average of these 10 numbers; and 4) Display the result to the user. Option Explicit Option Base 1 Sub Average() Dim i As Integer, Num() As Integer, N As Integer, Total As Integer N = 10 ReDim Num(N) Total = 0 For i = 1 To N Num(i) = InputBox("Please enter number " & i & ":") Total = Total + Num(i) Next i MsgBox "The average of these numbers is " & Total / N End Sub

Assignment Download “Lab6-Student Exercise – Arrays.xls” from the blackboard site, then: 1)Write codes to pop up an InputBox to ask the user for a customer name. 2)The program uses the user’s input to check whether the name is on the list. If it is, display a msgbox saying that the customer name is on the list, and the corresponding cell will be indicated in boldface and in blue. Otherwise, a msgbox will be displayed saying that the customer name is not on the list. (Hints: Dim the customer list as an array (string var. type) and Dim Found as Boolean. You will need the If-Then construction and For-Next or Do-While/Until loop too). Create a button to run and another to restore the formatting to its original style. (Make sure that you declare all your variables appropriately.)

Note There are two assignments for this week’s lab Please write the VBA code for the first assignment also in “Lab4- Student exercise-array.xls” under a separate module and create a different button on the first sheet to link to the subroutine.

To restore formatting Suppose the original style was no bold and color black. Sub reformat() With range(“a1:c21”).font.bold=false.colorindex = 1 End with End sub

How to Add a Button