简体   繁体   中英

Excel VBA ThisWorkbook Close Stopped Working

I am curious as to the possible reasons why ThisWorkbook.Close has stopped working when a button on a userform is clicked. Now I receive a 1004 error

Method Close or object Workbook failed

Previous backups of the workbook are fine and close down without error. Events have not been disabled. ThisWorkbook.Save or Application.Quit work ok.

Could getting trapped and have to resort to using Ctrl-Alt-Del to kill Excel to close the workbook do anything?

Any ideas?

Private Sub cmbClose_Click()

On Error GoTo errHandler:

'   Close workbook
ThisWorkbook.Close

'   Error handler
On Error GoTo 0
Exit Sub

errHandler::
    MsgBox "An Error has Occured " & vbCrLf & "The error number is : " _
    & Err.Number & vbCrLf & Err.Description & vbCrLf & vbCrLf & _
    "Please take a screenshot of this message and contact the developer "

End Sub

Just my guesses, but did you save your workbook before closing? I changed

ThisWorkbook.Close

to

ThisWorkbook.Close False

and it works perfectly as a sub. ( The "False" there is for property "SaveChanges", so I'm forcing it to close without prompting for saving coz I was testing this on a newly created workbook )

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