调用 Sub 和 Function 过程
若要从其他过程中调用某个 Sub 过程,请键入该过程的名称并包含任何所需的参数值。To call a Sub procedure from another procedure, type the name of the procedure and includevalues for any required arguments. 不需要使用 Call 语句,但如果使用了该语句,则必须将任何参数包含在圆括号内。The Callstatement is not required, but if you use it, you must enclose any arguments inparentheses.
You can use a Subprocedure to organize other procedures so they are easier to understand anddebug.You can use a Subprocedure to organize other procedures so they are easier to understand anddebug. In the following example, the Sub procedure Main calls the Subprocedure MultiBeep, passing the value 56 for its argument.In the following example, the Sub procedure Main calls the Sub procedure MultiBeep, passing the value 56 for its argument.
当 MultiBeep 运行后,控件返回到 Main,且 Main 调用 Sub 过程 Message。AfterMultiBeep runs, control returns to Main, and Main calls the Sub procedure Message. Message 显示一个消息框,当用户单击“确定”**** 时,控件返回到 Main,且 Main 完成。Message displays a message box; when the userclicks OK, control returns to Main, and Main finishes.
VB复制
Sub Main()
MultiBeep 56
Message
End Sub
Sub MultiBeep(numbeeps)
For counter = 1 Tonumbeeps
Beep
Next counter
End Sub
Sub Message()
MsgBox "Timeto take a break!"
End Sub
使用多个参数调用Sub 过程Call Subprocedures with more than one argument
下面的示例演示了使用多个参数调用 Sub 过程的两种方法。The following example shows two ways to calla Sub procedure with more than one argument. 第二次调用它时,需要将参数包含在圆括号内,因为使用了Call 语句。The second time it is called,parentheses are required around the arguments because the Call statementis used.
VB复制
Sub Main()
HouseCalc 99800, 43100
Call HouseCalc(380950, 49500)
End Sub
Sub HouseCalc(price AsSingle, wage AsSingle)
If 2.5 * wage <= 0.8 * price Then
MsgBox "Youcannot afford this house."
Else
MsgBox "Thishouse is affordable."
End If
End Sub
在调用Function 过程时使用圆括号Useparentheses when calling function procedures
若要使用函数的返回值,请将该函数分配给变量并将参数包含在圆括号内,如以下示例所示。To use the return value of a function, assignthe function to a variable and enclose thearguments in parentheses, as shown in the following example.
VB复制
Answer3 = MsgBox("Areyou happy with your salary?", 4, "Question3")
如果对函数的返回值不感兴趣,则可以按照调用 Sub 过程的同样方式调用函数。If you are not interested in the return valueof a function, you can call a function the same way you call a Subprocedure. 省略圆括号,列出参数,且不要将函数分配给变量,如以下示例所示。Omit the parentheses, list thearguments, and do not assign the function to a variable, as shown in thefollowing example.
VB复制
MsgBox "TaskCompleted!", 0, "Task Box"
如果您在上述示例中包含圆括号,则该语句将导致语法错误。If you include parentheses in thepreceding example, the statement causes a syntax error.
传递命名参数Pass named arguments
Sub 或 Function 过程中的语句可使用命名参数将值传递给所调用的过程。A statement in a Sub or Functionprocedure can pass values to called procedures by using named arguments. 您可以按照任何顺序列出命名参数。You can list named arguments inany order. 命名参数包含参数的名称后跟一个冒号和一个等号 (:=) 以及分配给该参数的值。A named argument consists of the name of the argument followed bya colon and an equal sign (:=), and the value assigned to the argument.
以下示例使用命名参数调用 MsgBox 函数,而不返回任何值。The following example calls the MsgBoxfunction by using named arguments with no return value.
VB复制
MsgBox Title:="TaskBox", Prompt:="TaskCompleted!"
以下示例使用命名参数调用 MsgBox 函数。The following example calls the MsgBoxfunction by using named arguments. 将返回值分配给变量。The return value is assigned tothe variable.
VB复制
answer3 = MsgBox(Title:="Question 3", _
Prompt:="Areyou happy with your salary?", Buttons:=4)
另请参阅See also
· 在代码中使用圆括号
|