I want to check if a userform is loaded or not while running a code. I tried several ways, no-one is working. I remember I did that before, but I can't recall my workaround. Any idea?
I found some solutions here and other places, they also was not working!
The issues are …
Here are my Tries:
Public Function IsFormVisible(FrmName As String) As Boolean
On Error GoTo errorH
IsFormVisible = False
Set Frm = UserForms(FrmName)
If Not Frm Is Nothing Then IsFormVisible = True
End Function
errorH:
IsFormVisible = False
End Function
Public Function IsFormVisible(FrmName As String) As Boolean
Dim Frm As UserForm
On Error GoTo errorH
IsFormVisible = False
For Each Frm In VBA.UserForms
If Frm.Name = FrmName Then
IsFormVisible = True
Exit Function
End If
Next
errorH:
IsFormVisible = False
End Function
Here are two simple options. First, you could declare frm
as Object
:
Public Function IsFormVisible(FrmName As String) As Boolean
Dim Frm As Object
On Error GoTo errorH
IsFormVisible = False
For Each Frm In VBA.UserForms
If LCase$(Frm.Name) = LCase$(FrmName) Then
IsFormVisible = True
Exit Function
End If
Next
errorH:
IsFormVisible = False
End Function
Or second, you could use TypeName
instead of .Name
:
Public Function IsFormVisible(FrmName As String) As Boolean
Dim Frm As UserForm
'On Error GoTo errorH
IsFormVisible = False
For Each Frm In VBA.UserForms
If lcase$(TypeName(Frm)) = lcase$(FrmName) Then
IsFormVisible = True
Exit Function
End If
Next
errorH:
IsFormVisible = False
End Function
I've made both of these case insensitive.