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?
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,328
Messages
5,486,181
Members
407,536
Latest member
farrukhikram

This Week's Hot Topics

Top