Automating the Importing of Macros

Written by Allen Wyatt (last updated July 29, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Gary regularly receives an XLS file that is generated by someone outside his company. He then needs to import some macros into the workbook (or, bother!, copy them from a TXT file) and then save the workbook out in the XLSM format. Gary wonders if there is a way to automate this, such that he can run a macro that will import the macros he would normally add manually and then save the workbook in the proper format.

There is a way to automate it by using the VBProject object. If your external text file contains, essentially, Visual Basic modules, you can use the Import method to actually import those modules. It behaves exactly the same as if you were to use the Import capabilities of the Visual Basic Editor. Here's an example of some code you could use:

Sub AutomateImport()
    Const ModulePath As String = "C:\temp\code.txt"

    Dim thisTarget As Workbook
    Dim thisName As String

    Set thisTarget = ActiveWorkbook
    thisName = thisTarget.Name

    ' Save as XLSM file (neceassry before importing module)
    ActiveWorkbook.SaveAs thisName & ".xlsm", _
      FileFormat:=xlOpenXMLWorkbookMacroEnabled

    ' Import the VBA code required
    thisTarget.VBProject.VBComponents.Import ModulePath

    ' Save the workbook
    ActiveWorkbook.Save
End Sub

Note that the code saves the active workbook as an XLSM file before actually doing the import. This is necessary because the Import function may balk if you try to import a VBA module into a workbook that doesn't support macros. In addition, if you get an error when running the macro, you may want to check in the Visual Basic Editor that you've established a reference to the VBA Extensibility object library.

In order to use the macro, make sure you update the ModulePath constant so that it points right to the desired text file. The macro should also be stored in your Personal workbook so that it will be available whenever you are using Excel.

You might also profit by examining some code on Ron DeBruin's site which shows how to both import and export VBA code within a macro:

https://www.rondebruin.nl/win/s9/win002.htm

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11855) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of Stubborn Icons

Ever tried to clean-up the icons on your toolbar only to have one that won't go away? This tip explores some possible ...

Discover More

Wrapping Text Around a Graphic

Place a graphic in your document, and you may want to make sure that your document text "wraps" around the edges of the ...

Discover More

Inserting a Paragraph from within a Macro

Macros are often used to process documents, resulting in changes of one manner or another. If you need your macro to add ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Controlling Window Size when Opening Additional Workbooks

When you open multiple workbooks, the way in which Excel sizes them is not the best for your needs. This tip looks at a ...

Discover More

E-mailing PDF Reports Results in Consistent Crash

It is possible to create macros that send out reports, via e-mail, from within Excel. Frank did this and ran into ...

Discover More

Out of Memory Errors when Accessing the VBA Editor

It can be frustrating when you get error messages doing something that you previously did with no errors. If you get an ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2023-07-29 14:45:00

J. Woolley

When you use the Visual Basic Editor to Export (Ctrl+E) a module, it normally produces a text file of type .bas (like Module1.bas) that inculdes Attribute statements like:
Attribute VB_Name = "Module1"
Attribute VB_Description = "Macros to..."
If you have used Application.MacroOptions or the Macro Options (Alt+F8) dialog to describe your macros, then the .bas file will include Attribute statements like:
Attribute MyMacro.VB_Description = "My macro to..."
Although you can change .bas to .txt (since the file is text), it is desirable to retain the original. To add these macros to another workbook, you can Import (Ctrl+M) the .bas file and these Attribute statements will be properly incorporated. If you copy/paste from a .txt file to a new module, they will not.
See https://sites.google.com/view/MyExcelToolbox


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.