ERLANDSEN DATA CONSULTING Excel & VBA Tips   Informasjon på norsk / Information in Norwegian

These pages are no longer updated and are only available for archive purposes.

Click here to visit the pages with updated information.

Add content to a module from a file

If you don't want to add a complete module, you can add just the missing procedures to 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)
' requires a reference to the Microsoft Visual Basic Extensibility library
' 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"

 

Document last updated 2000-02-05 12:51:07      Printerfriendly version

 

Erlandsen Data Consulting     http://www.erlandsendata.no/   
Excel & VBA Tips   Copyright ©1999-2017    Ole P. Erlandsen   All rights reserved
E-mail Contact Address