Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control...

21
http://blackboard.umbc.edu Lab 4 Range Review, Control Lab 4 Range Review, Control Logic and Loops Logic and Loops Range Review Range Review Control Logic and Loops Control Logic and Loops Exercise Exercise

Transcript of Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control...

Page 1: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Lab 4 Range Review, Control Logic and LoopsLab 4 Range Review, Control Logic and Loops

► ► Range ReviewRange Review

► ► Control Logic and LoopsControl Logic and Loops

► ► ExerciseExercise

Page 2: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Range Review (1)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 = 5Note: 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.

Page 3: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Range Review (2)Range Review (2)

● Some important methods of ranges Clear: deletes the values and the formatting of the range.

E.g., Range(“B2”).ClearClearContents: deletes only the values of the range. E.g.,

Range(“A1”).ClearContentsSelect: 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

Page 4: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Range Review (3)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

Page 5: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Range Review (4)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

Page 6: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Object BrowserObject Browser

• View Object Browser

• F2

(In VBE)

Page 7: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Control Logic – Chpt 7Control 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

Page 8: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

If Constructions (1)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

statements2ElseIf condition3 Then

statements3……Else

other statements]End IF

Example:

Page 9: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

If Constructions (2)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)

Page 10: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

If Constructions (3)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.30Else

UnitCost = 1.20End IF

ElseIf Product = “Gadgets” Then If NumberOrdered <= 500 Then

UnitCost = 2.70ElseIf NumberOrdered <= 600 Then

UnitCost = 2.60Else

UnitCost = 2.50End If

ElseUnitCost = 2.00

End IF

Page 11: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Case Constructions (1)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 Value1statements1

Case Value2statements2

… …[Case Else

other statements]End Select

Page 12: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Case Constructions (2)Case Constructions (2)Example:Dim UnitPrice as single, UnitCost as singleUnitCost = 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

Page 13: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

LoopsLoops

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

Page 14: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

For-Next LoopsFor-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, …, 49Sub AddOddInteger()

Dim Sum As Integer, i As IntegerSum = 0For i = 1 To 49 Step 2 Sum = Sum + iNext iMsgBox "The sum of odd numbers from 1-50 is " & Sum

End Sub• For loops can also be nested

Page 15: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

For Each Loops (1)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

Page 16: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

For Each Loops (2)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 BooleanFound = FalseFor Each ws in ActiveWorkbook.Worksheets If ws.Name = “Data” Then

Found = TrueExit For

End IfNext If Found = True Then MsgBox “There is a worksheet named Data.”Else MsgBox “There is no worksheet named Data.”End If

End Sub

Page 17: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Do Loops (1)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 Conditionstatements

Loop

2. Do While…Loop– Syntax:

Do While Conditionstatements

Loop

Page 18: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Do Loops (2)Do Loops (2)3. Do… Loop Until– Syntax:

Dostatements

Loop Until Condition4. Do…Loop While– Syntax:

Dostatements

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

Page 19: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Do Loops (3)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

Page 20: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

Do Loops (4)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

Page 21: Http://blackboard.umbc.edu Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.

http://blackboard.umbc.edu

ExerciseExercise

• 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!