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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

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

ADVERTISEMENT

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"!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,161
Messages
5,768,542
Members
425,480
Latest member
br400821

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