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 a procedure to a module

You can add code to a module without using a separate text file that contains the code. The macro below shows how this can be done. The macro must be customized to contain to the code you want to add:

Sub AddProcedureCode(wb As Workbook, strModuleName As String)
' requires a reference to the Microsoft Visual Basic 5.0/6.0 Extensibility library
    On Error Resume Next
    With wb.VBProject.VBComponents(strModuleName).CodeModule
        .InsertLines .CountOfLines + 1, "Sub NewSubName()"
        .InsertLines .CountOfLines + 1, "    Msgbox ""Hello World!"",vbInformation,""Message Box Title"""
        .InsertLines .CountOfLines + 1, "End Sub" & Chr(13)
    End With
    On Error GoTo 0
End Sub

Sub TestAddProcedureCode()
    AddProcedureCode ThisWorkbook, "Module1"
    AddProcedureCode ThisWorkbook, Worksheets("Sheet1").CodeModule
End Sub


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


Erlandsen Data Consulting   
Excel & VBA Tips   Copyright ©1999-2021    Ole P. Erlandsen   All rights reserved
E-mail Contact Address