如何将 VBA 函数调用到子过程中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1072075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call a VBA Function into a Sub Procedure
提问by Justin
I know this is a simple question for someone out there, but I have never really used function module at all because I did not understand what they were.
我知道这对外面的人来说是一个简单的问题,但我从来没有真正使用过功能模块,因为我不明白它们是什么。
So I have a whole bunch of things I can use this for (cut down on redundancy), but I want to know how I call into a sub (like a button click) procedure from a form.
所以我有很多东西可以用它来(减少冗余),但我想知道我如何从表单调用子(如按钮单击)过程。
I tried this...
我试过这个...
Sub Command_Click()
Call "pptCreator"
End Sub
I know that is pretty bad, but I have no idea how to bring this into a procedure.
我知道这很糟糕,但我不知道如何将其纳入程序。
回答by Robert Harvey
Here are some of the different ways you can call things in Microsoft Access:
以下是您可以在 Microsoft Access 中调用事物的一些不同方式:
To call a form sub or function from a module
从模块调用表单子或函数
The sub in the form you are calling MUST be public, as in:
您正在调用的表单中的 sub 必须是公开的,例如:
Public Sub DoSomething()
MsgBox "Foo"
End Sub
Call the sub like this:
像这样调用子:
Call Forms("form1").DoSomething
The form must be open before you make the call.
在您拨打电话之前,表格必须打开。
To call an event procedure, you should call a public procedure within the form, and call the event procedure within this public procedure.
要调用事件过程,您应该在表单中调用一个公共过程,并在这个公共过程中调用事件过程。
To call a subroutine in a module from a form
从表单调用模块中的子程序
Public Sub DoSomethingElse()
MsgBox "Bar"
End Sub
...just call it directly from your event procedure:
...只需直接从您的事件过程中调用它:
Call DoSomethingElse
To call a subroutine from a form without using an event procedure
在不使用事件过程的情况下从窗体调用子例程
If you want, you can actually bind the function to the form control's event without having to create an event procedure under the control. To do this, you first need a public function in the module instead of a sub, like this:
如果需要,您实际上可以将函数绑定到表单控件的事件,而无需在控件下创建事件过程。为此,您首先需要模块中的公共函数而不是子函数,如下所示:
Public Function DoSomethingElse()
MsgBox "Bar"
End Function
Then, if you have a button on the form, instead of putting [Event Procedure] in the OnClick event of the property window, put this:
然后,如果你在窗体上有一个按钮,而不是把 [Event Procedure] 放在属性窗口的 OnClick 事件中,而是把这个:
=DoSomethingElse()
When you click the button, it will call the public function in the module.
当你点击按钮时,它会调用模块中的公共函数。
To call a function instead of a procedure
调用函数而不是过程
If calling a sub looks like this:
如果调用子看起来像这样:
Call MySub(MyParameter)
Then calling a function looks like this:
然后调用一个函数看起来像这样:
Result=MyFunction(MyFarameter)
where Result is a variable of type returned by the function.
其中 Result 是函数返回的类型变量。
NOTE:You don't always need the Call keyword. Most of the time, you can just call the sub like this:
注意:您并不总是需要 Call 关键字。大多数时候,你可以像这样调用 sub:
MySub(MyParameter)
回答by shahkalpesh
if pptCreator is a function/procedure in the same file, you could call it as below
如果 pptCreator 是同一文件中的函数/过程,则可以如下调用它
call pptCreator()
call pptCreator()
回答by Rockey
Calling a Sub Procedure – 3 Way technique
调用子过程——3 种方法
Once you have a procedure, whether you created it or it is part of the Visual Basic language, you can use it. Using a procedure is also referred to as calling it.
一旦您有了一个过程,无论它是您创建的还是 Visual Basic 语言的一部分,您都可以使用它。使用过程也称为调用它。
Before calling a procedure, you should first locate the section of code in which you want to use it. To call a simple procedure, type its name. Here is an example:
在调用过程之前,您应该首先找到要在其中使用它的代码段。要调用一个简单的过程,请键入它的名称。下面是一个例子:
Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
msgbox strFullName
End Sub
Sub Exercise()
CreateCustomer
End Sub
Besides using the name of a procedure to call it, you can also precede it with the Call keyword. Here is an example:
除了使用过程的名称来调用它之外,您还可以在它前面加上 Call 关键字。下面是一个例子:
Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
Sub Exercise()
Call CreateCustomer
End Sub
When calling a procedure, without or without the Call keyword, you can optionally type an opening and a closing parentheses on the right side of its name. Here is an example:
在调用过程时,无论是否使用 Call 关键字,您都可以选择在其名称的右侧键入左括号和右括号。下面是一个例子:
Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
Sub Exercise()
CreateCustomer()
End Sub
Procedures and Access Levels
程序和访问级别
Like a variable access, the access to a procedure can be controlled by an access level. A procedure can be made private or public. To specify the access level of a procedure, precede it with the Private or the Public keyword. Here is an example:
与变量访问一样,对过程的访问可以由访问级别控制。程序可以是私有的,也可以是公开的。要指定过程的访问级别,请在它前面加上 Private 或 Public 关键字。下面是一个例子:
Private Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
The rules that were applied to global variables are the same:
Private: If a procedure is made private, it can be called by other procedures of the same module. Procedures of outside modules cannot access such a procedure.
Also, when a procedure is private, its name does not appear in the Macros dialog box
Public: A procedure created as public can be called by procedures of the same module and by procedures of other modules.
Also, if a procedure was created as public, when you access the Macros dialog box, its name appears and you can run it from there
应用于全局变量的规则是相同的:
私有:如果一个过程是私有的,它可以被同一模块的其他过程调用。外部模块的过程不能访问这样的过程。
此外,当过程为私有过程时,其名称不会出现在“宏”对话框中
公共:创建为公共的过程可以被同一模块的过程和其他模块的过程调用。
此外,如果一个过程被创建为公共的,当您访问宏对话框时,它的名称会出现,您可以从那里运行它
回答by Mark3308
Procedures in a Module start being useful and generic when you pass in arguments.
当您传入参数时,模块中的过程开始变得有用和通用。
For example:
例如:
Public Function DoSomethingElse(strMessage As String)
MsgBox strMessage
End Function
Can now display any message that is passed in with the string variable called strMessage.
现在可以显示使用名为 strMessage 的字符串变量传入的任何消息。
回答by Gary C
To Add a Function To a new Button on your Form: (and avoid using macro to call function)
向表单上的新按钮添加函数:(并避免使用宏调用函数)
After you created your Function (Function MyFunctionName()) and you are in form design view:
在创建函数(Function MyFunctionName())并进入表单设计视图后:
- Add a new button (I don't think you can reassign an old button - not sure though).
- When the button Wizard window opens up click Cancel.
- Go to the Button properties Event Tab - On Click - field.
- At that fields drop down menu select: Event Procedure.
- Now click on button beside drop down menu that has ... in it and you will be taken to a newPrivate Sub in the forms Visual Basic window.
- In that Private Sub type: Call MyFunctionName
It should look something like this:
- 添加一个新按钮(我认为您不能重新分配旧按钮 - 不过不确定)。
- 当按钮向导窗口打开时,单击取消。
- 转到按钮属性事件选项卡 - 单击时 - 字段。
- 在该字段下拉菜单中选择:事件过程。
- 现在单击包含 ... 的下拉菜单旁边的按钮,您将被带到窗体 Visual Basic 窗口中的一个新的Private Sub。
- 在那个 Private Sub 类型中: Call MyFunctionName
它应该是这样的:
Private Sub Command23_Click()
Private Sub Command23_Click()
Call MyFunctionName
调用 MyFunctionName
End Sub
结束子
- Then just save it.
- 然后只需保存它。

