Microsoft Office Tutorials and References
In Depth Information
Controlling Code Execution
Case 0.5 To 0.75
Msg = “Good Afternoon”
Case Else
Msg = “Good Evening”
End Select
MsgBox Msg
End Sub
And here’s a rewritten version of the Discount example using a Select Case construct. This
procedure assumes that Quantity is always an integer value. For simplicity, the procedure
performs no error checking.
Sub Discount3()
Dim Quantity As Variant
Dim Discount As Double
Quantity = InputBox(“Enter Quantity: “)
Select Case Quantity
Case “”
Exit Sub
Case 0 To 24
Discount = 0.1
Case 25 To 49
Discount = 0.15
Case 50 To 74
Discount = 0.2
Case Is >= 75
Discount = 0.25
End Select
MsgBox “Discount: “ & Discount
End Sub
The Case statement also can use a comma to separate multiple values for a single case. The
following procedure uses the VBA WeekDay function to determine whether the current day is a
weekend (that is, the Weekday function returns 1 or 7 ). The procedure then displays an
appropriate message.
Sub GreetUser1()
Select Case Weekday(Now)
Case 1, 7
MsgBox “This is the weekend”
Case Else
MsgBox “This is not the weekend”
End Select
End Sub
Search JabSto ::

Custom Search