180 likes | 283 Views
Lab 5 Arrays. ► Lab 4 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
E N D
Lab 5 Arrays ► Lab 4 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
Use ‘For … Next’ Loop Sub lab4exe5() Dim i As Integer, j As Integer, CutOff As Long, _ TotalMonths As Integer, ObjRange As Range CutOff = InputBox("Please input a CutOff value here:") Set ObjRange = Range("B3:D14") For j = 1 To 3 TotalMonths = 0 For i = 1 To 12 If ObjRange(i, j).Value > CutOff Then TotalMonths = _ TotalMonths + 1 Next i MsgBox "There are althgether " & TotalMonths & _ "months in branch " & j & " that has sales _ amount higher than the cut-off value of " _ & CutOff & ".", vbInformation, "Branch Result" Next j End Sub
Use ‘For Each … Next’ Loop Sub Lab4ExeX() Dim i As Integer, CutOff As Long, TotalMonths As Integer, ObjRange _ As Range, RCell As Range CutOff = InputBox("Please input a CutOff value here:") For i = 1 To 3 'scan branch 1 to 3 With Range("A2") 'set Range("A2") as current cell Set ObjRange = Range(.Offset(1, i), .Offset(1, i).End(xlDown)) End With TotalMonths = 0 For Each RCell In ObjRange ' Scan all 12 months for current branch If RCell.Value > CutOff Then TotalMonths = TotalMonths + 1 Next MsgBox "There are altogether " & TotalMonths & _ " months in Branch " & i & _ " that has sales amount higher than the cut-off value of " _ & CutOff & ".", vbInformation, "Branch Result" Next i End Sub
Immediate Window Practice • In VBE (Alt + F11) • Click “CTRL + G” • Or View Immediate Window
Immediate Window Practice • Type in the Immediate Window the following codes and see what happens: ?Application.Name ?Application.Version ?Format(Date, "long date") ?Format(86099.57654, "#,##0.00") ?Time InputBox "Your name?", "User name", " " MsgBox "Hello!", ,"Say Hello"
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 • DimArrayName(NumberOfElements) AsDataType • E.g., Dim State(4) 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 State(3) State(4) State(5) State(1) State(2) Nebraska Tennessee New Jersey New Mexico Texas Nebraska State(1) Array name: State This array contains 5 elements, which represent different states New Jersey State(2) New Mexico State(3) Tennessee State(4) State(5) Texas 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 • DimArrayName(NumberOfElements1, _ NumberOfElements2) AsDataType • E.g., create an array to store 12-month sales of 5 sales peopleDim 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
Exercise Download Lab6_Student from the blackboard, then: • Write codes to pop up an InputBox to ask the user for a customer name. • The program uses the user’s input to check whether the name is on the list. If it is, display an msgbox saying that the customer name is on the list, and the corresponding cell will be indicated in boldface and in blue. Otherwise, an 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.)
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