delete modules using vba

Johnwayne

Board Regular
Joined
Sep 6, 2005
Messages
103
hi,
Is there a way to delete modules using vba

i created 2 macros called
1) sub computation1 () in module 1
2) sub computation2 () in module 1

is there a way i can create a button to delete this 2 macros?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
Sub Delete_VBA_Modules()
Dim vbCom As Object
     
    Set vbCom = Application.VBE.ActiveVBProject.VBComponents
    
    vbCom.Remove VBComponent:= _
    vbCom.Item("Module1")
    'vbCom.Remove VBComponent:= _
    'vbCom.Item("Module2")
     
End Sub
 
Upvote 0
Not done this myself. I think it would be easier to put the routines into a module of their own and remove that :-
Code:
    Set MyModules = ActiveWorkbook.VBProject.vbComponents
    MyModules.Remove VBComponent:=MyModule("Module1")
 
Upvote 0
Because I am a masochist I have done it anyway :-
Code:
'=================================================================================
'- MACRO TO DELETE *SUBROUTINES* FROM A VBA CODE MODULE
'- Check Tools/References/"Microsoft Visual Basic for Applications Extensibility"
'- Brian Baulsom August 2007
'=================================================================================


'=================================================================================
'- MAIN ROUTINE
'=================================================================================
Sub DeleteSubroutines()
    DeleteSubroutine "Module1", "computation1"
    DeleteSubroutine "Module1", "computation2"
End Sub
'========= END OF MAIN ROUTINE ===================================================



'=================================================================================
'- SUBROUTINE : CALLED FROM MAIN ROUTINE
'=================================================================================
Private Sub DeleteSubroutine(ModuleName, SubName)
    Dim MyModule As Object
    Dim MyLineNumber As Integer
    Dim MyLine As String
    Dim StartLine As Integer
    Dim EndLine As Integer
    Dim MySubLines As Integer
    '-----------------------------------------------------------------------------
    Set MyModule = ActiveWorkbook.VBProject.vbComponents(ModuleName).codemodule
    MyLineNumber = 1
    With MyModule
        '-----------------------------------------------------------------------------
        '- Find subroutine
        '-----------------------------------------------------------------------------
        For MyLineNumber = 1 To .countoflines
            MyLine = .Lines(MyLineNumber, 1)
            If InStr(1, MyLine, SubName, vbTextCompare) > 0 Then
                StartLine = MyLineNumber
                Exit For
            End If
        Next
        '--------------------------------------------------------
        '- check subroutine found
        If MyLineNumber >= .countoflines Then
            MsgBox ("Cannot find Sub " & SubName & "()" & vbCr _
                  & "in module '" & ModuleName & "'")
            Exit Sub
        End If
        '-----------------------------------------------------------------------------
        '- Find End Sub
        '-----------------------------------------------------------------------------
        While InStr(1, MyLine, "End Sub", vbTextCompare) = 0
            MyLineNumber = MyLineNumber + 1
            MyLine = .Lines(MyLineNumber, 1)
        Wend
        EndLine = MyLineNumber + 1
        '-----------------------------------------------------------------------------
        '- delete lines
        '-----------------------------------------------------------------------------
        MySubLines = EndLine - StartLine
        .DeleteLines StartLine, MySubLines
    End With
    '----------------------------------------------------------------------------------
    MsgBox ("Deleted Sub " & SubName & " ( )" & vbCr _
          & "from module '" & ModuleName & "'" & vbCr & "= " & MySubLines & " lines.")
End Sub
'----------- end of sub routine --------------------------------------------------------
 
Upvote 0
cant seems to work

sorry guys, none of these code work..

what i want is a code to delete my modules..
i have 6 modules..and by pressing a button all 6 modules will be empty or deleted..
i cant seems to exeute the code.

sorry im kind of new in vba..
 
Upvote 0
error msg

i received this error msg once i execute the macros given
"method vbe of object_application failed"
 
Upvote 0
I tested this in Excel 2003 and 2007, it worked fine
Code:
Sub Delete_VBA_Modules() 
Dim vbCom As Object 
      
    Set vbCom = Application.VBE.ActiveVBProject.VBComponents 
    
    vbCom.Remove VBComponent:= _ 
    vbCom.Item("Module1") 
    vbCom.Remove VBComponent:= _ 
    vbCom.Item("Module2") 
    vbCom.Remove VBComponent:= _ 
    vbCom.Item("Module3") 
    vbCom.Remove VBComponent:= _ 
    vbCom.Item("Module4") 
    vbCom.Remove VBComponent:= _ 
    vbCom.Item("Module5") 
    vbCom.Remove VBComponent:= _ 
    vbCom.Item("Module6") 
      
End Sub
 
Upvote 0
'- Check Tools/References/"Microsoft Visual Basic for Applications Extensibility"

Have you checked this out, as mentioned in my code ?
(from VB Editor menu)
 
Upvote 0

Forum statistics

Threads
1,223,753
Messages
6,174,307
Members
452,554
Latest member
Louis1225

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top