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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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