检查me.userform是否仍然打开?

时间:2017-11-23 20:42:33

标签: vba loops optimization userform

我遇到的问题是在其代码中运行了do while循环的userform。当我关闭用户窗体时,循环仍在尝试运行,并导致错误。从只有一个按钮的用户窗体中查看此代码

   Private Sub btnWait_Click()
         Dim TenDays As Date
         TenDays = DateAdd("d", 10, Now) 'Set a date for 10 days in the future
         Do While Now < TenDays
              'just wait
              DoEvents
         Loop

         MsgBox ("Did you really wait ten days to see if this would work?")
    End Sub

运行表单,然后单击按钮 - 什么都不会发生 但是,如果我关闭用户窗体,VBA编辑器仍处于运行模式,因为循环仍处于活动状态(我假设将持续一周以上)

在循环中包含这样的东西会更好吗

         Do While Now < TenDays
              'just wait
              if me.isClosed then exit sub  ' What Real code would work here?
              DoEvents
         Loop

或者在关闭这样的表格时尝试处理它??

Private Sub UserForm_Terminate()
    me.FindRunningloopsandExitThem ' What would this sub look like?
End Sub

提前致谢

0 个答案:

没有答案