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"> 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">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.

Similar presentations


Presentation on theme: "Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise."— Presentation transcript:

1 http://blackboard.umbc.edu Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise

2 http://blackboard.umbc.edu 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

3 http://blackboard.umbc.edu 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

4 http://blackboard.umbc.edu 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

5 http://blackboard.umbc.edu Immediate Window Practice In VBE (Alt + F11) Click “CTRL + G” Or View  Immediate Window

6 http://blackboard.umbc.edu 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"

7 http://blackboard.umbc.edu 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..

8 http://blackboard.umbc.edu 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.

9 http://blackboard.umbc.edu One-dimensional Array Declaration One-dimensional array –Dim ArrayName(NumberOfElements) As DataType 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))

10 http://blackboard.umbc.edu 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).

11 http://blackboard.umbc.edu 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?

12 http://blackboard.umbc.edu 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

13 http://blackboard.umbc.edu 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)

14 http://blackboard.umbc.edu 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)

15 http://blackboard.umbc.edu 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

16 http://blackboard.umbc.edu Exercise Download Lab6_Student from the blackboard, 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 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.)

17 http://blackboard.umbc.edu 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

18 http://blackboard.umbc.edu How to Add a Button


Download ppt "Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise."

Similar presentations


Ads by Google