Download presentation
Presentation is loading. Please wait.
Published bySheena Lyons Modified over 9 years ago
1
http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise
2
http://blackboard.umbc.edu Range Review (1) ● Some important properties of ranges Address: returns the address of a range as a string. E.g., “B2:D6” Cells: refers to a particular cell of a Range object. E.g., Range(“A1:A10”).Cells(3) refers to cell A3 Range(“A1:C10”).Cells(3,2) refers to cell B3 Offset: returns a reference relative to a range. E.g., Range(“A5”).Offset(2,3) refers to cell D7 Range(“D5”).Offset(-2,-1) refers to cell C3 Value: returns the value of a single-cell range. E.g., Range(“A1”).Value = 5 Note: Value is the default property of the range object, so it can be omitted. That is, Range(“A1”)=5 has the same meaning as above.
3
http://blackboard.umbc.edu Range Review (2) ● Some important methods of ranges Clear: deletes the values and the formatting of the range. E.g., Range(“B2”).Clear ClearContents: deletes only the values of the range. E.g., Range(“A1”).ClearContents Select: selects the range. E.g., Range(“A1:B10”).Select is equivalent to highlighting the range “A1:B10” in Excel. Sort: sorts a range. Copy:… ● Object Browser in VBE – a helpful tool to learn about the properties and methods of objects
4
http://blackboard.umbc.edu Range Review (3) ● There are several ways to specify ranges with VBA: Use an address E.g., Range(“A1:B10”) Use a range name E.g., Range(“Sales”) Use a variable for a range name E.g., SalesName = Range(“Sales”).Name Then, this range can be referred to as Range(SalesName) Use a Range object variable E.g., Dim R As Range Set R = Range(“Sales”) R.Font.Size =12
5
http://blackboard.umbc.edu Range Review (4) ● There are several ways to specify ranges with VBA (Continued): Use the Cells property E.g., Range(“C5:E15”).Cells(4,2), which refers to cell D8 Use the Offset property E.g., Range(“C5”).Offset(4,0), which refers to cell C9 Use top left and bottom right arguments E.g., Range(Range(“C1”), Range(“D10”)) Use the End property E.g., With Range(“A1”) Range(.Cells(1,1),.End(xlDown). _ End(xlToRight)).Select End With
6
http://blackboard.umbc.edu Object Browser View Object Browser F2 (In VBE)
7
http://blackboard.umbc.edu Control Logic – Chpt 7 Logical constructions control the sequence of statements in a procedure, and thus enable a decision making capability. The following two constructions are used most frequently in VBA: If Constructions Case Constructions
8
http://blackboard.umbc.edu If Constructions (1) Basic syntax (In one single line): If condition Then statement A [Else statement B] Example: If grade > 90 Then MsgBox ″Good Performance! ″ Else _ MsgBox ″ Need to work harder. ″ More complicated syntax: If condition1 Then statements1 [ElseIf condition2 Then statements2 ElseIf condition3 Then statements3 …… Else other statements] End IF Example:
9
http://blackboard.umbc.edu If Constructions (2) Sub GreetMe() If Time < 0.5 Then MsgBox "Good morning!" & " It’s now " & Time,, “The Time” ElseIf Time >= 0.5 And Time < 0.75 Then MsgBox "Good afternoon!" & " It’s now " & Time,, “The Time” Else MsgBox "Good evening!" & " It’s now " & Time,, “The Time” End If End Sub (In VBA, Time function returns a value that represents current time of the day. The time of day is expressed as a fractional value, i.e.12 pm: Time = 0.5, 6pm: Time = 0.75)
10
http://blackboard.umbc.edu If Constructions (3) Nested If statements (under such circumstances, indentation is very important for ease of reading): Example: If Product = “Widgets” Then If NumberOrdered <= 200 Then UnitCost = 1.30 Else UnitCost = 1.20 End IF ElseIf Product = “Gadgets” Then If NumberOrdered <= 500 Then UnitCost = 2.70 ElseIf NumberOrdered <= 600 Then UnitCost = 2.60 Else UnitCost = 2.50 End If Else UnitCost = 2.00 End IF
11
http://blackboard.umbc.edu Case Constructions (1) If constructions can become fairly complex when there are multiple conditions with each condition having its own codes. Case construction is a good alternative for choosing among three or more options General Syntax: Select Case VariableName Case Value1 statements1 Case Value2 statements2 … [Case Else other statements] End Select
12
http://blackboard.umbc.edu Case Constructions (2) Example: Dim UnitPrice as single, UnitCost as single UnitCost = InputBox(“Please enter the unit cost:”,”Unit Cost” Select Case ProductIndex Case Is <= 3 [Is keyword for comparison operators] UnitPrice = 1.2 * UnitCost Case 4 To 6 [To keyword specifies a range of values] UnitPrice = 1.3 * UnitCost Case 7 UnitPrice = 1.4 * UnitCost Case Else UnitPrice = 1.1 * UnitCost End Select MsgBox “The unit cost was “ & format(UnitCost,”$##.#0) & “the unit price was “ & format(UnitPrice,”$##.#0) & “.”,,”Unit Price” ‘ Is and To are keywords
13
http://blackboard.umbc.edu Loops Perhaps the single most useful feature of computer programs is their ability to loop – to repeat the same type of task any number of times. In VBA, the following two types of loops are used most often: For loops – you determine when the loop is done Do loops – program determines when the loop is done
14
http://blackboard.umbc.edu For-Next Loops Syntax: For counter = startvalue To endvalue [Step stepvalue] statements Next [counter] counter is the name of a numeric variable and it keeps track of how many times the statements are repeated – common names: i, j, k… startvalue, endvalue, and stepvalue must be numeric and they can be either positive or negative, integer or non-integer (default stepvalue is 1) Example: add 1, 3, 5, …, 49 Sub AddOddInteger() Dim Sum As Integer, i As Integer Sum = 0 For i = 1 To 49 Step 2 Sum = Sum + i Next i MsgBox "The sum of odd numbers from 1-50 is " & Sum End Sub For loops can also be nested
15
http://blackboard.umbc.edu For Each Loops (1) For Each loops are used to loop through all objects in a collection Syntax: Dim itm As Object For Each itm in Collection statements Next Object and Collection will vary depending on the type of collection
16
http://blackboard.umbc.edu For Each Loops (2) Example: search through all worksheets of the active workbook for a sheet named Data. If you find one, you can exit the loop immediately. Sub FindWorksheet() Dim ws As Worksheet, Found As Boolean Found = False For Each ws in ActiveWorkbook.Worksheets If ws.Name = “Data” Then Found = True Exit For End If Next If Found = True Then MsgBox “There is a worksheet named Data.” Else MsgBox “There is no worksheet named Data.” End If End Sub
17
http://blackboard.umbc.edu Do Loops (1) The Do loops can be used when you need to loop while some condition holds or until some condition holds. There are following four variations of Do loops: 1. Do Until…Loop –Syntax: Do Until Condition statements Loop 2. Do While…Loop –Syntax: Do While Condition statements Loop
18
http://blackboard.umbc.edu Do Loops (2) 3. Do… Loop Until –Syntax: Do statements Loop Until Condition 4. Do…Loop While –Syntax: Do statements Loop While Condition Major difference: the statements in the loop in the first two variations might never be executed, but they will certainly be executed at least once in the last two variations Exit Do statement can be used to exit a Do Loop prematurely Be careful of infinite loops
19
http://blackboard.umbc.edu Do Loops (3) Example: Do While… Loop Count = 1 Do While Count <= 3 MsgBox Count Count = Count + 1 Loop Count = 1 Count <= 3 Display Count Count = Count + 1 F T Loop 3 times
20
http://blackboard.umbc.edu Do Loops (4) Example: Do …Loop Until Count = 1 Do MsgBox Count Count = Count + 1 Loop Until Count >3 Loop 3 times Count = 1 Count > 3 Display Count Count = Count + 1 F T
21
http://blackboard.umbc.edu Exercise Rewrite the previous GreetMe example by using the Case Construction instead of the IF-Then Construction. Hint: Time is not inputted or declared. It is a VBA keyword. Create a sub and use the case construction without declaring any variables!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.