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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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
 
Upvote 0
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:
 
Upvote 0
For Each vbCom In Application.VBE.ActiveVBProject.VBComponents
If vbCom.Name = "Module4" Then vbCom.Remove VBComponent:=vbCom.Item("Module4")
Next vbCom
 
Upvote 0
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)!!!!
 
Upvote 0
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"!
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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
Back
Top