Quit current workbook, but not other active workbooks

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.

Got a question.
I got a timer that counts from 1 min down, then runs a Sub called shutdown.
The problem is, when it closes, it leaves a open empty excel program open, not completely shutting it down, i fixed this with the code under, but if i got a other workbook active, it asks that to save it or not, as it wants to close that as well.

I only want to close the current one.
I managed to get it to work, if i got 2 active, it closes perfect, but if i only got the current active, it leaves a empty book open.

Take a look at my code:

Sub ShutDown()
'Application.DisplayAlerts = False
With ThisWorkbook
'ThisWorkbook.Save
' .Saved = True
' .Close
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End With


End Sub


If i use these:

'Application.DisplayAlerts = False
With ThisWorkbook
'ThisWorkbook.Save
' .Saved = True
' .Close

it works, but leaves a empty one, but closes the current perfectly if i got multiple books open.

If i use:

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit

Then it closes the active book perfectly, leaving no books open, but if i got multiple open, it wants to close them as well.

Is there a way to make it so, if its the only active, it closes it, leaving no empty ones, and if i got multiple open, it only closes the current one?

Thanks
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,503
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You can do something like:

Code:
If application.workbooks.count = 1 then
    application.quit
else
thisworkbook.close
end if

It will need additional code if you have a personal macro workbook open.
 

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.
It worked perfectly! Thanks a ton :)

You can do something like:

Code:
If application.workbooks.count = 1 then
    application.quit
else
thisworkbook.close
end if

It will need additional code if you have a personal macro workbook open.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,992
Messages
5,526,109
Members
409,685
Latest member
Bellybb

This Week's Hot Topics

Top