简体   繁体   English

VBA-如何在不打开另一个工作簿的情况下运行它?

[英]VBA - How run a Macro from another workbook without opening it?

I wanted to save all my VBA Project in a Excel workbook (or other type of file if possible) and then embbed it to run in another workbook. 我想将我所有的VBA项目保存在Excel工作簿(或其他类型的文件,如果可能)中,然后将其嵌入以在另一个工作簿中运行。 I've seen topics about it but only found ways to run the macros opening the first sheet. 我看过有关它的主题,但只找到了运行第一张表的宏的方法。

I want to run a macro from a first workbook in a second one without opening the first workbook to do it. 我想在第二个工作簿中的第一个工作簿中运行宏,而无需打开第一个工作簿来执行此宏。 How can I do that? 我怎样才能做到这一点?

Save the file with the macros as a xlam file and Excel can load them each time Excel opens. 将带有宏的文件另存为xlam文件,每次打开Excel时Excel都可以加载它们。

You need to open File -> options -> add-ins. 您需要打开文件->选项->加载项。

At the bottom there is a button Go to (or something, I don't have English Excel on the current computer.) 底部有一个转到按钮(或某些名称,我在当前计算机上没有英文Excel。)
Then add the file to the list by clicking Browse and finding the file you just saved as xlam file. 然后,通过单击浏览并找到刚刚保存为xlam文件的文件,将文件添加到列表中。

Two scenarios comes to mind: 我想到两种情况:

1) You have a second macro for personal use and can save it locally (save in: %USERPROFILE%\\AppData\\Roaming\\Microsoft\\Excel\\XLSTART ). 1)您还有另一个供个人使用的宏,可以将其保存在本地(保存在: %USERPROFILE%\\ AppData \\ Roaming \\ Microsoft \\ Excel \\ XLSTART )。 This will allow you to create quick-buttons for your macros, etc. These types of macros open with Excel and will be separate VBA Projects inside of the default VBA editor. 这将允许您为宏等创建快捷按钮。这些类型的宏可通过Excel打开,并且将在默认VBA编辑器中作为单独的VBA项目。

2) You have a network or drive that multiple users need to access, so each user has a macro in their file (.xlsm or .xlsb), where that internal macro reads Application.Run "filepath\\workbookname.xlsb!macro", which also allows you to call a private subroutine (note that you could use Call, but Application.Run will ensure that even Private macros are able to be accessed). 2)您有多个用户需要访问的网络或驱动器,因此每个用户的文件中都有一个宏(.xlsm或.xlsb),该内部宏读取Application。运行“ filepath \\ workbookname.xlsb!macro”,这也允许您调用私有子例程(请注意,您可以使用Call,但是Application.Run将确保甚至可以访问Private宏)。 This shouldn't require the other workbook be open, though I have personally had one user whose computer always opens the other file, regardless. 尽管我本人已经有一个用户的计算机始终打开另一个文件,但这不应该要求打开另一个工作簿。


Edit: 编辑:

Third scenario (really 2b): 第三种情况(确实是2b):

3) You have files where you want to regularly access another file... you will follow a similar approach to point 2 where you make a macro to Application.Run, though you can save that macro in your XLSTART folder... this will allow you to have a source macro location where others may also want to access and utilize. 3)您有要定期访问另一个文件的文件...您将采用类似的方法指向要在Application.Run上创建宏的第2点,尽管您可以将该宏保存在XLSTART文件夹中...允许您在其他人也可能想要访问和利用的源宏位置。 The source document would allow you to maintain 1 file for many users. 文档将允许您为许多用户维护1个文件。

Your answers were great! 您的回答很棒! Great to know about XLSTART folder from @Cyril, but based on @Andreas answer I found my path. 很高兴从@Cyril了解XLSTART文件夹,但是基于@Andreas回答,我找到了自己的路径。

The "problems" with adding an Add-In as @Andreas said, are cause my VBA Project would be avaliable on the VB Editor to every workbook on that computer, and to run my macros I'd have to use Application.Run("workbook.xlam!Macro"). 如@Andreas所说,添加外接程序的“问题”是因为我的VBA项目将在VB编辑器上可用于该计算机上的每个工作簿,并且要运行我的宏,我必须使用Application.Run(“ workbook.xlam!Macro”)。

Then I found References, which I have the same features, including I can delete my .xlam file to remove my code, and don't have the problems I mentioned above. 然后,我发现引用具有相同的功能,包括可以删除.xlam文件以删除代码,并且没有上面提到的问题。

Adding my VBA .xlam file as an reference, it'll be avaliable only to that specific workbook and I can run my macro just like it was on the same workbook. 添加我的VBA .xlam文件作为参考,它仅适用于该特定工作簿,并且我可以像在同一工作簿上一样运行宏。

For general knowledge: 一般知识:

ADDING A REFERENCE: 添加参考:

1- Save your project as an Excel Add-In (.xlam file) 1-将项目另存为Excel加载项(.xlam文件)

2- Open your target workbook, go to the Visual Basic Editor 2-打开目标工作簿,转到Visual Basic编辑器

3- Go to Tools > References > Browse... find your .xlam file and make sure it's checked. 3-转到工具>引用>浏览...,找到您的.xlam文件并确保已选中它。

4- Done! 4-完成! Now you'll see the project on the editor and can run your macros just like it was on the same workbook. 现在,您将在编辑器上看到该项目,并且可以像在同一工作簿上一样运行宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM