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:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Your name test is case sensitive and is probably failing because it is comparing UserForm2 to Userform2, which are not the same. Change it to:

Code:
Public usrForm As Object

Function IsUserFormLoaded(ByVal usfrName As String) As Boolean
  
  IsUserFormLoaded = False
  For Each usrForm In VBA.UserForms
    If lcase$(usrForm.Name) = LCase$(usfrName) Then
      IsUserFormLoaded = True
      Exit For
    End If
  Next
End Function
 
Upvote 0
Sorry RoryA i tried Below But again Error Poped Up

Code:
Function IsUserFormLoaded(ByVal usfrName As String) As Boolean
  
  IsUserFormLoaded = False
  For Each usrForm In VBA.UserForms
    If LCase$(usrForm.Name) = LCase$(usfrName) Then
      IsUserFormLoaded = True
      Exit For
    End If
  Next
End Function
 
Upvote 0
Perhaps its the Typo:-
This :-
Code:
LCase$(usrform.Name)

Not this:-
Code:
LCase$(usfrName)
 
Upvote 0
Thanks MickG

It worked. Also FYI
Even
Code:
 If usfrName = usfrName Then  ..... without Lcase$ Also worked.
Will it be better to put Lcase$ ? as name test is case sensitive replied by RoryA post #2
 
Upvote 0
Kindly wait Just tried the following
Code:
Option Explicit


Private Sub cmdLoadUf2_Click()
Load UserForm2
UserForm2.Show
End Sub


Private Sub cmdTrial_Click()
If IsUserFormLoaded("Userform2") Then
        MsgBox "Form Loaded: " & vbCrLf & IsUserFormLoaded(usfrName) & vbCrLf & usrForm.Name   'usfrName
    End If
End Sub
Guess what usrForm.Name displays the Name as Userform1 instead of Userform2. Is it because the focus is on Userform1 as both the command buttons are on userform1
 
Upvote 0
What error are you getting?
 
Upvote 0
What error are you getting?
Really not able to figure it out. Because just clicking on trial button without clicking on cmdLoaduf2_click()
Msgbox is still displayed.

I want specifically for Userform2 if loaded then display msg
 
Last edited:
Upvote 0
If the checking is just for UserForm2 you could do without the generic function and just set up a boolean flag that can be toggled upon loading and unloading UserForm2.

See if this works for you :

1- In UserForm1
Code:
Public UserForm2Loaded As Boolean

Private Sub cmd1_Click()
    Load UserForm2
    UserForm2.Show vbModeless
End Sub

Private Sub cmd2_Click()

    If UserForm2Loaded Then
        MsgBox "UserForm2 Loaded."
    Else
        MsgBox "UserForm2 Not Loaded."
    End If

End Sub

2- In UserForm2 :
Code:
Private Sub UserForm_Initialize()
   UserForm1.UserForm2Loaded = True
End Sub

Private Sub UserForm_Terminate()
      UserForm1.UserForm2Loaded = False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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