Closing workbook

priyatoshsarkar

New Member
Joined
Oct 21, 2008
Messages
46
Hi,

I would like to know what will be the code for below requirement:

"Once the workbook with macro will open, all the open workbook will get colsed".

I'm trying this for a while but it's not working.

Please help.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi. Try this in the ThisWorkbook module

Code:
Private Sub Workbook_Open()
Dim wb As Workbook
For Each wb In Workbooks
    If wb.Name <> ThisWorkbook.Name Then wb.Close savechanges:=True
Next wb
End Sub
 
Upvote 0
Presuming you mean you want to close any other workbooks that are open, maybe:
Rich (BB code):
Option Explicit
    
Sub exa3()
Dim wb As Workbook
    
    For Each wb In Application.Workbooks
        If Not wb.FullName = ThisWorkbook.FullName Then
            wb.Close
        End If
    Next
End Sub
 
Upvote 0
After the Next statement,

Code:
ThisWorkbook.Close SaveChanges:=True ' or False
 
Upvote 0
I don't know what you mean.

When the code running in a workbook closes the workbook it's in, the code is no longer running.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,752
Members
452,940
Latest member
rootytrip

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