vba 从加载项运行宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2814116/
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
running a macro from an add-in
提问by l--''''''---------''''''''''''
i have an add-in called book1. inside the addin there is a module called module1 which has a sub called addin1
我有一个名为 book1 的加载项。在插件内部有一个名为 module1 的模块,它有一个名为 addin1 的子
i would like to run the macro addin1 from a different workbook
我想从不同的工作簿运行宏 addin1
i am trying to call this macro like this:
我试图像这样调用这个宏:
Call Addin1
but that's not working
但这不起作用
and i tried:
我试过:
Call book1.xlam.Module1.AddIn1
which is not working either
这也不起作用
does anyone know how to run a macro that is within an add-in ?
有谁知道如何运行加载项中的宏?
回答by Jacob G
(Assuming Office 2007) Here's an example:
(假设 Office 2007)这是一个示例:
1- Open a new Workbook.
2- Add a macro that contains the code MsgBox("Add-In")
3- Save as xlam file.
4- Open new Workbook.
5- Click Office Button -> Excel Options
6- Click the Add-Ins tab on the left.
7- At the bottom, next to the "Manage" dropdown, select "Excel Add-Ins" and click Go.
8- Click Browse and navigate to your xlam file.
9- Ensure the box next to your file is checked and click Ok.
10- Click the Office Button -> Excel Options.
11- Click Customize Tab.
12- Select Macros in the "Choose Commands From Dropdown."
13- Double click your AddIn and now a button will appear on the Quick Access Toolbar.
14- Click the button and your message box will now show.
1- 打开一个新的工作簿。
2- 添加包含代码的宏MsgBox("Add-In")
3- 另存为 xlam 文件。
4- 打开新的工作簿。
5- 单击 Office 按钮 -> Excel 选项
6- 单击左侧的加载项选项卡。
7- 在底部的“管理”下拉菜单旁边,选择“Excel 加载项”并单击“前往”。
8- 单击浏览并导航到您的 xlam 文件。
9- 确保选中文件旁边的框,然后单击确定。
10- 单击 Office 按钮 -> Excel 选项。
11- 单击自定义选项卡。
12- 在“从下拉列表中选择命令”中选择宏。
13- 双击您的插件,现在快速访问工具栏上会出现一个按钮。
14- 单击该按钮,现在将显示您的消息框。
回答by nathanvda
In your workbook you write:
在你的工作簿中你写:
Sub test()
' from other excel file
Application.Run ("youraddin.xla!ShowForm")
End Sub
And in the addin you have
在插件中你有
Public Sub ShowForm()
loginform.Show
End Sub
回答by RAK_da_Pira
In relation to how to run subroutines in installed add-ins (.xlam):
关于如何在已安装的加载项 (.xlam) 中运行子例程:
Make sure the VBA project associated with the add-in has a unique name (not VBAProject) - say Addin_1.
Within the VBA project for the workbook from which you wish to call the subroutines in the add-in, set a reference to Addin_1. Tools > References, find Addin_1 in the list of available references, and click on the adjacent box (a tick appears).
Within a subroutine in a code module of your workbook, you can now call a subroutine of the add-in, using:
Call Addin_1.routine_name(routine parameters)
确保与加载项关联的 VBA 项目具有唯一名称(不是 VBAProject) - 比如说 Addin_1。
在您希望从中调用加载项中的子例程的工作簿的 VBA 项目中,设置对 Addin_1 的引用。工具 > 引用,在可用引用列表中找到 Addin_1,然后单击相邻的框(出现一个勾号)。
在工作簿代码模块的子例程中,您现在可以调用加载项的子例程,使用:
Call Addin_1.routine_name(routine parameters)
Since you have a reference to Addin_1, the names of the subroutines in the add-in will appear once you type Call Addin_1.and the parameter list for the specific subroutine will appear once you have typed Call Addin_1.routine_name(, which greatly assists in writing error-free code.
由于您有对 Addin_1 的引用,因此一旦您键入Call Addin_1.,就会出现加载项中子例程的名称,并且一旦键入Call Addin_1.routine_name(,就会出现特定子例程的参数列表,这对编写无错误代码有很大帮助。
Tested with Excel 2013. Discovered when I was curious to see what happened when I set a reference to the VBA project of an installed add-in (.xlam). I have not seen this documented.
使用 Excel 2013 进行测试。当我很好奇设置对已安装加载项 (.xlam) 的 VBA 项目的引用时会发生什么时发现。我还没有看到这个记录。
回答by Ommit
I believe this is what your looking for :) You'll need all the single quotes and ! in the right places, can be a little tricky.
我相信这就是您要找的 :) 您将需要所有单引号和 ! 在正确的地方,可能有点棘手。
run "'book1'!module1"
回答by Aleksey
In addition to Jacob G's answer - if your .xlam book is protected by password, you should open it for edit (i.e. enter password). Otherwise macros of the .xlam book will not be visible in "Choose Commands From Dropdown"
除了 Jacob G 的回答 - 如果您的 .xlam 书籍受密码保护,您应该打开它进行编辑(即输入密码)。否则 .xlam 书的宏在“从下拉列表中选择命令”中将不可见

