如何关闭属于另一个文件的表单 [英] How to close a form that belongs to another file

查看:64
本文介绍了如何关闭属于另一个文件的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我打开了2个excel文件。两者都包含一个userform,其中包含一个用于关闭表单的按钮。当我在两个表格中的一个上按下该按钮时,我希望两个表格都关闭/隐藏。 

I have 2 excel file open. Both contain a userform with a button to close the form(s). When I press on that button on one of the two forms, I want both forms to close/hide. 

当我这样做时:

Private Sub CommandButton1_Click()

Dim Obj As Object
        For Each Obj In VBA.UserForms
                Obj.Hide
        Next Obj
End Sub

...只关闭一个表格。我猜是因为另一种形式属于另一个文件,因此不在我的代码所看到的Userform集合中。

...only one form is closed. I guess because the other form belongs to the other file and therefore not in the Userform collection my code is looking at.

我想知道是否有办法使这项工作?非常感谢您的帮助!

I wonder if there is a way to make this work? Your help is very appreciated!

推荐答案

您需要致电在另一本书中关闭该书形式的例行程序。试试这样的事情

You need to call a routine in the other book to close that book's form. Try something like this

' Book1 code'    Book1 normal module codePublic gbFormRunning As BooleanSub ShowForm()     UserForm1.Show vbModelessEnd SubSub CloseForm()     If gbFormRunning Then             Unload UserForm1     End IfEnd Sub' Book1 Userform codePrivate Sub UserForm_Initialize()     Me.Caption = ThisWorkbook.Name     gbFormRunning = TrueEnd SubPrivate Sub UserForm_Terminate()     gbFormRunning = FalseEnd Sub'''''''''''' Book2 code' this could be in the button code of a form in Book2Application.Run "Book1.xlsm!module1.CloseForm"

更改Book1.xlsm并更改名称具有要关闭的表单的工作簿,以及包含CloseForm宏的模块的名称的module1。 (明确包括模块名称并非总是必要的)

Change Book1.xlsm and to the name of the workbook with the form you want to close, and module1 to the name of the module that contains the CloseForm macro. (Explicitly including the module name is not always necessary)

Peter Thornton

Peter Thornton


这篇关于如何关闭属于另一个文件的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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