Check if userform open

你说的曾经没有我的故事 提交于 2021-01-27 04:31:08

问题


I run a program which repeats itself. It should stop, when the user closes the userform. It runs without stopping.

Since the program calls itself every 8 seconds, I want to check at the end if the userform is still loaded / opened.

Public Sub NextPicture1()
   PictureChange = Now + TimeValue("00:00:08")
   If Onboarding_Projekt.Visible = True Then
      Application.OnTime PictureChange, "NextPicture1"
   End If
End Sub

回答1:


You can use a function like this:

Public Function IsLoaded(formName As String) As Boolean
Dim frm As Object
For Each frm In VBA.UserForms
    If frm.Name = formName Then
        IsLoaded = True
        Exit Function
    End If
Next frm
IsLoaded = False
End Function

Usage:

If IsLoaded("Form_Test") Then
    'Do Something
End If

Your code should look like this:

Public Sub NextPicture1()
   PictureChange = Now + TimeValue("00:00:08")
   If IsLoaded("Onboarding_Projekt") Then
      Application.OnTime PictureChange, "NextPicture1"
   End If
End Sub

Public Function IsLoaded(formName As String) As Boolean
    Dim frm As Object
    For Each frm In VBA.UserForms
        If frm.Name = formName Then
            IsLoaded = True
            Exit Function
        End If
    Next frm
    IsLoaded = False
End Function



回答2:


Here's a version that shows both whether it's loaded and whether it's visible:

Function Userform_Check( _
    form_name As String) _
        As Integer

    ' Returns:
    '   0 - Userform is not loaded
    '   1 - Loaded but not visible
    '   2 - Loaded and visible

    ' mUtilities.Userform_Check()

    Dim frm As Object

    Userform_Check = 0

    For Each frm In VBA.UserForms
        If frm.name = form_name Then
            Userform_Check = 1

            If frm.Visible Then Userform_Check = 2

            Exit For
        End If
    Next frm

' Function Userform_Check( _
    form_name As String) _
        As Integer
End Function



回答3:


I found a field that should help identify it easily:

  If userform.Visible = True Then
      'do something
  End If


来源:https://stackoverflow.com/questions/50563719/check-if-userform-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!