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

Alt+Enter Stopped Working Correctly

What do you do if a keypress you know worked correctly before all of a sudden stops working as you expect? This tip ...

Discover More

Using TC Fields for Notes

The TC field is normally used in constructing manual Tables of Contents. The way the field works, however, makes it a ...

Discover More

Tracing Precedent Cells

Cells that affect another cell are called precedent cells. If you need to know which cells affect a particular cell, ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Copying a Set Range from Multiple Worksheets to a New Worksheet

Want to create a summary worksheet that pulls a single row of data from each worksheet in the workbook? Here are a couple ...

Discover More

Negating a Cell Using a Macro

There are two ways to create macros: recording them or writing them from scratch. Some things cannot be done in a macro ...

Discover More

Displaying Worksheets in a Slideshow Fashion

Want to step through the worksheets in a workbook, displaying them like a slideshow? The macros provided in this tip can ...

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 two more than 7?

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.