
How to create an "save and exit" macro
James,
The simple answer to your question is:
ThisWorkbook.Save
Application.Quit
The bad part about that is that it will try to quit the application
regardless of what else may be open.
The code below takes into account the "Personal.xls" workbook
(which is usually hidden if in use at all) and tests to see if anything
else is opened.
Other than the personal.xls workbook, if another workbook is
opened, this will save and exit this workbook.
If no other workbooks are opened, it will save and Quit Excel.
'Option Explicit
Sub CloseAndExit()
ThisWorkbook.Save
Dim WBCount As Integer
WBCount = Application.Workbooks.Count
If WorkbookIsOpen("Personal.xls") Then _
WBCount = WBCount - 1
If WBCount > 1 Then
' user has some other workbook open
ThisWorkbook.Close
Else
Application.Quit
End If
End Sub
Private Function WorkbookIsOpen(wbname) As Boolean
' Function copied from j-walk's site
' Returns TRUE if the workbook is open
Dim x As Workbook
On Error Resume Next
Set x = Workbooks(wbname)
If Err = 0 Then WorkbookIsOpen = True _
Else WorkbookIsOpen = False
End Function
John
Quote:
> I want to create a macro that will save the excel workbook
> and then exit the spreadsheet...