Deleting macros by running a macro?

  • Thread starter Thread starter Fan924
  • Start date Start date
Hi,

Be careful how you use this, there's no going back if you save the workbook.
This code can delete the module it's in (a sort of code suicide) or run from
another module delete that one. Chane the last line to the name of the module
to delete.

Sub Death_To_Code()
Dim ThisModule As Object
Set ThisModule = Application.VBE.ActiveVBProject.VBComponents
ThisModule.Remove VBComponent:=ThisModule.Item("Module2")
End Sub


Mike
 
I found this from 2000

Sub OpenFileAndDeleteModule()
Application.ScreenUpdating = False
Workbooks.Open "C:\Excel\book1.xls"
With ActiveWorkbook.VBProject
.VBComponents.Remove .VBComponents("Module1")
End With
ActiveWorkbook.Save
ActiveWorkbook.Close

End Sub
 
I have been playing with this. From the archives. Deletes all macros.
Sometimes it deletes itself, sometimes it does not.Can it be fixed so
it always deletes itself?

Sub DeleteAllMacros()
Dim Composantvbe As Object
With ThisWorkbook.VBProject
For Each Composantvbe In .VBComponents
If Composantvbe.Type = 100 Then
With Composantvbe.CodeModule
.DeleteLines 1, .CountOfLines
.CodePane.Window.Close
End With
Else: .VBComponents.Remove Composantvbe
End If
Next Composantvbe
ActiveWorkbook.Save
ActiveWorkbook.Close
End With
End Sub
 
Back
Top