运行其他模块的VBA模块 [英] VBA module that runs other modules

查看:513
本文介绍了运行其他模块的VBA模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Microsoft VBA中进行编程。起初我需要在一个宏的帮助下生成一个QueryTable(我已经得到了代码),之后在宏的帮助下,我需要应用使用QueryTable中的数据的公式。我所面临的问题是QueryTable只出现在其代码所在的Sub完成工作之后。这意味着我不能在其中生成公式的代码,因为没有可以生成公式的数据。



现在的想法是写一个运行其他模块的模块:

  Sub moduleController()
运行Module1
运行Module2
End Sub

这给出错误:


运行时错误1004 - 无法运行宏macroname。此工作簿中的宏可能不可用,或者所有宏可能被禁用。


可能是什么解决方案?也许我的QueryTable加载问题有另一个解决方案?

解决方案

只要所讨论的宏在同一个工作簿中,你验证名称是否存在,您可以通过名称而不是模块从任何其他模块中调用这些宏。



所以如果在Module1中有两个宏Macro1和Macro2以及Module2您有Macro3和Macro 4,然后在另一个宏中,您可以将它们全部称为:

  Sub MasterMacro()
调用宏1
调用宏2
调用宏3
调用宏4
结束子


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.

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.

What could be the solution? Maybe there is another solution for my QueryTable loading problem?

解决方案

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:

Sub MasterMacro()
    Call Macro1
    Call Macro2
    Call Macro3
    Call Macro4
End Sub

这篇关于运行其他模块的VBA模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆