python调用vba事件sub_调用 Sub 和 Function 过程

调用 Sub 和 Function 过程Calling Sub and Function procedures

08/14/2019

o365devx.png?size=32

本文内容

若要从其他过程中调用某个 参数值。To call a procedure, type the name of the procedure and include values for any required arguments. 不需要使用The

You can use a Sub procedure to organize other procedures so they are easier to understand and debug.You can use a Sub procedure to organize other procedures so they are easier to understand and debug. In the following example, the Sub procedure Main calls the Sub procedure 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。After MultiBeep runs, control returns to Main, and Main calls the Sub procedure Message. Message 显示一个消息框,当用户单击“确定”**** 时,控件返回到 Main,且 Main 完成。Message displays a message box; when the user clicks OK, control returns to Main, and Main finishes.

Sub Main()

MultiBeep 56

Message

End Sub

Sub MultiBeep(numbeeps)

For counter = 1 To numbeeps

Beep

Next counter

End Sub

Sub Message()

MsgBox "Time to take a break!"

End Sub

备注

有兴趣开发跨多个平台扩展 Office 体验的解决方案吗?Interested in developing solutions that extend the Office experience across multiple platforms? 与 VSTO 外接程序和解决方案相比,Office 外接程序的占地面积较小,您可以使用几乎任何 web 编程技术(如 HTML5、JavaScript、CSS3 和 XML)构建它们。Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.

使用多个参数调用 Sub 过程Call Sub procedures with more than one argument

下面的示例演示了使用多个参数调用 Sub 过程的两种方法。The following example shows two ways to call a Sub procedure with more than one argument. 第二次调用它时,需要将参数包含在圆括号内,因为使用了 Call 语句。The second time it is called, parentheses are required around the arguments because the Call statement is used.

Sub Main()

HouseCalc 99800, 43100

Call HouseCalc(380950, 49500)

End Sub

Sub HouseCalc(price As Single, wage As Single)

If 2.5 * wage <= 0.8 * price Then

MsgBox "You cannot afford this house."

Else

MsgBox "This house is affordable."

End If

End Sub

在调用 Function 过程时使用圆括号Use parentheses when calling function procedures

若要使用函数的返回值,请将该函数分配给变量并将参数包含在圆括号内,如以下示例所示。To use the return value of a function, assign the function to a variable and enclose the arguments in parentheses, as shown in the following example.

Answer3 = MsgBox("Are you happy with your salary?", 4, "Question 3")

如果对函数的返回值不感兴趣,则可以按照调用 Sub 过程的同样方式调用函数。If you are not interested in the return value of a function, you can call a function the same way you call a Sub procedure. 省略圆括号,列出参数,且不要将函数分配给变量,如以下示例所示。Omit the parentheses, list the arguments, and do not assign the function to a variable, as shown in the following example.

MsgBox "Task Completed!", 0, "Task Box"

如果您在上述示例中包含圆括号,则该语句将导致语法错误。If you include parentheses in the preceding example, the statement causes a syntax error.

传递命名参数Pass named arguments

Sub 或 命名参数将值传递给所调用的过程。A statement in a Sub or named arguments. 您可以按照任何顺序列出命名参数。You can list named arguments in any order. 命名参数包含参数的名称后跟一个冒号和一个等号 (:=) 以及分配给该参数的值。A named argument consists of the name of the argument followed by a colon and an equal sign (:=), and the value assigned to the argument.

以下示例使用命名参数调用 MsgBox 函数,而不返回任何值。The following example calls the MsgBox function by using named arguments with no return value.

MsgBox Title:="Task Box", Prompt:="Task Completed!"

以下示例使用命名参数调用 MsgBox 函数。The following example calls the MsgBox function by using named arguments. 将返回值分配给变量。The return value is assigned to the variable.

answer3 = MsgBox(Title:="Question 3", _

Prompt:="Are you happy with your salary?", Buttons:=4)

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值