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
Sub OpenFileAndDeleteModule()
Application.ScreenUpdating = False
Workbooks.Open "C:\Excel\book1.xls"
With ActiveWorkbook.VBProject
.VBComponents.Remove .VBComponents("Module1")
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.