vba 如何将 VBScript 宏导入 Excel 工作簿?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7570668/
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 to import VBScript macros into Excel workbook?
提问by Martin Dimitrov
I have several Excel workbooks. They all share the same macro modules. What I would like to achieve is when editing one module in one workbook not to have to edit the same module in the other workbooks.
我有几个 Excel 工作簿。它们都共享相同的宏模块。我想要实现的是在一个工作簿中编辑一个模块时不必编辑其他工作簿中的同一个模块。
Naturally, my first step was to export on save the modules in .bas files. But the problem is that I cannot import them on load.
当然,我的第一步是导出保存在 .bas 文件中的模块。但问题是我无法在加载时导入它们。
I had tried this:
我试过这个:
Private Sub Workbook_Open()
Set objwb = ThisWorkbook
Set oVBC = objwb.VBProject.VBComponents
Set CM = oVBC.Import("C:\Temp\TestModule.bas")
TestFunc
End Sub
There is a TestModule.bas in the same dir with content:
在同一个目录中有一个 TestModule.bas 与内容:
Function TestFunc()
MsgBox "TestFunc called"
End Function
When the workbook is opened, a compile error appears: Sub or Function not defined
. If I manually import the module everything works just fine.
打开工作簿时,会出现编译错误:Sub or Function not defined
。如果我手动导入模块一切正常。
Thanks for any advice.
感谢您的任何建议。
采纳答案by Tim Williams
Like you, I couldn't get the import to work from the workbook_open. You could put your import code in a sub a separate module, and call it from your workbook_open like this:
像您一样,我无法从 workbook_open 导入工作。您可以将导入代码放在一个单独的模块中,然后从您的 workbook_open 中调用它,如下所示:
Private Sub Workbook_Open()
Application.OnTime Now, "ImportCode"
End Sub
That seemed to work for me (a direct call did not...)
这似乎对我有用(直接打电话没有......)