Listing A Procedure That Uses the Or Operator to Perform Multiple Logical Tests

Function FutureValue2(Rate As Single, Nper As Integer, Pmt As Currency, ^Frequency As String) As Currency

If Frequency = "Monthly" Or Frequency = "Quarterly" Then If Frequency = "Monthly" Then


FutureValue2 = FV(Rate / 4, Nper * 4, Pmt / 4) End If


MsgBox "The Frequency argument must be either " & _ Monthly"" or ""Quarterly""!"

End If End Function

Note that this procedure isn't particularly efficient because you end up testing the Frequency argument in two places. However, that just means that this example isn't the best use of the And and Or operators. The overall principle of using these operators to perform multiple logical tests is a useful one, however, and you should keep it in mind when constructing your decision-making code.

In Listing 6.3, if Frequency equals either Monthly or Quarterly,the result of the first if...Then...Else is true and the procedure then executes a second if...Then...Else structure.This is called nesting one control structure within another.This is very common in VBA procedures, but it can also get very confusing very quickly.To help you keep things straight, not only indent the statements within the first if...Then...Else,but double-indent the statements within the second if...Then...Else (refer to Listing 6.3 for an example).

0 0

Post a comment