Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close User Form

Status
Not open for further replies.

Mantle51

Programmer
Aug 17, 2006
97
US
Is there an event that closes a named user form?

Something like

Buton_Click()

UserForm1.close

End Sub

Thanks...........
 



Hi,

That's something that you can do with your button click event.

Skip,

[glasses] [red][/red]
[tongue]
 
UserForms are basically their own class module, except the (UserForm) object variable isn't declared by yourself, but by VBA instead. If you know the name of your userform, you can close it like so (from an ActiveX control, ie from the Controls Toolbox)..

Code:
Private Sub CommandButton1_Click()
    Dim MyUserForm As UserForm1
    If MyUserForm Is Nothing Then
        Set MyUserForm = UserForm1
    End If
    Unload MyUserForm
End Sub

If you want to use the name of a userform as a variable, then you could loop through them like so ..

Code:
Private Sub CommandButton1_Click()
    Dim UF As UserForm1, myUF As UserForm1, strFormName As String
    strFormName = "UserForm1"
    For Each UF In VBA.UserForms
        If UF.Caption = strFormName Then
            Set myUF = UF
            Exit For
        End If
    Next UF
    If Not myUF Is Nothing Then Unload myUF
End Sub

Here is a good read on the subject.

HTH

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top