无法关闭用户窗体

发布于 2024-08-17 04:05:16 字数 1337 浏览 7 评论 0原文

让我来设置一下环境。

这是在 Excel 中运行的 VBA 代码。

我有一个包含 msflexgrid 的用户表单。此弹性网格显示客户列表以及客户、销售人员、企业社会责任、制造商代表以及区域、分配。当您单击某列(假设在“区域”列下)时,将打开另一个用户窗体以显示“区域”列表。然后,单击您选择的区域,用户表单消失,新区域取代旧区域。

这一切都很有效,直到您单击您选择的区域,“区域”用户表单不会消失(它闪烁)并且新区域不会传输底层用户表单。

我应该提到的是,当我单步执行代码时,它效果很好。

我假设它与 Flexgrid 有关,因为所有其他打开用户表单的用户表单(没有 FlexGrid)都可以正常工作。

以下是一些代码示例:

** 来自 flexgrid 的单击事件,显示区域用户表单以及关闭区域用户表单时新区域的分配。

Private Sub FlexGrid_Customers_Click()

With FlexGrid_Customers

    Select Case .Col
        Case 0
        Case 2
        Case 4
        Case 6
            UserForm_Territories.Show
        Case Else
    End Select

    If Len(Trim(Misc1)) > 0 Then
        .TextMatrix(.Row, .Col) = Trim(Misc1)
        .TextMatrix(.Row, .Col + 1) = Trim(Misc2)
    End If

End With

End Sub

** 以下 Subs 用于 Territory 用户表单

Private Sub UserForm_Activate()

Misc1 = ""
Misc2 = ""

ListBox_Territory.Clear
Module_Get.Territories

End Sub

Private Sub UserForm_Terminate()

Set UserForm_Territories = Nothing

End Sub

Private Sub ListBox_Territory_Click()

With ListBox_Territory
    Misc1 = Trim(.List(.ListIndex, 0))
    Misc2 = Trim(.List(.ListIndex, 1))
End With

Hide
UserForm_Terminate

End Sub

我知道这是一个冗长的解释,但我是一个相当不错的 VBA 程序员,这让我难住了。

任何帮助将不胜感激。

Let me set up the environment.

This is VBA code running in Excel.

I have a userform that contains a msflexgrid. This flexgrid shows a list of customers and the customer', salesperson, csr, mfg rep, and territories, assignments. When you click in a column, let's say under the Territory column, another userform opens to show a list of Territories. You then click on the territory of your choice, the userform disappears and the new territory takes the place of the old territory.

This all works great until you click on the territory of your choice the 'Territory' userform does not disappear (it flickers) and the new territory does not transfer the underlying userform.

I should mention that when I'm stepping through the code it works great.

I'm assuming it has something do to with the flexgrid as all the other userform (that don't have flexgrids) that open userform work just fine.

Following is the some code sample:

** Click event from flexgrid that shows Territory userform and assignment of new territory when territory userform is closed.

Private Sub FlexGrid_Customers_Click()

With FlexGrid_Customers

    Select Case .Col
        Case 0
        Case 2
        Case 4
        Case 6
            UserForm_Territories.Show
        Case Else
    End Select

    If Len(Trim(Misc1)) > 0 Then
        .TextMatrix(.Row, .Col) = Trim(Misc1)
        .TextMatrix(.Row, .Col + 1) = Trim(Misc2)
    End If

End With

End Sub

** The following Subs are used in the Territory userform

Private Sub UserForm_Activate()

Misc1 = ""
Misc2 = ""

ListBox_Territory.Clear
Module_Get.Territories

End Sub

Private Sub UserForm_Terminate()

Set UserForm_Territories = Nothing

End Sub

Private Sub ListBox_Territory_Click()

With ListBox_Territory
    Misc1 = Trim(.List(.ListIndex, 0))
    Misc2 = Trim(.List(.ListIndex, 1))
End With

Hide
UserForm_Terminate

End Sub

I know this a long winded explanation but I'm a fairly decent VBA programmer and this has me stumped.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2024-08-24 04:05:16

我不会说你所做的事情是错误的(因为它永远不会起作用),但它吓坏了我。这不是我处理表单的方式。

首先,您使用 UserForm_Territories(类/表单名称)来引用隐式创建的表单实例。这是我一直避免做的事情。我总是会显式地创建表单的实例,所以不是:

UserForm_Territories.Show

我会这样做:

Dim oTerritoriesForm As UserForm_Territories
Set oTerritoriesForm = New UserForm_Territories

oTerritoriesForm.Show vbModal

' get the values from the form here

Unload oTerritoriesForm

接下来,更令人担忧的是,您通过显式调用它来破坏 UserForm_Terminate 行为。我无法想象你为什么要这样做,除非你认为它可以解决你所说的问题。我的建议:不要这样做。

更糟糕的是,您试图分配给该 Terminate 方法中隐式创建的表单实例。你也不应该这样做。我很惊讶甚至可以编译。

看起来您正在尝试强制隐式创建的表单实例来模仿显式创建的实例。在这种情况下,请显式创建它,如上所示。

I'm not going to say what you're doing is wrong (in that it won't ever work), but it scares the heck out of me. This is not the way I'd deal with forms.

Firstly, you're using UserForm_Territories (the class/form name) to refer to an implicitly-created instance of the form. This is something I've always avoided doing. I would always create an instance of a form explicitly, so instead of:

UserForm_Territories.Show

I would do:

Dim oTerritoriesForm As UserForm_Territories
Set oTerritoriesForm = New UserForm_Territories

oTerritoriesForm.Show vbModal

' get the values from the form here

Unload oTerritoriesForm

Next, and much more worryingly, you're subverting the UserForm_Terminate behaviour by calling it explicitly. Why you're doing this I can't imagine, unless you thought that it would work around your stated problem. My advice: don't do that.

Worse, you're attempting to assign to the implicitly-created instance of the form within that Terminate method. You shouldn't be doing that, either. I'm surprised that even compiles.

It seems like you're trying to force the implicitly-created instance of the form to mimic an explicitly-created one. In which case, create it explicitly, as shown above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文