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

Changing Default Tab Stops

If you don't explicitly set tab stops in a paragraph, Word relies upon a default tab stop distance. You can adjust that ...

Discover More

Transposing Two Paragraphs

Need to swap two adjacent paragraphs? Your editing arsenal can include a command to do this if you use the macro in this tip.

Discover More

Rounding in a Mail Merge

Word allows you to merge information into a document from various sources such as an Excel worksheet. If you do, you may ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Putting an X in a Clicked Cell

Need to click on a cell and have it replaced with an "X"? Macros make it easy to do, as illustrated in this tip.

Discover More

Storing Macros in Templates

How Excel uses templates is different than how Word uses templates. This tip looks at those differences and discusses ...

Discover More

Working while a Macro is Running

If you have a macro that takes a long time to process a workbook, you might want to continue working in Excel while the ...

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 three minus 2?

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.