Close Excel Window

Pestomania

Active Member
Joined
May 30, 2018
Messages
332
Office Version
  1. 365
Platform
  1. Windows
How can I accomplish closing an Excel workbook and it's except window completely. But I don't want it to close the entire application unless it is the only one left open!

So in a lot of cases, I have a lot of Excel books open. So using the application (.) Quit command cannot be used because it closes everything.

The problem is when I have only one workbook open When I use the workbook(.)close command, it closes the book. But leaves open a window.

Not sure what to do for it to understand the difference!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Re: Close Excel Windoe

It sounds like you are talking about VBA. Maybe this:
Code:
If Workbooks.Count = 1 Then 
   Application.Quit
Else
   wb.Close
End If
where wb is the workbook you want to close
 
Upvote 0
6StringJazzer

Is it possible to close without saving using VBA?

The file I have has a VBA that is set to run from a button click. Once the VBA has finished I would like the workbook to close completely without saving.
The VBA saves the info as a PDF hence.
 
Last edited:
Upvote 0
6StringJazzer

Is it possible to close without saving using VBA?

The file I have has a VBA that is set to run from a button click. Once the VBA has finished I would like the workbook to close completely without saving.
The VBA saves the info as a PDF hence.
Code:
wb.Close SaveChanges:=False
 
Upvote 0
Re: Close Excel Windoe

Hello, so I will type in my code below. It is closing active workbook but not the application. I need it to close application whenever there is 1 workbook open.

Code:
Sub doyouwanttoclose ()

Dim answer as string

Answer = MsgBox ("Do you want to close", vbyesno)

If answer = vbyes then

If workbooks.count = 1 then
   Application.Quit
Else
   ActiveWindow.Close
End if

If answer = vbno then
  Exit sub
End if
End if

End sub
 
Last edited:
Upvote 0
Re: Close Excel Windoe

You probably have PERSONAL.xlsb open too, which is hidden by default. Try this, which I have tested. If you have an old version of Excel your PERSONAL file may have a different name. What version do you have?

Code:
Sub doyouwanttoclose()


   Dim answer As String
   
   answer = MsgBox("Do you want to close", vbYesNo)
   
   If answer = vbYes Then
   
      If Workbooks.Count = 1[COLOR=#ff0000] Or _[/COLOR]
[COLOR=#ff0000]         (Workbooks.Count = 2 And Workbooks(1).Name = "PERSONAL.xlsb")[/COLOR] Then
         Application.Quit
      Else
         ActiveWindow.Close
      End If
      
      If answer = vbNo Then
        Exit Sub
      End If
      
   End If


End Sub
 
Upvote 0
Re: Close Excel Windoe

You probably have PERSONAL.xlsb open too, which is hidden by default. Try this, which I have tested. If you have an old version of Excel your PERSONAL file may have a different name. What version do you have?

Code:
Sub doyouwanttoclose()


   Dim answer As String
   
   answer = MsgBox("Do you want to close", vbYesNo)
   
   If answer = vbYes Then
   
      If Workbooks.Count = 1[COLOR=#ff0000] Or _[/COLOR]
[COLOR=#ff0000]         (Workbooks.Count = 2 And Workbooks(1).Name = "PERSONAL.xlsb")[/COLOR] Then
         Application.Quit
      Else
         ActiveWindow.Close
      End If
      
      If answer = vbNo Then
        Exit Sub
      End If
      
   End If


End Sub

This worked perfectly!! Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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