Excel VBA - remove VBA code

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

Many thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:

On Chip Pearson's web site, there is an example of the removal of the procedure, "DeleteThisProc":

[ http://www.cpearson.com/excel/vbe.aspx ]

Deleting A Procedure From A Module

This code will delete the procedure DeleteThisProc from the Module1. You must specify the procedure type in order to differentiate between Property Get, Property Let, and Property Set procedure, all of which have the same name.

    Sub DeleteProcedureFromModule()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim StartLine As Long
        Dim NumLines As Long
        Dim ProcName As String
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("Module1")
        Set CodeMod = VBComp.CodeModule
        ProcName = "DeleteThisProc"
        With CodeMod
            StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
            NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
            .DeleteLines StartLine:=StartLine, Count:=NumLines
        End With
    End Sub

Open in new window

Does that help?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Why do you want to remove procedures?
AlanJC99Author Commented:
That is brilliant!

Thank you so much for the quick and really helpful reply!!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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.
[ fanpages ]IT Services ConsultantCommented:
You're welcome, AlanJC99.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.