简体   繁体   中英

Userform in VBA macro is closing the entire excel file

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?

如果只想关闭用户窗体,请使用Unload Me代替Application.Quit

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM