Microsoft Office Tutorials and References
In Depth Information
End Function
Notice that the second If-Then statement uses >= (greater than or equal to). This covers the extremely remote
chance that the time is precisely 12:00 noon when the function is executed.
Another approach is to use the Else clause of the If-Then construct:
Function GREETME()
If Time < 0.5 Then GREETME = “Good Morning” Else _
GREETME = “Good Afternoon”
End Function
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 following is another example that uses the If-Then construct. This Function procedure calculates a discount
based on a quantity (assumed to be an integer value). It accepts one argument (quantity) and returns the appro-
priate discount based on that value.
Function DISCOUNT(quantity)
If quantity <= 5 Then DISCOUNT = 0
If quantity >= 6 Then DISCOUNT = 0.1
If quantity >= 25 Then DISCOUNT = 0.15
If quantity >= 50 Then DISCOUNT = 0.2
If quantity >= 75 Then DISCOUNT = 0.25
End Function
Notice that each If-Then statement in this procedure is always executed, and the value for DISCOUNT can
change as the function executes. The final value, however, is the desired value.
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 make up 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 state-
ments 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
Search JabSto ::

Custom Search