0
votes

I created a UserForm with several functions.

The form opens as I open the Excel file, however if I try to close the form the Excel file close together. Additionally, I can't open the VBA of this Excel (containing the form), so what I do (and that is really dum) it is to open another Excel, press Alt+F11 to open the macro environment and then I can open my Excel file with the UserForm.

I think my problem is in this specific code:

Private Sub UserForm_Terminate()
    'Application.Visible = True
    ActiveWorkbook.Saved = True
    Application.Quit
End Sub

Can anyone guess what is the problem here?

2
What would you like it to do instead of close Excel? The Application.Quit method is what closes Excel when the UserForm is closed. Remove or comment that line out and try again. Is there anything in particular you want done when the UserForm is closed? - Soulfire
possibly you need to use unload userform command instead of Application.Quit which closes excel like it supposed to be. - Dubison

2 Answers

0
votes

If you just want to close the Userform use Unload Me instead of Application.Quit.

0
votes

You need to Show your UserForm as modeless :

UserForm.Show False

This way you would be able to navigate behind the UF and maybe even open VBA directly.

And clearly, if you just close your host application when you quit the UF, it'll be harder to do anything else!

So maybe this will be a better solution :

Private Sub UserForm_Terminate()
    ActiveWorkbook.Saved = True
    'ThisWorkbook.Save


    If MsgBox("Do you want to close Excel?", vbOKCancel + vbQuestion, "Exit Excel?") <> vbOK Then
        'Do nothing
    Else
        Application.Quit
    End If
End Sub

I don't know what your line ActiveWorkbook.Saved = True was for, if it is to save or just to exit without a message, because you don't save the file with that.