简体   繁体   中英

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. 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.

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.)
Then add the file to the list by clicking Browse and finding the file you just saved as xlam file.

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 ). 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.

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). 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):

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. The source document would allow you to maintain 1 file for many users.

Your answers were great! Great to know about XLSTART folder from @Cyril, but based on @Andreas answer I found my path.

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").

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.

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.

For general knowledge:

ADDING A REFERENCE:

1- Save your project as an Excel Add-In (.xlam file)

2- Open your target workbook, go to the Visual Basic Editor

3- Go to Tools > References > Browse... find your .xlam file and make sure it's checked.

4- Done! Now you'll see the project on the editor and can run your macros just like it was on the same workbook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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