PDA

View Full Version : Solved: Close excel if last open workbook



Rejje
11-22-2010, 11:56 AM
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.


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

Trebor76
11-22-2010, 03:53 PM
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

Rejje
11-22-2010, 04:59 PM
You're a good teacher - many, many thanks!

Trebor76
11-22-2010, 07:01 PM
You're welcome ;)