VBA "ThisWorkbook.Close" 不关闭此工作簿!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2282021/
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-08 10:17:34  来源:igfitidea点击:

VBA "ThisWorkbook.Close" Does not close this workbook!

excelvbaexcel-vba

提问by Sumason

I'm Writing a VBA macro in excel that is supposed to open "fileB.xls" and then close immediately after it opens. Since we will be talking about 3 excel files I'll call the first one fileA.

我正在用 excel 编写一个 VBA 宏,它应该打开“fileB.xls”,然后在打开后立即关闭。由于我们将讨论 3 个 excel 文件,因此我将第一个称为 fileA。

The code for the buttons on the userform for file A look like the following.

文件 A 的用户表单上按钮的代码如下所示。

Private Sub CommandButton2_Click()  
    'Code for Button in FileA '
    Workbooks.Open Filename:="File Path/fileB.xls"  
    ThisWorkbook.Close  
End Sub

Once "fileB.xls" opens it launches a form to collect some user information.

一旦“fileB.xls”打开,它就会启动一个表单来收集一些用户信息。

However the original file does not close after, "fileB.xls" is opened.

但是,原始文件在打开“fileB.xls”后不会关闭。

Sub Workbook_Open()
    'Code in FileB '
    'Display a form to obtain information from the user.  '
    frmOpenFile.Show  
End Sub

It might be worth noting that fileB opens a third and final excel file (FileC). Code that is similar to FileA's code is used in FileB and work fine when launching FileC (FileB closes fine).

可能值得注意的是,fileB 打开第三个也是最后一个 excel 文件 (FileC)。与 FileA 的代码类似的代码用于 FileB,并且在启动 FileC 时工作正常(FileB 关闭正常)。

The idea is that there is one "master" file where you choose which form you need to fill out. This master file then closes itself and lets the user fill out the form, and based upon user input a final excel file is opened.

这个想法是有一个“主”文件,您可以在其中选择需要填写的表格。然后,该主文件自行关闭并让用户填写表单,并根据用户输入打开最终的 excel 文件。

So, just to reiterate the problem, FileA opens, launches a userform, the user clicks on a button, File B lauches, FileA does not close when it should.

所以,只是重申这个问题,FileA 打开,启动一个用户表单,用户点击一个按钮,File B 启动,FileA 在它应该关闭的时候没有关闭。

I would be grateful for any help!

我将不胜感激!

EDIT: I should note that if rewrite FileA to directly open FileC, FileA closes correctly. Could the user form from FileB prevent File A from closing?

编辑:我应该注意,如果重写 FileA 以直接打开 FileC,FileA 将正确关闭。FileB 中的用户表单能否阻止 File A 关闭?

回答by Dick Kusleika

Yes, it's the userform. The userform in File C is nonmodal. It's the opposite of the one in File B. When File B opens, the userform opens and all code execution stops until it's closed. With File C, code execution continues. If you change File B's userform properties ShowModal to False, they will work the same.

是的,这是用户表单。文件 C 中的用户表单是非模态的。它与文件 B 中的相反。当文件 B 打开时,用户窗体打开并且所有代码执行停止,直到它关闭。使用文件 C,代码继续执行。如果您将文件 B 的用户窗体属性 ShowModal 更改为 False,它们的工作方式相同。

In the VBE, open the Userform in question and press F4 to open the Properties dialog. Find the ShowModal property and change to False.

在 VBE 中,打开有问题的用户窗体并按 F4 打开属性对话框。找到 ShowModal 属性并更改为 False。

回答by Robert Mearns

If your workbook FileA has changed in anyway the user will be prompted to save the workbook.

如果您的工作簿 FileA 发生了任何更改,系统将提示用户保存工作簿。

This might be hidden from view depending on what other code you are running.

根据您正在运行的其他代码,这可能会隐藏起来。

To avoid this prompt, so long as you don't need to save the changes, this code will work:

为避免此提示,只要您不需要保存更改,此代码将起作用:

ThisWorkbook.Close SaveChanges = False