How to create an "save and exit" macro 
Author Message
 How to create an "save and exit" macro

I want to create a macro that will save the excel workbook
and then exit the spreadsheet...


Sat, 04 Jun 2005 11:02:02 GMT  
 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...



Sat, 04 Jun 2005 11:22:58 GMT  
 How to create an "save and exit" macro
In case you actually meant exit the workbook rather than the application
(Excel)

Sub SaveAndClose()
  Activeworkbook.Close Savechanges:=True
End Sub

This assumes the workbook is not a new unsaved workbook.

Regards,
Tom Ogilvy


Quote:
> I want to create a macro that will save the excel workbook
> and then exit the spreadsheet...



Sat, 04 Jun 2005 11:43:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Avoiding "Save before exit" in VBA

2. Exiting w/o "Save" message

3. Change setting for "saving upon exit"

4. Word ask "Save changes on exit"

5. macro autoexec including an "exit command"

6. Auto Exit "Freeze Macro"?

7. exit record after "on exit" event

8. Macro that "Saves as HTML"

9. Macro "lost" after File, Save As

10. "Save as" in macro


 
Powered by phpBB® Forum Software © phpBB Group