» Add content to a module from a file using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Modules, Class Modules in VBA
VERSION - All Microsoft Excel Versions
If you don't want to add a complete module, you can add just the missing proceduresto an existing module by using the macro below. It adds the content of a text file to an existing module:
Sub ImportModuleCode(ByVal wb As Workbook, _
ByVal ModuleName As String, ByVal ImportFromFile As String)
' imports code to ModuleName in wb from a textfile named ImportFromFile
Dim VBCM As CodeModule
If Dir(ImportFromFile) = "" Then Exit Sub
On Error Resume Next
Set VBCM = wb.VBProject.VBComponents(ModuleName).CodeModule
If Not VBCM Is Nothing Then
VBCM.AddFromFile ImportFromFile
Set VBCM = Nothing
End If
On Error GoTo 0
End Sub
Example:
ImportModuleCode ActiveWorkbook, "TestModule", "C:\FolderName\NewCode.txt"
Book Store:
Recommended Books:
- Mortgages For Dummies®
- Essentials of Accounting and Post Test Booklet 8, Eighth Edition
- Marketing Planning for Services
- Advanced modelling in finance using Excel and VBA
- Microsoft Office Xp: Advanced Concepts and Techniques: Word 2002, Excel 2002, Access 2002, Powerpoint 2002
- Microsoft Outlook 2002 for Dummies
No comments have been submitted.

