Close the active workbook and in case it is the only workbook quit Excel. But code does not work properly.

BVOPP

Board Regular
Joined
Feb 9, 2015
Messages
50
This is the code i used:
VBA Code:
Sub Close_Active_Workbook(Save_Changes As Boolean)
    Dim book As Workbook
    Dim number_of_openbooks As Integer
    
    number_of_openbooks = 0
    For Each book In Workbooks
        number_of_openbooks = number_of_openbooks + 1
    Next book
    
    If number_of_openbooks = 1 Then
        ActiveWorkbook.Close SaveChanges:=Save_Changes
        'close excel as well
        Application.Quit
    Else
        ActiveWorkbook.Close SaveChanges:=Save_Changes
    End If

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Probably you have some hidden open workbook...
Try modifying this portion:
VBA Code:
    number_of_openbooks = 0
    For Each book In Workbooks
        Debug.Print book.Name, Windows(book.Name).Visible
        If Windows(book.Name).Visible Then number_of_openbooks = number_of_openbooks + 1
    Next book
'your next code
 
Upvote 0
Thanks tried that one but same result:
1650796782049.png
 
Upvote 0
Probably when you close the activeworkbook the macro get badly truncated.
Try a different approach, IE saving the file (rather then Closing it) then quitting excel
VBA Code:
'previous code
    Next book
    
    If number_of_openbooks = 1 Then
        If Save_Changes Then
            ActiveWorkbook.Save
        End If
        Application.DisplayAlerts = False
        'close excel as well
        Application.Quit
    Else
        ActiveWorkbook.Close SaveChanges:=Save_Changes
    End If

Bye
 
Upvote 0
Solution
VBA Code:
If number_of_openbooks = 1 Then
If Save_Changes Then
ActiveWorkbook.Save
End If
Application.DisplayAlerts = False
'close excel as well
Application.Quit
Else
ActiveWorkbook.Close SaveChanges:=Save_Changes
End If
GREAT This is it ! Thanks a lot!
 
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,673
Members
449,178
Latest member
Emilou

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