Martin
Martin

Reputation: 23

Close workbook with Userform

I have made a User form in Excel, so when i open the xlsm-file it only opens the user form, and the workbook is hidden.

But when i close the user form with the [X]-button I want it to close both the workbook and the user form without saving.

When I close the user form now, and try to open the same file again, it says that it is already/stil open.

Start up code:

Private Sub Workbook_Open()
 Application.Visible = False
 Fordelinger.Show vbModeless
End Sub

Close code:

Private Sub Fordelinger_Deactivate()
Application.Quit
Workbooks("EstimatDOK.xlsm").Close True
End Sub

Can anyone help? :)

Upvotes: 2

Views: 18365

Answers (2)

Karthick Gunasekaran
Karthick Gunasekaran

Reputation: 2713

you can use the below code to restrict close (X) button

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
        MsgBox "The X is disabled, please use a button on the form to Exit.", vbCritical
    End If
End Sub

or

Private Sub UserForm_Terminate()
    ThisWorkbook.Close savechanges:=False
    Application.Quit
End Sub

Upvotes: 1

user3598756
user3598756

Reputation: 29421

may be you wanted this code in the UserForm code pane

Private Sub UserForm_Terminate()
    ThisWorkbook.Close
End Sub

Upvotes: 4

Related Questions