Testing to find if UserForm is Loaded

D

DanB

I have multiple userforms open and hidden.

How can I test to see if a userform is loaded before attempting to either
Unload or change attributes to any controls contained therein?

thanks,

DanB
 
J

JLGWhiz

Why not use and If ...Then statement:

If UserForm1.Visible = True Then
'Do something
Else
UserForm1.Show
'Do something
End If
 
D

DanB

Not quite what I'm looking for but it'd probably work.
I'll need to check if the result is different for testing both Hidden and
Unloaded Forms.

To be more specific, if I had a form that was previously Unloaded I want to
make sure that it doesn't exist before I try to Unload it again. I was
hoping that there was something as simple as an IsLoaded property, but no
such luck.

If there's another way of detecting, I'm all ears!

Best,
DB
 
J

JLGWhiz

If it is hidden you can use the Show method to make it visible. Show also
will invoke Load so in either case, the Show method will make the form
visible. Testing if it is visible will tell you if it is in a state that
you can unload.

If you are thinking of running a loop to test for the status of each
UserForm, it could be problematic in that if the form is loaded modal, it
must be unloaded to proceed to further code execution in the calling
procedure. If it is loaded modeless, I don't know of a criteria to check
for the hidden forms except by name or whethere it is visible/hidden. To
check by name would probably entail making an array of names to loop
against. Anyhow, the visible property seemed the easiest way to me.
 
D

DanB

Just tested, it .visible comes up False for both states of Hidden or Unloaded.

I guess I'll need to create public flag variables and create code to track
when I load/unload forms.

Thanks for trying,

DB
 
R

Rick Rothstein

I think this function will do what you want, just pass it the name of the
UserForm...

Function IsUserFormLoaded(UserFormName As String) As Boolean
Dim UF As Object
For Each UF In UserForms
If UCase(UF.Name) = UCase(UserFormName) Then
IsUserFormLoaded = True
Exit Function
End If
Next
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top