Consulting

Results 1 to 8 of 8

Thread: Solved: Hidden Userform

  1. #1

    Solved: Hidden Userform

    Is there a way to determine if a userform is hidden?

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Use the userform.visible property. It is used to either test its status or change it.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  3. #3
    I understand the property, I think there is a difference between Hidden vs Unload. From my understanding, Hidden means its out of site, but the data on the userform can be used for other purposes. Unload means (I think) the data on the userform is lost.

    Basically, I just want to know if one particular userform (AppType) is hidden.

    I have this code, but it loops, therefore slows down other codes

    [vba]Dim usrFrm As Object
    For Each usrFrm In UserForms
    If usrFrm.Name = "AppType" Then
    MsgBox "Hidden"
    End If
    Next usrFrm
    [/vba]
    Last edited by av8tordude; 08-30-2012 at 03:40 AM.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Aviator,

    Quickly tested, but for seeing if the form is loaded, maybe:

    [VBA]Option Explicit

    Sub Setup1()
    Load UserForm1
    Load AppType
    MsgBox IsLoaded("AppType")

    Unload AppType
    Load UserForm2
    MsgBox IsLoaded("AppType")
    End Sub

    Function IsLoaded(UserFormName As String) As Boolean
    Dim Count As Long

    For Count = 0 To UserForms.Count - 1
    If UserForms(Count).Name = UserFormName Then
    IsLoaded = True
    Exit For
    End If
    Next
    End Function[/VBA]

    For seeing if the form is loaded and showing/visible, maybe:
    [VBA]Sub Setup2()
    Load UserForm1
    Load AppType

    MsgBox IsLoadedAndHidden("AppType")

    Unload AppType
    Load UserForm2
    MsgBox IsLoadedAndHidden("AppType")

    AppType.Show vbModeless
    MsgBox IsLoadedAndHidden("AppType")

    AppType.Caption = "It's me!"
    Dim n As Long
    For n = 0 To UserForms.Count - 1
    MsgBox UserForms(n).Caption
    Next

    End Sub

    Function IsLoadedAndHidden(UserFormName As String) As Boolean
    Dim Count As Long

    For Count = 0 To UserForms.Count - 1
    If UserForms(Count).Name = UserFormName Then
    If UserForms(Count).Visible Then
    IsLoadedAndHidden = True
    Exit For
    End If
    End If
    Next
    End Function
    [/VBA]

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    ...see reason for editing...
    Last edited by GTO; 08-30-2012 at 04:30 AM. Reason: ACK! Sorry, fumbly fingers

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I should have tacked in:

    The above presumes forms named:
    • UserForm1
    • UserForm2
    • AppType
    For what it is worth, you may wish to consider prefacing (notation) your variable names. 'frmAppType' might make easier sense a few months from now (or at least make sense quicker upon reading the code), when you go back and add or edit code.

    BTW - you are correct as to Load(ed) vs Show(n). If your Load or Show the form, the Object Module is loaded. If you Hide the form, it still exists and any variables existing in it can still be used.

    Hope that helps,

    Mark
    Last edited by GTO; 08-30-2012 at 04:53 AM.

  7. #7
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    If it's something you need regularly within your routine you could use an indicator (named range perhaps?) that changes state during the initialize and terminate events of the userform.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  8. #8
    Thanks you both. I think I will use the boolean procedure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •