No success deleting modules

B

BrianG

I have three modules in a worksheet. I'm calling a Sub in Module3 which
needs to delete Module1 and Module2 then save the wookbook. The only
time it works is if I run it standalone. If it's called, the modules
aren't removed. I considered deleting all vbComponents but I need
Module3 left intact to transfer control to a new workbook. Maybe I need
to somehow transfer control to a Sub in Module3 rather than call it.
Thoughts?

The called Sub in Module3 is coded as follows:

Sub DelModules()
Dim VBComp1 As VBComponent
Dim VBComp2 As VBComponent
Set VBComp1 = ThisWorkbook.VBProject.VBComponents("Module1")
Set VBComp2 = ThisWorkbook.VBProject.VBComponents("Module2")
ThisWorkbook.VBProject.VBComponents.Remove VBComp1
ThisWorkbook.VBProject.VBComponents.Remove VBComp2
End Sub
 
T

Tom Ogilvy

If you try to save the workbook before the code thread that deletes the
modules completes, then it will be saved with the modules intact. You might
try using application.Ontime to break this thread. Do the save in the code
that the ontime executes.
 

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