当表单关闭并设置为vbModeless时,如何等待特定代码运行?

时间:2022-01-29 05:52:07

I have a macro that generates a report to show that our bank and accounting records tie out for the day. In the end of the macro a UserForm displays only if a discrepancy is detected and allows the user to remove it from the ListBox once they have taken care of the discrepancy.

我有一个宏,它会生成一份报告,显示我们的银行和会计记录与当天相符。在宏的末尾,UserForm仅在检测到差异时显示,并允许用户在处理了差异之后从列表框中删除它。

In order for the user to analyze the input data (which is imported into the workbook on a separate sheet) while keeping the UserForm open (to track the discrepancies), I simply set the UserForm.Show vbModeless. My issue is now that I'm trying to implement a new function to run after the UserForm is closed but with it in a Modeless state, it simply checks if the condition is met and moves on.

为了使用户能够分析输入数据(在单独的表单中导入到工作簿中),同时保持UserForm打开(跟踪差异),我只是设置了UserForm。显示vbModeless。现在我的问题是,在UserForm关闭之后,我正在尝试实现一个新的函数来运行,但是在一个非模型状态中,它只是检查条件是否满足并继续运行。

I tried to implement a GoTo X in the OKButton event listener, but I'm not sure how to have the code GoTo a line in the main Module when referenced from within the UserForm.

我尝试在OKButton事件监听器中实现GoTo X,但我不知道如何在用户表单中引用主模块中的代码。

My Question: How can I keep this UserForm set as Modeless, and allow the user to navigate through the data with it open, but not continue code unless it's closed / unloaded / OkButton is pressed?

我的问题是:如何将这个UserForm设置为Modeless,并允许用户在打开的数据中进行导航,但除非关闭/卸载/ OkButton被按下,否则不能继续执行代码?

Here's the button Listener:

按钮的侦听器:

Private Sub OKButton_Click()
    '.... Code here
    Me.Hide
    LeftoverValues.Closed = true '(other UserForm if discrepancies not taken care of)
    Closed = true 'boolean to flag this as closed
    GoTo UnmatchedSummaryClosed: '(name of form, GoTo Line in module)
End Sub

Here's the code for the end of the macro - from calling UserForm to end:

下面是宏结尾的代码——从调用UserForm到结束:

    '... Approx 2000 lines worth of code leading to this

    Application.ScreenUpdating = True

    Beep 'To alert users that it's 'finished'

    If WireValues.Count > 0 Or BWGPValues.Count > 0 Then
        Call DifferenceForm    'Discrepancy UserForm - Named UnmatchedSummary
    End If

    Beep   'alert to show discrepancy check is done - shows if needed

    Call warnForm

UnmatchedClosed:   'INTENDED GoTo LINE FROM USERFORM
    If UnmatchedSummary.Closed And LeftoverValues.Closed And WarningForm.Closed Then
        Call ARSummary
    End If

    Call StopTimer(time1, Started1)  'Timer to show how long macro took
    Debug.Print "Total Time: " & ShowTime(time1) & "ms"

    If UnmatchedSummary.Closed And WarningForm.Closed And ARSummaryDone Then
        End             'Also want this to happen only if everything's done
    End If              ' Don't want data to hang if user doesn't close macro template
End Sub

1 个解决方案

#1


1  

You can try putting a call to the function you want to run in the UserForm_Terminate() which fires when the userform is closed.

您可以尝试对要在UserForm_Terminate()中运行的函数进行调用,该函数在userform关闭时触发。

#1


1  

You can try putting a call to the function you want to run in the UserForm_Terminate() which fires when the userform is closed.

您可以尝试对要在UserForm_Terminate()中运行的函数进行调用,该函数在userform关闭时触发。