Disable macro using a macro

1Big_Daddy

New Member
Joined
Jul 25, 2007
Messages
5
I have found a way to delete the module the particular macro is in:

Sub DeleteCopyLog()
Dim vbCom As Object

Set vbCom = Application.VBE.ActiveVBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("Module4")

End Sub

However, the code that calls the particular macro I'm deleting reamins in the master macro. When, and if, the user runs the master macro again the error msgbox: "Sub or function not defined" appears!

Can anyone help???

Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Try this:


Sub DeleteCopyLog()
Dim vbCom As Object

On Error GoTo myEnd

Set vbCom = Application.VBE.ActiveVBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("Module4")

myEnd:
End Sub
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022
I'm sorry. But for a moment, it seemed like you said that you want to delete all the code in Module4 and yet have your MAIN macro attempt to CALL subroutines located in Module4? :unsure:
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
For Each vbCom In Application.VBE.ActiveVBProject.VBComponents
If vbCom.Name = "Module4" Then vbCom.Remove VBComponent:=vbCom.Item("Module4")
Next vbCom
 

1Big_Daddy

New Member
Joined
Jul 25, 2007
Messages
5
Yes, Greg....that was was my intent to remove the code so that it would not repeat after being used once.

I found some code on Ozgrid.com that performed the delete function Joe recommened. This brought the current problem of when that module was no longer part of the procedure, the error msg appeared because my main macro still had the "call" line within it.

I think I found a way to write around the problem though.

I added a line within the macro I want to skip over once used placing the word "Done" in a selected cell.

Then by adding a If, Then statement I can jump right over the "call" command If sheet16.range("g2") = "Done" Then.....Exit Sub.


Thanks for both your help (Greg, Joe)!!!!
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022
Yes, you should simply use an IF statement to test whether the subroutine should be called or not. To completely delete the code is what I would call "doing it the hard way"!
 

Forum statistics

Threads
1,186,163
Messages
5,956,290
Members
438,245
Latest member
bhavdip_mangukiya

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
Top