Stop VB from deleting modules from wrong file

C

Code Numpty

I have an Excel 2003 template that includes 2 macros, the final bit of code
removes both macros, as shown below
--------------------------------------------------------------------------------
On Error Resume Next

Set vbCom = Application.VBE.ActiveVBProject.VBComponents

vbCom.Remove VBComponent:= _
vbCom.Item("Module3")

vbCom.Remove VBComponent:= _
vbCom.Item("Module4")

On Error Goto 0

End Sub
--------------------------------------------------------------------------------

Users often have multiple files created from the same template open at the
same time, unsaved. When the macro runs it sometimes removes the modules from
the wrong file rather than the active one.

What code do I need to add to ensure that the modules are deleted from the
active file only?
 
C

Code Numpty

So far so good.
Looks like ActiveWorkbook does the trick better than ActiveVBProject.

I just wish I knew why. Thanks Dave.
 
J

Jon Peltier

Because the active project in the VB editor is not necessarily the active
workbook in Excel. Think about it.

- Jon
 

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