Macro Mondays – How To Write A Macro To Close All Of Your Workbooks At Once


vba-classes-logo
Hello Excellers time for some more #macromonday fun!

This week again I wanted to share with you some code that saved me a tonne of time, a really small piece of code, but with big benefits.  This Macro allows you to close all workbooks you have at once, that’s right no going into each of your workbooks activating it, closing it saving the changes – this takes little Macro takes good care of all of that for you.  This Macro is best stored in your Personal Macro Workbook.  Below are some other blog posts relating to Personal Macro Workbook you may find useful.

Macro Mondays – Create A Shortcut To Your Personal Macro Workbook

Macro Mondays – Why Is My Personal Macro Workbook Not Loading Automatically?

Macro Mondays – How To Delete Your Personal Macro Workbook

 

What Does The Macro Do?

This Macro uses to workbooks collection to loop through all open workbooks, saves them then closes them down.

How Does It Work?

MACRO MONDAYS CLOSE ALL WORKBOOKS AT ONCE1

FIRST. Open Visual Basic – by hitting ALT +F11 or Developer Tab – Right Click on personal.xlsb  in the Project Window. I have stored this macro in my Personal Macro Workbook, so it is available to use whenever I want to.

Step 1. First we need to declare a variable.  This ensures that Excel creates a memory container for it.  The variable wb as Workbook represents a Workbook object.  This ensures we can loop through all of the open workbooks.

Step 2. This part of the code simply loops through all of the open workbooks, saves them, then closes them.  The SaveChanges: = True ensure the workbook is saved before closing.

Step 3. Test Your Macro!.  Yes it really is a simple piece of code isn’t it?.

Want To Copy The Code?

[stextbox id=”grey”]

Sub CloseMyWorkbooks()
Dim wb As Workbook
For Each wb In Workbooks
wb.Close SaveChanges:=True
Next wb

End Sub

[/stextbox]

 

 


If you want to see all of my blog posts in the Macro Mondays Series you can find them all in the link below. Why not book mark it?, Yes it is updated EVERY Monday.

You can also download the corresponding example workbook that contains the Macro code by clicking on the download button below.

How To Excel At Excel – Macro Mondays Blog Posts.

[wpdm_package id=’10338′]

Learn Excel With Our Excel Online Courses

Barbara

Barbara is an experienced data analyst with over 20 years of experience. Author of the How To Excel At Excel Top 50 Excel tips Ebook, YouTuber and Excel Blogger.

Recent Posts