Excel – How to get excel to close completely after creating a macro in a personal workbook

microsoft excelmicrosoft-excel-2007

I am using Microsoft Excel 2007 and have several macros in my personal.xlsb workbook, which I use often, so it is very convenient that Excel opens them automatically when it starts. What I don't like is when I click on the "X" in the upper right corner of the window Excel does not exit when I close the last visible workbook. I think that this is because personal.xlsb is still open (though hidden). There are several other questions here on Superuser that have people remove personal.xlsb or move it so it doesn't open on startup (question 65297) or change settings to have only one window show in the taskbar (question 86989). (Sorry there are no hyperlinks–apparently I need more reputation to add additional hyperlinks.)

I would like to have personal.xlsb open when I open Excel, have each Excel window show in the taskbar but have Excel exit when I click the "X" on the last workbook that isn't personal.xlsb. Any thoughts on how to achieve this?

Best Answer

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.

Related Question