by Allen Wyatt
(last updated June 24, 2017)
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11855) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
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!
Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros, ...Discover More
When creating user forms for use in Excel, you are provided with a range of controls you can add, including check boxes. ...Discover More
Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.