Delete or Remove Event Macros, VBA Code and Module

Brief Description: 
This macro will delete all event macros, sub routines and user defined functions from Active workbook. This requires reference to "Microsoft Visual Basic for Applications Extensibility 5.3" and trusted access to VBA project model.(Developer --> Macro security --> Trust access to VBA project model).

Code: 
Sub RemoveAllVBACode()
'This code will delete ALL VBA code in a VBProject.
On Error GoTo Errorhand
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
        Set VBProj = ActiveWorkbook.VBProject
        For Each VBComp In VBProj.VBComponents
            If VBComp.Type = vbext_ct_Document Then
                Set CodeMod = VBComp.CodeModule
                With CodeMod
                    .DeleteLines 1, .CountOfLines
                End With
            Else
                VBProj.VBComponents.Remove VBComp
            End If
        Next VBComp
    Exit Sub
Errorhand:
        MsgBox "Go to Developer Tab --> Macro Security and enable ""Trust access to VBA project object model."" and then try again."
On Error GoTo 0
End Sub

Comments

CCNA Training said…
Thanks for the article it was interesting