Link to home
Start Free TrialLog in
Avatar of AlanJC99
AlanJC99

asked on

Excel VBA - remove VBA code

Hi
I would appreciate help with writing VBA that will delete certain VBA procedures.  Is this possible?

Many thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why do you want to remove procedures?
Avatar of AlanJC99
AlanJC99

ASKER

That is brilliant!

Thank you so much for the quick and really helpful reply!!
It is possible. You need to use the Microsoft Visual Basic for Application Extensibility library, Set a reference to the library if you want to use early binding.
You will also need to give security permission: Trust Centre>Macro settings> Trust access to the VBA project model.

This Macro uses early binding
Sub DelProc()
    Dim vbp As VBProject
    Dim vbComp As VBComponent
    Dim code As CodeModule
    Dim strModuleName As String
    
    Dim strProcName As String
    Dim lLineStart As Long
    Dim lLineCount As Long
    
    strModuleName = "Module2"
    strProcName = "MyProc"
    
    Set vbp = ActiveWorkbook.VBProject
    Set vbComp = vbp.VBComponents(strModuleName)
    Set code = vbComp.CodeModule
    With code
        lLineStart = .ProcStartLine(strProcName, vbext_pk_Proc)
        lLineCount = .ProcCountLines(strProcName, vbext_pk_Proc)
        .DeleteLines lLineStart, lLineCount
    End With
End Sub

Open in new window

Should have refreshed.
You're welcome, AlanJC99.