PDA

View Full Version : How to close excel properly



AS87
11-23-2016, 04:25 AM
I've been using Application.quit to close excel when I close the userform. This worked fine until I introduced it to someone else who used the file and subsequently closed it but then locked me out with the message "locked for editing by another user".

How do I close Excel properly to release any sessions or hooks to the server? The file is stored on a shared drive.

Kenneth Hobs
11-23-2016, 07:13 AM
Welcome to the forum!

I am not sure why you don't just close the workbook and let the user's other files that may be open, stay open.

You need to close and save or not all files open first. If just the macro's workbook open then:

Unload Me 'Close current userform if code in that userform and something like this below.
'close all other workbooks open first.., code can do that.
ThisWorkbook.Close True
Application.Quit 'Should be redundant at this point.

AS87
11-24-2016, 07:46 AM
Set MainWorkbook = ActiveWorkbook

'lots of code inbetween

MainWorkbook.Close savechanges:=True

This seems to be leaving the "session" open and giving a locked for editing error.

Is this the workaround?


Set MainWorkbook = ThisWorkbook

'lots of code inbetween

MainWorkbook.Close True savechanges:=True

Kenneth Hobs
11-24-2016, 08:33 AM
I guess you could keep the ActiveWorkbook open if needed. The key is to close all that you need and then save ThisWorkbook or not and then quit the application. If you want to keep ActiveWorkbook open then that can be coded too.


Sub Main()
Dim MainWorkbook As Workbook, wb As Workbook


'lots of code inbetween

Set MainWorkbook = ThisWorkbook

For Each wb In Workbooks
If wb.Name <> MainWorkbook.Name Then wb.Close True
Next wb
MainWorkbook.Save
Application.Quit
End Sub