运行其他模块的 VBA 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9908872/
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
VBA module that runs other modules
提问by ositra
I'm programming in Microsoft VBA. At first I need to generate a QueryTable with the help of a macro (I've got the code for that) and after that with the help of macros I need to apply formulas that use the data in the QueryTable. The problem that I am facing is that the QueryTable appears only after the Sub, in which its code is, has finished working. That means that I cannot include the code that generates formulas in it, because there is no data for the formulas to be generated on.
我在 Microsoft VBA 中编程。起初我需要在宏的帮助下生成一个 QueryTable(我已经有了它的代码),然后在宏的帮助下我需要应用使用 QueryTable 中的数据的公式。我面临的问题是 QueryTable 仅在其代码所在的 Sub 完成工作后才会出现。这意味着我不能在其中包含生成公式的代码,因为没有用于生成公式的数据。
The idea right now is to write a module that runs other modules:
现在的想法是编写一个运行其他模块的模块:
Sub moduleController()
Run "Module1"
Run "Module2"
End Sub
This gives the error:
这给出了错误:
Run time error 1004 - cannot run the macro "macroname". The macro may not be available in this workbook or all macros may be disabled.
运行时错误 1004 - 无法运行宏“macroname”。宏在此工作簿中可能不可用,或者可能禁用了所有宏。
What could be the solution? Maybe there is another solution for my QueryTable loading problem?
解决办法是什么?也许我的 QueryTable 加载问题有另一种解决方案?
回答by Jerry Beaucaire
As long as the macros in question are in the same workbook and you verify the names exist, you can call those macros from any other module by name, not by module.
只要有问题的宏在同一个工作簿中并且您验证名称存在,您就可以通过名称而不是模块从任何其他模块调用这些宏。
So if in Module1 you had two macros Macro1 and Macro2 and in Module2 you had Macro3 and Macro 4, then in another macro you could call them all:
因此,如果在 Module1 中您有两个宏 Macro1 和 Macro2,而在 Module2 中您有 Macro3 和 Macro 4,那么在另一个宏中您可以将它们全部调用:
Sub MasterMacro()
Call Macro1
Call Macro2
Call Macro3
Call Macro4
End Sub
回答by shahkalpesh
Is "Module1" part of the same workbook that contains "moduleController"?
If not, you could call public method of "Module1" using Application.Run someWorkbook.xlsm!methodOfModule
.
“Module1”是包含“moduleController”的同一工作簿的一部分吗?
如果没有,您可以使用Application.Run someWorkbook.xlsm!methodOfModule
.
回答by KeithG
I just learned something new thanks to Artiso. I gave each module a name in the properties box. These names were also what I declared in the module. When I tried to call my second module, I kept getting an error: Compile error: Expected variable or procedure, not module
感谢 Artiso,我刚刚学到了一些新东西。我在属性框中为每个模块命名。这些名称也是我在模块中声明的名称。当我尝试调用我的第二个模块时,我不断收到错误:编译错误:预期的变量或过程,而不是模块
After reading Artiso's comment above about not having the same names, I renamed my second module, called it from the first, and problem solved. Interesting stuff! Thanks for the info Artiso!
在阅读了上面关于没有相同名称的 Artiso 的评论后,我重命名了我的第二个模块,从第一个模块开始调用它,问题解决了。有趣的东西!感谢阿蒂索的信息!
In case my experience is unclear:
如果我的经验不清楚:
Module Name: AllFSGroupsCY Public Sub AllFSGroupsCY()
模块名称:AllFSGroupsCY 公共子 AllFSGroupsCY()
Module Name: AllFSGroupsPY Public Sub AllFSGroupsPY()
模块名称:AllFSGroupsPY 公共子 AllFSGroupsPY()
From AllFSGroupsCY()
从 AllFSGroupsCY()
Public Sub FSGroupsCY()
AllFSGroupsPY 'will error each time until the properties name is changed
End Sub