例 1 (Execute ステートメント)
' The Execute statement performs a calculation entered by the
' user and displays the result. If the user enters an invalid
' calculation, a compilation error occurs, and the DoCalc sub
' displays an appropriate message. The Option Declare statement
' disallows the implicit declaration of variables in the
' calculation. The user can enter 700 * 32, for example, or
' "My name is " & "Fred", or Today - 365, but an entry such as
' x / y generates an error.
Sub DoCalc
' To handle any compilation error in the Execute statement
On Error GoTo BadCalc
Execute |Option Declare
Dim x ' x is a Variant to accept any calculation.
x = | & InputBox ("Enter your calculation") & |
MessageBox "The result is " & x|
Exit Sub
' Report an error and exit.
BadCalc:
MessageBox "Not a valid calculation"
Exit Sub
End Sub
DoCalc ' Call the sub.
例 2 (Execute 関数)
' You can use the Execute function to return an integer such
' as a status code. In this example, the Execute function
' performs the calculation entered by the user. If the result
' is less than 0 or greater than 1 (100%), Execute returns a
' status code, and the ComputeInterest sub displays an
' appropriate message.
Sub ComputeInterest
Dim script As String, calc As String, retcode As Integer
calc$ = InputBox("Compute loan interest (charge/loan)")
script$ = _
|Option Declare
Sub Initialize
Dim pct As Single
pct! = | & calc$ & |
If pct! < 0 Then
End -2 ' -2 is a status code.
ElseIf pct! > 1 Then
End -3 ' -3 is a status code.
End If
MessageBox("Interest is " & Format(pct!,"percent"))
End Sub|
retcode% = Execute (script$)
If retcode% = -2 Then
MessageBox("You computed a negative interest rate!")
ElseIf retcode% = -3 Then
MessageBox("You computed an excessive interest rate!")
End If
End Sub
ComputeInterest ' Call the sub.