2
votes

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? :)

2
Does your code reach the line after Application.Quit? - Verzweifler
I tried to set the Application.Quit line last, but I still have the same problem. - Martin
Have you tried this solution yet? stackoverflow.com/questions/3628252/… - Ralph
if you open your task manager and see that excel is still open, you may have to set excel=0, search for "Why won't excel close" - Davesexcel

2 Answers

4
votes

may be you wanted this code in the UserForm code pane

Private Sub UserForm_Terminate()
    ThisWorkbook.Close
End Sub
1
votes

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