Consulting

Results 1 to 4 of 4

Thread: Solved: Close excel if last open workbook

  1. #1

    Solved: Close excel if last open workbook

    I'd like to add a function to this sub that checks if any other excel workbook is open. If not then close excel application.

    [VBA]
    Sub CloseForceSave()
    'Save the workbook, then close it
    ThisWorkbook.Close savechanges:=True
    End Sub
    [/VBA]

  2. #2
    Hi Rejje,

    Try this:

    Sub CloseForceSave()
     
        'Save the workbook.
        ThisWorkbook.Save
     
        'If this is the only workbook opened in the current session, then...
        If Workbooks.Count = 1 Then
            '...quit Excel
            Application.Quit
        'Else...
        Else
            '...close the active workbook
            ThisWorkbook.Close
        End If
     
    End Sub
    HTH

    Robert

  3. #3
    You're a good teacher - many, many thanks!

  4. #4
    You're welcome

Posting Permissions

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