excel vba projects not closing

11,261

Solution 1

try... It works for me in a similar type of program.

'closes data workbook
source_wb.Close False

Solution 2

It seems that the VBE editor is not always visible to the workbook that is being closed.

I included the following code in my ThisWorkbook module which comes from a comment in another thread and this resolved matters.

http://dailydoseofexcel.com/archives/2004/12/11/google-desktop/

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
' -------------------------------------------------------------
' this code ensures that the VBA project is completely removed
' when the workbook is closed
' http://dailydoseofexcel.com/archives/2004/12/11/google-desktop/
' -------------------------------------------------------------
 If Not (Application.VBE.MainWindow.Visible) Then
 Application.VBE.MainWindow.Visible = True
 Application.VBE.MainWindow.Visible = False
 End If
 End Sub

Solution 3

I recently had this problem: I have a workbook that grabs data from other workbooks that I use as databases. On one of these, I inadvertently placed some code. This caused the workbook to remain visible in VBE even after it had been closed. My solution was to keep my database workbooks free of code, and that solved the problem.

Share:
11,261
elbillaf
Author by

elbillaf

Father. Programmer.

Updated on June 14, 2022

Comments

  • elbillaf
    elbillaf about 2 years

    I'm going through 100s of excel files in VBA, extracting certain data and copying it to a main spreadsheet in a main workbook. I have a VBA script that resides in this main spreadsheet.

    I'm trying to get each source workbook to close after I open it and get what I need. It looks something like this:

    dim main_wb
    dim source_wb
    set main_wb = activeworkbook
    
    Loop thru workbook names
      set source_wb = workbooks.open(a_workbook_name)
    
      do some stuff
      eventually copy a few rows from various sheets into the main wb
    
      source_wb.close()
      set source_wb = Nothing
    End Loop
    

    The problem is that it SEEMS like the system is continuing to keep the file open in the project explorer ... and eventually it runs out of memory or something. All files work fine individually. It's only when I attempt to process them all at once that I have a problem. The workbook "closes()" but the project still exists in the project explorer in the developer window.

    How do I tell it to close out a project. I need to be able to, no BS, close the project and go on to the next one for hundreds and potentially thousands of files - automatically, in code, no intervention from user.

  • elbillaf
    elbillaf almost 11 years
    Cr@p! That seems to have worked! Thanks! Never would have occurred to me that that would make a difference!
  • elbillaf
    elbillaf almost 11 years
    Nope. It worked one time, but failed the second run...maybe that's the thing ... I have to exit out and run again every time. Weird.
  • elbillaf
    elbillaf almost 11 years
    Not working at all now. Interesting ... it worked one time and not again. So strange.
  • elbillaf
    elbillaf almost 11 years
    Working consistently now - in the sense that it doesn't fail, but all those workbooks are still listed in the project browser.
  • elbillaf
    elbillaf almost 11 years
    Weirdness. I don't know what I did, but after the process runs, the workbooks are no longer listed. That works!
  • Mike
    Mike almost 11 years
    wonderful.. i have never had trouble with this but I know sometimes excel is funky for a little bit then starts working again.. glad it is working!
  • Jon Peltier
    Jon Peltier over 4 years
    The problem has resurfaced recently, and following this solution, ensuring that the VBE window is closed when the workbook is closed, helps as often as not following it.