Consulting

Results 1 to 4 of 4

Thread: How to close excel properly

  1. #1

    How to close excel properly

    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.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    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

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •