从用户窗体启动用户窗体后,为什么卸载第二个用户窗体会同时关闭两者? [英] Why, after launching a userform from a userform, does unloading the 2nd userform close both?

查看:284
本文介绍了从用户窗体启动用户窗体后,为什么卸载第二个用户窗体会同时关闭两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作表上的按钮将启动一个宏,该宏打开一个用户窗体(例如userform1). Userform1是以非模式加载的,以便用户使用userform1和工作表(即,单击单元格)进行输入.在userform1上有一个按钮,单击该按钮可打开另一个用户表单(例如userform2). Userform2是模态的.单击userform2上的取消"按钮将按预期方式卸载userform2.但是,由于某种原因,它也会卸载我不想要的userform1.如果我将userform1设为模态,则卸载userform2不会卸载userform1;但是,用户不能再使用(即单击)工作表中的单元格.我找不到任何可以为我提供线索的信息,为什么卸载一个用户窗体会同时卸载这两个用户窗体.

A button on a worksheet launches a macro that opens a userform (say, userform1). Userform1 is loaded non-modal in order for the user to use both userform1 and the worksheet (i.e., click cells) for input. There is a button on userform1 that, when clicked, opens another userform (say, userform2). Userform2 is modal. Clicking a Cancel button on userform2 unloads userform2 as it is supposed to; however, it, for some reason, also unloads userform1, which I do not want. If I make userform1 modal, then unloading userform2 does not unload userform1; however, the user can no longer use (i.e., click) the cells in the worksheet. I cannot find any info that will give me a clue as to why unloading one userform unloads both.

推荐答案

我很高兴我偶然发现了这个旧线程.

I am very happy I just stumbled upon this old thread.

我发现关闭VBA编辑器窗口后问题消失了.您确实必须关闭它,将窗口最小化是不够的.窗口是否在同一屏幕上打开也无关紧要.只有关闭它才对我有用.我发现,一旦卸载了 Form2 ,VBA编辑器就会显示 Form2 ,无论我使用的是哪个其他代码模块.

What I discoverd is that the problem goes away when the VBA-editor window is closed. You really have to CLOSE it, minimizing the window is not enough. It also does not matter if the window is opend on the same screen or not. Only closing it did the trick for me. What I discoverd is that as soon as Form2 was unloaded, the VBA-editor shows Form2, no matter what other code-module I was just in.

我旨在将问题隔离在TestWorkbook.xlsm(下面的查看代码)中.在 Form2.Show 之后,我尝试了建议 DoEvents 一行代码,两者都很有帮助.在我的DevelopmentAddIn.xlam中,它们没有帮助,第一个窗体仍然关闭.因此问题仍然可能出在我的AddIn的更复杂的代码中.

I aimed to isolate the problem in a TestWorkbook.xlsm(review code below). I tried out the suggestions DoEvents and a line of code after Form2.Show and both helped. In my DevelopmentAddIn.xlam they did not help, the 1st form still closes. So the problem could still lay in the more complex code of my AddIn.

但是就像我说的那样,尽管我仍然不明白为什么,但是关闭VBA编辑器窗口确实可以解决问题.

But like I said, closing the VBA-editor window does the trick, all though I still do not understand why.

TestWorkbook.xslm (两个用户窗体 Form1 Form2 和一个代码模块 mLoad )

TestWorkbook.xslm (two userforms Form1 and Form2 and a code module mLoad)

mLoad:

Option Explicit
Public Changed As Integer

'***Load the 1st form
Public Sub LoadFirstForm()
    Load Form1

    'allow user to change the active workbook
    Form1.Show vbModeless
End Sub

'***Load a 2nd form (from Form1)
Public Sub LoadSecondForm()
    Dim a  As Integer

    Load Form2

    'continue after 2nd form closes
    Form2.Show vbModal

    'suggestions
    a = 1
    DoEvents

    '2nd form was changed
    If Changed = 1 Then
        Form1.InfoBox.Value = "Changed"

        'process changes

    '2nd form is unchanged
    Else
        Form1.InfoBox.Value = "Unchanged"
    End If
End Sub

Form1 (带有按钮 cmdLoad 和文本框 InfoBox )

Form1 (with button cmdLoad and textbox InfoBox)

'***Load the 2nd form (from Form1)
Private Sub cmdLoad_Click()
    LoadSecondForm
End Sub

Form2:(带有按钮 cmdOK )

Option Explicit

'***Initial status is 'unchanged'
Private Sub UserForm_Initialize()
    Changed = 0
End Sub

'***Status is 'changed'
Private Sub cmdOk_Click()
    Changed = 1

    'close 2nd form and continue
    Unload Me
End Sub

这篇关于从用户窗体启动用户窗体后,为什么卸载第二个用户窗体会同时关闭两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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