2
votes

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.

4
A year or two ago I saw something about this problem being associated with Google Desktop Search - do you have that? May also be associated with certain COM add-ins (Acrobat?)Tim Williams
I'd better have look at this: closed workbooks persist in the VBA Project Explorer window when the files are opened and closed manually, too - and it's all too easy to end up editing VBA in modules that look like the project you want to work on, and lose your work because you were actually working on a 'Ghost Project' for a file with the same name - a file that isn't actually open, but still shows up in the Project Explorer.Nigel Heffernan

4 Answers

1
votes

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

'closes data workbook
source_wb.Close False
1
votes

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
1
votes

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.

-1
votes

Solution : Manage your Save (Yes, No, Cancel) Destroy links to Addins in your Application Close these Addins Close your Application