PDA

View Full Version : Close active workbook if multiple workbooks are open/ close excel if only one open



gringo287
08-09-2013, 04:19 PM
**Sorry Couldnt fit reasonable Explanation in title**

Hi,

Am i doing what i normally do and thinking too much in to this, or is it more tricky than it sounds..

I want to be able to close my workbook after my macro has done its job. How do i dictate, whether the whole application will be closed if only my workbook is open, or only close my workbook, if multiple workbooks are open??

SamT
08-09-2013, 10:31 PM
ThisWorkbook.Close Closes Excel only if that book is the only one open.

gringo287
08-10-2013, 01:23 AM
Thank SamT,

Am i mis-understanding my own question?? I tried that on the project I'm working on and it just closes the window and keeps the excel application open, but i put that down to other code clashing in some way. I've just set up a new workbook, to test out your suggestion and therefor :-


Sub Button1_Click()
ThisWorkbook.Close savechanges = True
End Sub

Is the only code and it still only closes the window and leaves excel open.

HaHoBe
08-10-2013, 03:13 AM
Hi,

maybe something like

Sub Test()
If Workbooks.Count > 1 Then
ThisWorkbook.Close
Else
ThisWorkbook.Saved = True
Application.Quit
End If
End Sub
but you would need to take care for any personal macrobook.

Ciao,
Holger

gringo287
08-10-2013, 06:05 AM
HaHoBe, you are my man of the day!! thank you.