Excel VBA import VBE module programmatically


Is it possible in Excel VBA to import a module from another macro-enabled spreadsheet
and run the code in it?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
You can create a template (xlst) workbook that contains the code you want to use as the basis for a new workbook. In other words add the code to a workbook, save it as an xlst file and then whenever you want to use that code in a new workbook, select that template as the starting point.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
You can also save the code as an addin then that code will be available for any workbook. It really depends why you want to do this. See:
Excel Addins

If you really want to import the code then read Chip Pearsson's article

Adding/Deleting Modules

Remember that if you want to import code then the VBA Project cannot be protected so you lose that security.
Rob HensonFinance AnalystCommented:
Alternatively, have the code in WB1 to just run the code in WB2. I suspect WB2 has to be open to do so might need two steps to the WB1 code; open WB2 and run code.

WB2 can be opened and hidden and then closed without saving.

Rob H
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.