closing multiple workbooks at once

Sam05

New Member
Joined
Feb 7, 2009
Messages
23
Hello,

I've been trying to get the right code to close multiple workbooks from inside a macro, using a FOR loop. I have say Book1.xls ... Book10.xls and I want to substitute the numeral in the following code by the i-th value but haven't been able to:

For i = 1 To 10
Windows("Book1.xls").Activate
ActiveWorkbook.Close
Next i

Please help with the code...
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Someone please help,

I'm opening the wkbks with:

For i = 1 To 10
Workbooks.Open Filename:="C:\My Documents\Book" & i & ".xls"
Next i

which works ok, but nothing similar to close them.

FYI, there are more xls files in the folder.
 
Upvote 0
It seems like you are opening all those files in one instance of excel. Why dont you just click on the red X at the top right corner of excel? This will close all the excel files opened in that instance.
 
Upvote 0
jindon,

tried your suggestion but doesn't do anything.... files are still open, no error messages or nothing.

Any idea?
 
Upvote 0
jindon,

tried your suggestion but doesn't do anything.... files are still open, no error messages or nothing.

Any idea?
No error ? even typo ?
Rich (BB code):
For i = 1 To 10
    Workbooks("Book" & i & ".xls").Close True
Next

or try
Rich (BB code):
Workbooks.Close
 
Upvote 0
well, I fixed the True since the begining, that's why "no errors".

Workbooks.Close does close ALL my workbooks; I want to keep open the one from where I execute the macro.

Almost there, what else can I try?
 
Upvote 0
Then try
Code:
Dim wb As Workbook
For Each wb In Workbooks
    If wb.Name <> ThisWorkbook.Name Then wb.Close True
Next
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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