Delete a macro using a macro

S

StephenT

Hello, I have written a macro to clean my workbook before publishing, and I
would like to delete a macro using this macro.

Can someone please help with the code to delete a macro?

This won't break the internet will it?
 
R

Ron de Bruin

Hi StephenT

Try this if "Trust access to VBA project object model" is checked if the macro is in Module1


Sub RunMacroAndDeletModule()
MsgBox "By module1"
With ThisWorkbook.VBProject.VBComponents
.Remove .Item("Module1")
End With
End Sub
 
S

StephenT

Thanks Rob

Unfortunately no joy there.

The code works if I record a new macro called Module1 (the macro calls
itself 'myworksheet.xls'!Module1.Module1). The code then deletes that new
module.

But if I try to replace the "Module1" with my own macro that I want to
delete "deleteme1" then it doesn't work i.e. :

Sub RunMacroAndDeletModule()
MsgBox "By deleteme1"
With ThisWorkbook.VBProject.VBComponents
.Remove .Item("deleteme1")
End With
End Sub

Any advice for killing my macro called "deleteme1"? I also have other macros
that I don't want deleted at the same time

thanks
S
 
S

StephenT

Oh I get it... Put all the macros you need to delete in their own module,
then delete the module using your original code.

Works now, thanks!
 

Ask a Question

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.

Ask a Question

Top