Test if a Userform is loaded or activated??

R

Robert Crandal

My userform only appears or activates when an event
occurs on my spreadsheet. How can I test whether
or not the Userform is currently loaded or visible??

Does the Userform object have a property or flag
I can check to see if the form is loaded??

thank u
 
P

Per Jessen

Hi

Use the Visible property of the userform to test if it is visible:

If Userform1.Visible=True Then
'Userform is visible
Else
'Userform is not visible
End If

Regards,
Per
 
P

Peter T

Referring to a userform will load it into memory if it wasn't already (even
simply to test its visible property). Try something like this -

Sub test()
Dim i As Long
Dim bIsLoaded As Boolean, bIsVisible As Boolean
Dim sFrmName As String

sFrmName = "UserForm1" ' << CHANGE

For i = 1 To UserForms.Count
If UserForms(i - 1).Name = sFrmName Then
bIsLoaded = True
bIsVisible = UserForms(i - 1).Visible
Exit For
End If
Next

MsgBox bIsLoaded & vbCr & bIsVisible, , sFrmName

End Sub

Regards,
Peter T
 
R

Robert Crandal

How would you modify that test() function to accept
a parameter of a single userform name?? I only want to
test if a single form is loaded.

Here's the template I'm looking for:

Public Sub test (ByVal form_name as String)
'
' ???????
'
End Sub


Thank u Peter
 
P

Peter T

If you *only* want to know if the form is loaded and not interested if it is
visible -

Function IsFormLoaded(sFrmName As String) As Boolean
Dim i As Long
Dim bIsLoaded As Boolean ' , bIsVisible As Boolean

For i = 1 To UserForms.Count
If UserForms(i - 1).Name = sFrmName Then
bIsLoaded = True
' bIsVisible = UserForms(i - 1).Visible
Exit For
End If
Next

' MsgBox bIsLoaded & vbCr & bIsVisible, , sFrmName

IsFormLoaded = bIsLoaded

End Function


to test, try simply
msgbox IsFormLoaded("UserForm1")

If you want to know if the form is visible (and by definition in must be
loaded), look at bIsVisible in the example


Regards,
Peter T
 
R

Rick Rothstein

I know in the end, it doesn't matter, but why not run your For..Next loop
between 0 and UserForms.Count-1 and then reference the "i" variable directly
in your two UserForms references? It's personal choice, of course, but it
always bothers me to directly reference to a UserForm's index using a -1
offset like that.
 
P

Peter T

I'm (almost) sure I've always posted that Userforms loop exactly as you
suggest. But I was getting bored with that way so thought I'd do it
differently, just for a change <g>

OK, real reason, thought it might make it a tad more understandable without
going into explanations that the index of the first loaded form, if there is
one, is 0.

Regards,
Peter T
 

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