Microsoft Office Tutorials and References
In Depth Information
The Select Case construct
Another approach is to use the Else clause of the If-Then construct. For example:
Sub GreetMe()
If Time < 0.5 Then MsgBox “Good Morning” Else _
MsgBox “Good Afternoon”
End Sub
Notice that the preceding example uses the line continuation sequence (a space
followed by an underscore); If-Then-Else is actually a single statement.
The preceding examples all used a single statement for the Then clause of the
If-Then construct. However, you often need to execute multiple statements if a
condition is True. You can still use the If-Then construct, but you need to use an
End If statement to signal the end of the statements that comprise the Then clause.
Here’s an example that executes two statements if the If clause is True:
If x > 0 Then
y = 2
z = 3
End If
You can also use multiple statements for an If-Then-Else construct. Here’s an
example that executes two statements if the If clause is True, and two other
statements if the If clause is not True.
If x > 0 Then
y = 2
z = 3
Else
y = -2
z = -3
End If
The Select Case construct
The Select Case construct is useful for choosing among three or more options.
This construct also works with two options and is a good alternative to If-Then-
Else . The following example of a Select Case construct shows another way to
code the GreetMe examples presented in the preceding section:
Sub GreetMe()
Select Case Time
Case Is < 0.5
MsgBox “Good Morning”
Case 0.5 To 0.75
Search JabSto ::




Custom Search