Tom’s Tutorials For Excel: Exporting VBA Module Code to a Text File

Tom’s Tutorials For Excel: Exporting VBA Module Code to a Text File

Here’s a quick way to export all the VBA code from a workbook module, assuming for example:
• The module of interest is named Module1.
• The text file that will receive this code is named “YourFileName.txt”.
• “YourFileName.txt” is located in the folder path C:\YourFilePath\.
• “YourFileName.txt” is closed and you want it to stay closed.
• I used Notepad as the text editor application for the text file.

First, for the workbook holding the module of interest, open that workbook and hit Alt+F11 to go into the VBE. From the menu bar click Tools > References, and establish a reference to
Microsoft Visual Basic For Applications Extensibility 5.3.





Next, hit Ctrl+G to open the Immediate window.

Then, type in or copy and paste this single line of code into the Immediate window:

ActiveWorkbook.VBProject.VBComponents
("Module1").Export"C:\YourFilePath\YourFileName.txt"

Note, I show the single line of code as two lines here, so the entire code can be seen on this web page by all internet browsers. Really, there is no line break, so be sure to place it all into the Immediate window on one continuous line as shown in the next picture:



Finally, with the cursor in the Immediate window, and blinking somewhere in or on that line of code you typed or copied and pasted, hit the Enter key.

The copy is very fast and looks like nothing happened, although the code from Module1 of the active workbook transferred to that closed text file.

The ref to VBA Extensibility 5.3 beforehand is required, but it only needs to be done once.

Share Button
Posted in Tom's Tutorials for Excel
Tags: , , , , , , , , , , , ,
3 comments on “Tom’s Tutorials For Excel: Exporting VBA Module Code to a Text File
  1. Max Nick says:

    Hi there,
    I stumbled across your page when I was looking for a fast way to export modules, bu to a specified Folder.
    I used what your recommended above, and it worked, but then it didn’t
    Let me explain.
    I have several modules, and using your roadmap, I have only been able to export the file at the top of the list. Not the others. Is there a workaround, without having to write a new Module?
    I am using Excel 2003

    Thanks for letting me know.
    Cheers
    Max

    • Tom Urtis says:

      Hi Max – –

      Do you want the code in each module to be in one single Notepad file or do you want the code in each module to be saved into its own separate Notepad file?

  2. Ken says:

    I’d like the code in each module to be in one single Notepad file if you can do that please.

Leave a Reply

Your email address will not be published. Required fields are marked *

*