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

Checkboxes in a Merged Document

When creating a mail-merge document, you may want to include some special characters, such as check-marked boxes, in the ...

Discover More

Taking Pictures

Have you ever wanted to take a "picture" of a part of a worksheet and put it in another section? This tip explains how to ...

Discover More

Combining and Formatting Times

Excel allows you to store times in your worksheets. If you have your times stored in one column and an AM/PM indicator in ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Removing a Directory

Macros allow you to perform all sorts of file-related operations. One such operation allows you to delete a directory. ...

Discover More

Adding Buttons to Your Worksheet

You can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.

Discover More

Replacing Letters with Numbers

If you have a text string that contains both letters and numbers and you want to convert those letters to numbers ...

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 5 + 3?

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.