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.

Delete a procedure from a module

With the macro below you can delete an existing procedure from a module:

Sub DeleteProcedureCode(ByVal wb As Workbook, _
    ByVal DeleteFromModuleName As String, ByVal ProcedureName As String)
' requires a reference to the Microsoft Visual Basic Extensibility library
' deletes ProcedureName from DeleteFromModuleName in wb
Dim VBCM As CodeModule, ProcStartLine As Long, ProcLineCount As Long
    On Error Resume Next
    Set VBCM = wb.VBProject.VBComponents(DeleteFromModuleName).CodeModule
    If Not VBCM Is Nothing Then
        ' determine if the procedure exist in the codemodule
        ProcStartLine = 0
        ProcStartLine = VBCM.ProcStartLine(ProcedureName, vbext_pk_Proc)
        If ProcStartLine > 0 Then ' prosedyren finnes, slett den
            ProcLineCount = VBCM.ProcCountLines(ProcedureName, vbext_pk_Proc)
            VBCM.DeleteLines ProcStartLine, ProcLineCount
        End If
        Set VBCM = Nothing
    End If
    On Error GoTo 0
End Sub

Example:

DeleteProcedureCode Workbooks("WorkBookName.xls"), _
    "Module1", "ProcedureName"

 

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

 

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