VBA“ThisWorkbook.Close”不关闭这个工作簿! [英] VBA "ThisWorkbook.Close" Does not close this workbook!

查看:3460
本文介绍了VBA“ThisWorkbook.Close”不关闭这个工作簿!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用于打开fileB.xls的excel中的VBA宏,然后在打开后立即关闭。因为我们将谈论3个excel文件,我将调用第一个fileA。



用户格式的文件A的按钮代码如下所示: / p>

  Private Sub CommandButton2_Click()
'FileA中的Button代码'
Workbooks.Open文件名:=文件路径/ fileB.xls
ThisWorkbook.Close
End Sub

一次fileB.xls打开它启动一个表单来收集一些用户信息。



但是,原始文件不会关闭,fileB.xls被打开。

  Sub Workbook_Open()
'FileB中的代码'
'显示一个表单,从用户。 '
frmOpenFile.Show
End Sub

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



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



所以,只是为了重申这个问题,FileA打开,启动一个userform,用户点击一个按钮,File B的笑脸,FileA不应该关闭。 / p>

我会感谢任何帮助!



编辑:我应该注意,如果重写FileA直接打开FileC,FileA会正确关闭。用户可以从FileB窗体中禁止文件A关闭吗?

解决方案

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



在VBE中打开相关的Userform,然后按F4打开属性对话框。查找ShowModal属性并更改为False。


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.

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

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.

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

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).

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.

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.

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?

解决方案

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.

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

这篇关于VBA“ThisWorkbook.Close”不关闭这个工作簿!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆