在VBA中,可以使用下列代碼來判斷窗體是否加載:
Function IsFormLoaded(formName As String) As Boolean
Dim frm As Object
For Each frm In VBA.UserForms
If frm.Name = formName Then
IsFormLoaded = True
Exit Function
End If
Next frm
IsFormLoaded = False
End Function
使用方法如下:
Sub Test()
If IsFormLoaded("Form1") Then
MsgBox "Form1 is loaded."
Else
MsgBox "Form1 is not loaded."
End If
End Sub
在上面的示例中,如果名為"Form1"的窗體已加載,則將顯示消息"Form1 is loaded.“,否則顯示消息"Form1 is not loaded.”。