Determining whether Userform2 is Loaded : Error Object Variable or With Block Variable not set

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
688
Hello

I've button which loads Userform2.

In Second command button i want to know whether userform2 is loaded

in Gen Module
Code:
Public usrForm As Object

Function IsUserFormLoaded(ByVal usfrName As String) As Boolean
  
  IsUserFormLoaded = False
  For Each usrForm In VBA.UserForms
    If usrForm.Name = usfrName Then
      IsUserFormLoaded = True
      Exit For
    End If
  Next
End Function

in Userform1
Code:
Private Sub cmd1_Click()
Load UserForm2
UserForm2.Show vbModeless
End Sub

Private Sub cmd2_Click()
''''Option 1
    If IsUserFormLoaded("Userform2") Then
        MsgBox "Form Loaded:"
    End If
[B]Here No Msgbox with msg is displayed even if form is loaded[/B]

''''Option 2

   If usrForm.Name = "Userform2" Then
[COLOR=#ff0000][B]'Error on the above line " Object Variable or With Block Variable not set
[/B][/COLOR]             MsgBox "Form Loaded:"
   End If
End Sub
Thanks NimishK
 
Last edited:
There is a subtle bug in the previous code as it loads UserForm1 whern terminating UserForm2.

Better to just have the Boolean variable in a Standard Module or better still if you want to keep everything inside the UserForm Module, use an application level Name as follows :

1- In UserForm1 Module:
Code:
Private Sub cmd1_Click()
    Load UserForm2
    UserForm2.Show vbModeless
End Sub

Private Sub cmd2_Click()

    If Application.ExecuteExcel4Macro("Test") = "UserForm2Loaded" Then
        MsgBox "UserForm2 Loaded."
    Else
        MsgBox "UserForm2 Not Loaded."
    End If

End Sub


2- In UserForm2 Module:
Code:
Private Sub UserForm_Initialize()
    Application.ExecuteExcel4Macro "SET.NAME(""Test"",""UserForm2Loaded"")"
End Sub

Private Sub UserForm_Terminate()
    Application.ExecuteExcel4Macro "SET.NAME(""Test"",""UserForm2NotLoaded"")"
End Sub
 
Last edited:
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Jaafar Tribak
Hi
Your second Reply ie post #11 sounds more practical.
BTW what is ExecuteExcel4Macro

In anyway can i get the Name of Userform(s) in Msgbox ie userform2 or userform3 etc if few UFs are added though i earlier menioned in post #9 particular userform2.
Ofcourse will have respective command buttons to load Each Form .

Thanks NimishK
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top