2

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

Martin
  • 23
  • 1
  • 1
  • 3

2 Answers2

4

may be you wanted this code in the UserForm code pane

Private Sub UserForm_Terminate()
    ThisWorkbook.Close
End Sub
user3598756
  • 28,893
  • 4
  • 18
  • 28
1

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
Karthick Gunasekaran
  • 2,697
  • 1
  • 15
  • 25