vba Excel:如何从 VbScript 调用 Sheet 模块中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20351538/
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
Excel: How to call functions in Sheet modules from VbScript
提问by user2696565
Using VBScript, I'm trying to run a subroutine that resides in one of the code modules. Now this particular sub calls several other subs/functions, including few that reside on the sheet modules. And on calling these subs residing in Sheet Modules, I get can error:
使用 VBScript,我试图运行驻留在其中一个代码模块中的子例程。现在这个特定的 sub 调用了几个其他 subs/function,包括一些驻留在工作表模块上的 subs/function。在调用这些驻留在 Sheet Modules 中的 subs 时,我可能会出错:
"Sub or Function undefined!"
How can I get around this? There are a lot of such functions and I don't want to move all of them from Sheet modules to code modules. Is there a better work around for this?
我怎样才能解决这个问题?有很多这样的函数,我不想将它们全部从 Sheet 模块移动到代码模块。有没有更好的解决方法?
VBScript that calls a sub (residing in Code modules) that does not call any code on Sheet modules is working fine, so I'm sure it is the above bit that is causing problem.
调用不调用 Sheet 模块上的任何代码的子(驻留在代码模块中)的 VBScript 工作正常,所以我确定是上述位导致问题。
回答by Sam
If you are calling Sub Routines or functions from different modules then you need to make sure of two things
如果你从不同的模块调用子程序或函数,那么你需要确保两件事
- The Sub / Function should not be declared Private
- You call the Sub / Function by prefixing the module name from which the code lives in
- Sub / Function 不应声明为 Private
- 您可以通过为代码所在的模块名称添加前缀来调用 Sub/Function
So if you have a Sub called TestSub
and it lives in Sheet1's code module, then you should call it like Sheet1.TestSub
. Calling it by using just TestSub
will result in the error Sub or Function not defined
.
因此,如果您调用了一个 SubTestSub
并且它位于 Sheet1 的代码模块中,那么您应该像Sheet1.TestSub
. 使用 just 调用它TestSub
会导致错误Sub or Function not defined
。
As mentioned in the comments, the name of the sheet isn't always the same as the display name. You can find the name of the sheet from the object explorer in the VBE
正如评论中提到的,工作表的名称并不总是与显示名称相同。您可以在 VBE 中的对象资源管理器中找到工作表的名称
In this example, the real sheet name is on the left, whereas the display name is on the right. You should refer to the sheet using the name on the left.
在此示例中,实际工作表名称在左侧,而显示名称在右侧。您应该使用左侧的名称来参考工作表。