VBA - close all workbooks in macro.

whippler

New Member
Joined
Dec 13, 2002
Messages
15
I have a template that pulls in data from 37 other workbooks each week and calculates info with charts. My macro does not close all of the 37 workbooks automatically b/c each book prompts for "save changes? etc". Its not a big deal to click no 37 times real quick, but I would like my macro to close them all for me. Is there an easy way to write this in a loop so I dont have to hard code this for all 37 book file names?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

oldham94l

Board Regular
Joined
Jul 14, 2002
Messages
136
just put
activeworkbook.close savechanges:=true

after each file used
i suppose thats the end of the loop
 

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
Just to expand a little on the above.

activeworkbook.close savechanges:=true
>>This will close the workbook and save any changes.

activeworkbook.close savechanges:=false
>>This will close the workbook without saving any changes.

If you need any help in implementing the above, post your routine to the board so that we can see exactly what you are doing.
 

Mudface

MrExcel MVP
Joined
Feb 18, 2002
Messages
3,339
As far as a loop goes, try the code below which will avoid you having to activate each workbook then close it with ActiveWorkbook:-<pre>
Public Sub CloseAllWorkbooks()
Dim wb As Workbook

For Each wb In Workbooks
wb.Close False ' Or True if you want changes saved
Next wb

End Sub</pre>
This message was edited by Mudface on 2002-12-15 14:23
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,960
Messages
5,767,331
Members
425,404
Latest member
Bairkus

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
Top