PROPER/CORRECT way to save, close workbook and close Excel through VBA

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
I have the following code that I want to save data, save workbook, close workbook and quit the excel application. I think it is closing the workbook before it can execute the Application.Quit line. It is executed by a button.

Code:
Private Sub CommandButton4_Click()
Call SaveTextBoxData
ThisWorkbook.Save
MsgBox ("Your data has been saved."), vbInformation
ThisWorkbook.Close
Application.Quit
End Sub
1. What do I change to make it do what I want, i.e., to quit Excel AFTER closing????
2. Is the "ThisWorkbook.save" and "thisworkbook.close" the proper way to save and close (as opposed to "Activeworkbook.save" and "activeworkbook.close"????
3. Does the Application.Quit command automatically and correctly close the workbook???? Do you even need the "Thisworkbook.close" line?

thanks
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,952
Office Version
365, 2010
Platform
Windows
You shouldn't need the Save line:

Code:
Private Sub CommandButton4_Click()
Call SaveTextBoxData
MsgBox ("Your data has been saved."), vbInformation
ThisWorkbook.Close (True)
Application.Quit
End Sub
 

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
why not? I want to save the workbook (in addition to saving my textbox data to the workbook). What about the other questions numbered 1,2,3?
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,952
Office Version
365, 2010
Platform
Windows
The TRUE parameter of ThisWorkbook.Close tells it to save changes.

You want to save your workbook somehow before quitting the application.
 

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
Ok I understand the True statement. But since I have Application.Quit as the last line, doesn't the workbook save AND close BEFORE it gets to the .Quit, thus never executing the .Quit line and leaving Excel open?
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
1. What do I change to make it do what I want, i.e., to quit Excel AFTER closing????
2. Is the "ThisWorkbook.save" and "thisworkbook.close" the proper way to save and close (as opposed to "Activeworkbook.save" and "activeworkbook.close"????
3. Does the Application.Quit command automatically and correctly close the workbook???? Do you even need the "Thisworkbook.close" line?
Ok I understand the True statement. But since I have Application.Quit as the last line, doesn't the workbook save AND close BEFORE it gets to the .Quit, thus never executing the .Quit line and leaving Excel open?

Greetings,

From your questions... You cannot quit the application (or do anything else for that matter) after the code quits running, which it does upon the WB closing. In short, you may want the code to check for other open WBs, see if they are saved or handle if they are not, close them, then after saving ThisWorkbook, quit the application.

Always use ThisWorkbook instead of ActiveWorkbook, unless the code really needs to run against another open workbook (in which case I would set a reference to the other WB and use the object name).

Anyways, here is an example of something like what you might be trying to do.

Hope it helps,

Mark

Code:
Option Explicit
'
Private Sub CommandButton4_Click()
Dim WB As Workbook
'
  'Call SaveTextBoxData
'
  If Not ThisWorkbook.Saved Then
    ThisWorkbook.Save
    MsgBox "Your data has been saved.", vbInformation, vbNullString
  End If
'
  For Each WB In Application.Workbooks
    If Not WB.Saved Then
      If MsgBox("You are about to exit the application.  The workbook named """ & WB.Name & """ is not saved.  Would you like to save """ & WB.Name & """ before it closes?", vbQuestion Or vbYesNo Or vbDefaultButton1, "Save Workbook...?") = vbYes Then
        WB.Close SaveChanges:=True
        DoEvents
      Else
        'WB.Saved = True
        WB.Close SaveChanges:=False
      End If
    End If
  Next
'
  'All workbooks (except this one) are closed, and we already saved this workbook, so just quit the application.
  Application.Quit
End Sub
 

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
Thanks for responding, but I am still confused. I have one workbook (bunch of sheets). I have two command buttons on a userform.

Command button label " Save and Exit". I want to save the one workbook, close it (if necessary), and then Quit/close Excel. The code:
Code:
Private Sub CommandButton4_Click()
Call SaveTextBoxData
ThisWorkbook.Save
MsgBox ("Your data has been saved."), vbInformation
Application.Quit
End Sub
[code]
Does this code do what I want?


The second button label "Exit Without Saving".  I simply want to close the file (if necessary) and quit excel without saving the file.  The code.

[code]
Private Sub CommandButton13_Click()
Application.DisplayAlerts = False
Application.Quit
Application.DisplayAlerts = True
End Sub
I think this piece of code needs some changing ???
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
Thanks for responding, but I am still confused. I have one workbook (bunch of sheets). I have two command buttons on a userform.

Command button label " Save and Exit". I want to save the one workbook, close it (if necessary), and then Quit/close Excel. The code:
Code:
Private Sub CommandButton4_Click()
Call SaveTextBoxData
ThisWorkbook.Save
MsgBox ("Your data has been saved."), vbInformation
Application.Quit
End Sub
Does this code do what I want?
Not tested and I have no idea of what happens in SaveTextData, but yes, I think that should work. Not being obstinate, but it does not check to see if the user has other workbooks open, or whether any such other workbook is saved.
The second button label "Exit Without Saving". I simply want to close the file (if necessary) and quit excel without saving the file. The code.

Code:
Private Sub CommandButton13_Click()
Application.DisplayAlerts = False
Application.Quit
Application.DisplayAlerts = True
End Sub
I think this piece of code needs some changing ???
Skipping the part about any possible other workbooks, rather than killing alerts, why not just ThisWorkbook.Saved = True, then Application.Quit ?

Hope that helps,

Mark
 

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
yes it does. thank you and thanks for the code that checks if other workbooks are open.

By the way, I did what you said just above here regarding ThisWorkbook.Saved=True, then application quit. Upon opening again, excel crashed without giving me a reason. I know you do not know my code, but could this way of closing/quitting have anything to do with it? I could then open the file with macros disabled, and then ran the userform using the "run" command and everything worked fine.
 

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
Hey GTO
Was looking at your code (making sure all workbooks are saved). Why is one line commented out (WB.Saved=True)? I am still relatively new to VBA and trying to figure it out
 

Watch MrExcel Video

Forum statistics

Threads
1,099,007
Messages
5,465,981
Members
406,458
Latest member
Barboza Babcock

This Week's Hot Topics

Top