Auto Close Personal Workbook

eforti

Board Regular
Joined
Aug 15, 2005
Messages
222
Hello All,
In Excel 2007 when you try to close Excel you have to close it twice because your personal workbook is open. I realize this is because I have all windows shown in my taskbar, but that's not a feature I want to give up. I'm trying to find a code I can place in my personal workbook that will make it close excel when it's the only workbook active. The below suggestion is all I've found, but I'm not sure how to execute it.

"Create code for the Workbook.SheetDeactivate Event, that checks if
ActiveWorkbook.Sheets.Count =
(1 or 0, you'll have to play with this to see which works)

Then have it Close the Workbook if it's true."


The concept makes sense to me, but I just don't understand how to make it happen.

Thanks in advance
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You could just Shift+click the Close button.
 
Upvote 0
Thanks for the feedback rorya. I had read that in another post too. While you are absolutely correct I'm hoping for something even more idiot proof (once the coding is installed of course).
 
Upvote 0
1. Copy the code into ThisWorkboook module of the PERSONAL.XLSB
2. Into ThisWorkboook module press Ctrl-S to save PERSONAL.XLSB
3. Quit and reload Excel 2007 to autorun Workbook_Open event subroutine
4. Close workbook to autorun App_WorkbookDeactivate event subroutine

After testing just comment If MsgBox part of the code

Rich (BB code):

' Put code into ThisWorkboook module of the PERSONAL.XLSB and save

Dim WithEvents App As Application

Private Sub Workbook_Open()
  Set App = Application
End Sub

Private Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
  If Workbooks.Count <= 2 Then
    If MsgBox("Quit Excel?", vbYesNo + vbQuestion) = vbYes Then
      Application.Quit
    End If
  End If
End Sub
 
Last edited:
Upvote 0
More correct updated code:
Rich (BB code):

' Put code into ThisWorkboook module of the PERSONAL.XLSB and save
Public WithEvents App As Application

Private Sub Workbook_Open()
  Set App = Application
End Sub

Private Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
  If Not Me.Windows(1).Visible Then
    If Workbooks.Count = 2 Then
      If MsgBox("Quit Excel?", vbYesNo + vbQuestion) = vbYes Then
        App.OnTime Now, ThisWorkbook.CodeName & ".QuitMe"
      End If
    End If
  End If
End Sub

Private Sub QuitMe()
  App.Quit
End Sub
 
Last edited:
Upvote 0
Thanks for the feedback ZVI. The code works as you've designed it but I wonder if it can be done without the msg box? When I click close I'd like to have it just be a one step process as it was in previous versions of excel. The message box prevents me from having to click close twice, but I'm still clicking in two places to get out of excel.
 
Upvote 0
MsgBox is just for testing. In post #4 I’ve mentioned that MsgBox part of the code can be commented.
Like this:
Rich (BB code):

' Put code into ThisWorkboook module of the PERSONAL.XLSB and save
Public WithEvents App As Application

Private Sub Workbook_Open()
  Set App = Application
End Sub

Private Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
  If Not Me.Windows(1).Visible Then
    If Workbooks.Count = 2 Then
      'If MsgBox("Quit Excel?", vbYesNo + vbQuestion) = vbYes Then
        App.OnTime Now, ThisWorkbook.CodeName & ".QuitMe"
      'End If
    End If
  End If
End Sub

Private Sub QuitMe()
  App.Quit
End Sub
 
Upvote 0
Perfect ZVI! Thank you for the help. I misunderstood your point in comment #4. I thought I was commenting on the use of it. Thanks for clarifying.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,713
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top