vba 你能用excel引用一个外部宏吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10634345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:10:36  来源:igfitidea点击:

Can you refer to an external macro with excel?

excelexcel-vbavba

提问by Jacxel

I have a reasonably complex macro that I need to run on multiple different excel sheets, this macro is updated periodically and whenever a change is made its necessary to change it in each individual excel sheet. is there a way to get each excel document to refer to the one macro?

我有一个相当复杂的宏,我需要在多个不同的 excel 工作表上运行,这个宏会定期更新,每当有必要进行更改时,就需要在每个单独的 excel 工作表中进行更改。有没有办法让每个excel文档引用一个宏?

for example if i had a hierarchy like this:

例如,如果我有这样的层次结构:

    DOCUMENTS:
       -xlsheet1.xls
       -xlsheet3.xls
       -xlsheet2.xls
       MACROS:
            -macro1.bas

where there was a button in each sheet that ran macro1 when clicked.

每个工作表中都有一个按钮,单击时会运行 macro1。

回答by Siddharth Rout

I would recommend either moving that macro to your personal file or create an Add-In

我建议要么将该宏移动到您的个人文件中,要么创建一个加载项



Working with Personal File

处理个人文件

Topic: Deploy your Excel macros from a central file

主题:从中央文件部署 Excel 宏

Link: http://office.microsoft.com/en-us/excel-help/deploy-your-excel-macros-from-a-central-file-HA001087296.aspx

链接http: //office.microsoft.com/en-us/excel-help/deploy-your-excel-macros-from-a-central-file-HA001087296.aspx

Quote from the above link:

引用上面的链接

Have you ever wanted to use that really handy macro in all of your Excel worksheets? You can. This column explains how to place your macros in a file called personal.xls and make them available each time you start Excel.

您是否曾经想在所有 Excel 工作表中使用这个非常方便的宏?你可以。此列说明如何将宏放置在名为personal.xls 的文件中,并在每次启动Excel 时使它们可用。

Though it says that it applies to Microsoft Excel 2002 but it applies to all excel versions.

虽然它说它适用于 Microsoft Excel 2002,但它适用于所有 excel 版本。



Creating an Add-In

创建插件

Topic: Creating Excel Add-ins

主题创建 Excel 加载项

Link: http://www.ozgrid.com/VBA/excel-add-in-create.htm

链接http: //www.ozgrid.com/VBA/excel-add-in-create.htm

Quote from the above link:

引用上面的链接

I am often asked by users 'what is the best way to distribute their macros?' My answer, is without doubt via an Excel Add-in. After all, this is what Add-ins are for. For those that are not sure what an Excel add-in is, it's is nothing more than an Excel Workbook that has been saved as an Add-in, File>Save as \ Microsoft Excel Add-in (*.xla). Once saved and re-opened the Workbook will be hidden and can only be seen in the "Project Explorer" via the Visual Basic Editor. It is NOT hidden in the same way as the Personal.xls as this can be seen (and made visible) via Windows>Unhide.

用户经常问我“分发宏的最佳方式是什么?” 我的答案毫无疑问是通过 Excel 插件。毕竟,这就是插件的用途。对于那些不确定什么是 Excel 加载项的人来说,它只不过是一个已另存为加载项的 Excel 工作簿,文件>另存为 \Microsoft Excel 加载项 (*.xla)。保存并重新打开后,工作簿将被隐藏,只能通过 Visual Basic 编辑器在“项目资源管理器”中看到。它的隐藏方式与 Personal.xls 不同,因为可以通过 Windows>Unhide 看到(并使其可见)。

回答by Jacxel

The Personal file is good for having a macro across any number of workbooks on a single computer. In a networked environment with multiple users, you could simulate the Personal file by having a single workbook with your macros in it and coding all other workbooks to open and hide this workbook when they start up.

Personal 文件适用于在一台计算机上的任意数量的工作簿中使用宏。在具有多个用户的网络环境中,您可以通过在其中包含宏的单个工作簿并对所有其他工作簿进行编码以在它们启动时打开和隐藏此工作簿来模拟个人文件。