PDA

View Full Version : check if userform is visible



danovkos
12-09-2010, 06:30 AM
sorry for maybe my stupid question, but how can i check, if my user form is now loaded, visible?
:banghead:
vazby - is my userform

If Vazby.Visible Then Unload Vazby


i tried this, but it works only sometimes. And now it return Run-time error Type mismatch.
it stops and highlighte this part of code (If Vazby.Visible Then )
i dont use "option explicit" is this a problem?

any suggestions?
thx

Bob Phillips
12-09-2010, 06:49 AM
Try something like



Dim uform As MSForms.UserForm

On Error Resume Next
Set uform = UserForms("Vazby")
On Error GoTo 0
If Not uform Is Nothing Then uform.Unload

danovkos
12-09-2010, 07:17 AM
thx XLD,
but maybe i do something wrong...
it seems, as this line
Set uform = UserForms("Vazby")
doesnt works for me...i dont know why...

Begining of my code...

Private Sub Worksheet_Change(ByVal Target As Range)

Dim uform As MSForms.UserForm
Dim a As Long

On Error Resume Next
Set uform = UserForms("Vazby")


but when my code is runing (i am debuggingit my code) and go trough this line and i check in this moment (i am on next line) value uform, it return uform is nothing, but on my sheet is userform "vazby" visible :(. It return in buble (i show with mouse cursor on value)- UserForms("Vazby")=type mismatch

Of course then my script doesnt do what i want. It does not unload userform vazby. :(

Bob Phillips
12-09-2010, 09:07 AM
Can you post the workbook?

mikerickson
12-09-2010, 07:28 PM
The problem with all of these approaches is that by referring to the userform, you are loading it.
This code will returns 1.

Unload UserForm1
If UserForm1.Visible Then
Rem do nothing
End If
MsgBox Userforms.Count

One work around would be to declare a Public variable in a normal module and have the userform's Initialize and Terminate events set that variable
Private Sub UserForm_Initialize()
UFIsHere = True
End Sub

Private Sub UserForm_Terminate()
UFIsHere = False
End SubThat way, one can test the variable without referring to the Userform (i.e. without loading the userform)

Public UFIsHere As Boolean

Sub test()
If UFIsHere Then
MsgBox "UF 1 is loaded"
Else
MsgBox "uf1 not loaded"
End If
End Sub

danovkos
12-10-2010, 12:46 AM
XLD i can not post my wb, because it is too big (23MB) and to many private infos is there. But thx for try.

I will try your advise mikerickson.
I hope it will works for me.

thx mates.
:hi: