vba code to close all workbooks except active workbook which has macro

aman2059

Board Regular
Joined
Jan 17, 2016
Messages
75
Hello,

Trying to figure out the vba code which can help me to close all opened workbooks except the active one which has macro. Below is my code. But it does not close any workbook.

Please help. Thank you in advance.

Code:
Sub CloseOtherWorkbook()

Dim WB As Workbook

Windows("Master file.xlsm").Activate

Application.ScreenUpdating = False
For Each WB In Application.Workbooks
    If Not (WB Is Application.ActiveWorkbook) Then
        WB.Close
    End If
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi Andrew,

Thank you so much. It worked. :)

One more thing I wanted to add on this. Currently, it gives prompt message to save or not to save before closing workbooks. I wanted all workbooks to save by itself and then close.

Could you please help.

Code:
Sub CloseOtherWorkbook()
'this is to close all workbooks - let's figure how to do it

Dim WB As Workbook

Windows("Master file.xlsm").Activate

Application.ScreenUpdating = False
For Each WB In Application.Workbooks
    If WB.Name <> ThisWorkbook.Name Then
        WB.Close
    End If
Next
Application.ScreenUpdating = True
End Sub


Try:

If WB.Name <> ThisWorkbook.Name Then
 
Upvote 0
Hi Andrew,

Thank you so much. It worked. :)

One more thing I wanted to add on this. Currently, it gives prompt message to save or not to save before closing workbooks. I wanted all workbooks to save by itself and then close.

Could you please help.

Code:
Sub CloseOtherWorkbook()
'this is to close all workbooks - let's figure how to do it

Dim WB As Workbook

Windows("Master file.xlsm").Activate

Application.ScreenUpdating = False
For Each WB In Application.Workbooks
    If WB.Name <> ThisWorkbook.Name Then
        WB.Close
    End If
Next
Application.ScreenUpdating = True
End Sub


Try:

If WB.Name <> ThisWorkbook.Name Then
 
Upvote 0
Code:
Sub CloseOtherWorkbook()
'this is to close all workbooks - let's figure how to do it

Dim WB As Workbook

Windows("Master file.xlsm").Activate
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For Each WB In Application.Workbooks
    If WB.Name <> ThisWorkbook.Name Then
        WB.Close savechange:=True
    End If
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
Last edited:
Upvote 0
Code:
Sub CloseOtherWorkbook()
'this is to close all workbooks - let's figure how to do it

Dim WB As Workbook

Windows("Master file.xlsm").Activate
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For Each WB In Application.Workbooks
    If WB.Name <> ThisWorkbook.Name Then
        WB.Close savechange:=True
    End If
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Hi There,

I was trying this code as well, however my script does not recognize this line:

WB.Close savechange:=True

I changed it to read: WB.Close savechange:=False, as I do not want it to save.

Anything I am doing wrong?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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