excel vba 项目没有关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17952467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:29:35  来源:igfitidea点击:

excel vba projects not closing

excelvbaexcel-vbaproject

提问by elbillaf

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.

我正在 VBA 中浏览 100 个 excel 文件,提取某些数据并将其复制到主工作簿中的主电子表格中。我有一个位于此主电子表格中的 VBA 脚本。

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.

问题是它似乎系统继续在项目资源管理器中保持文件打开......最终它会耗尽内存或其他东西。所有文件单独工作正常。只有当我尝试一次处理它们时才会出现问题。工作簿“closes()”但该项目仍然存在于开发人员窗口的项目资源管理器中。

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.

我如何告诉它关闭一个项目。我需要能够在没有 BS 的情况下关闭项目并继续处理数百个甚至数千个文件的下一个项目 - 自动,在代码中,无需用户干预。

采纳答案by Mike

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

尝试...它在类似类型的程序中对我有用。

'closes data workbook
source_wb.Close False

回答by matthu

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

似乎 VBE 编辑器并不总是对正在关闭的工作簿可见。

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

我在我的 ThisWorkbook 模块中包含了以下代码,它来自另一个线程中的评论,这解决了问题。

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

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

回答by John John

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.

我最近遇到了这个问题:我有一个工作簿,它从我用作数据库的其他工作簿中获取数据。其中之一,我无意中放置了一些代码。这导致工作簿在 VBE 中即使关闭后仍然可见。我的解决方案是让我的数据库工作簿没有代码,这解决了问题。

回答by lu76fer

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

解决方案:管理您的保存(是、否、取消) 销毁应用程序中插件的链接 关闭这些插件 关闭您的应用程序