Author: Oscar Cronquist Article last updated on October 11, 2019
This article demonstrates several VBA macros, they will save you time if you have lots of worksheets. The first macro copies worksheets from the active workbook to new workbooks, each worksheet becomes a new workbook.
The second VBA macro copies selected worksheets to new workbooks, each worksheet becomes a new workbook. The third VBA macro copies worksheets from all open workbooks and collects them all to a single master workbook.
The last macro names the created worksheets based on the original workbook name.
Table of Contents
- Copy each sheet in active workbook to new workbooks
- Copy selected sheets to new workbooks
- Copy sheets in every open workbook to a master workbook
- Add workbook name to sheets
Copy each sheet in active workbook to new workbooks
The following macro copies each sheet in the current workbook to new workbooks.
VBA code
'Name macro Sub CopySheetsToNewWorkbooks() 'Dimension variable and declare data types Dim SHT As Worksheet 'Iterate through worksheets in active workbook For Each SHT In ActiveWorkbook.Worksheets 'Copy worksheet to a new workbook SHT.Copy 'Continue with next worksheet in acteive workbook Next 'Stop macro End Sub
Copy selected sheets to new workbooks
If a sheet contains a list (excel 2003) or a table (excel 2007) you can't only use the SelectedSheets property to copy selected sheets. This article by Ron de Bruin explains a workaround: Copying Worksheets with a List or Table
- Press and hold Ctrl and select multiple sheets in your workbook
- Run CopySelectedSheetsToNewWorkbooks macro
VBA code
'Name macro Sub CopySelectedSheetsToNewWorkbooks() 'Dimension variable and declare data types Dim AW As Window 'The SET statement saves the active window as an object reference to variable AW Set AW = ActiveWindow 'Iterate through selected worksheets based on active window For Each SHT In AW.SelectedSheets 'The NewWindow method creates a new window based on the active window using an object reference saved to variable TempWindow Set TempWindow = AW.NewWindow 'Copy worksheet to a new workbook SHT.Copy 'Close the newly created window TempWindow.Close 'Continue with next worksheet Next 'Stop macro End SubCopy sheets in every open workbook to a master workbook
This macro copies all sheets in all open workbooks to a singlemaster workbook.
'Name macro Sub CopySheetsToMasterWorkbook() 'Dimension variables and declare data types Dim WBN As Workbook, WB As Workbook Dim SHT As Worksheet 'Create a new workbook and save an object reference to variable WBN Set WBN = Workbooks.Add 'Iterate through all open workbooks For Each WB In Application.Workbooks 'Check if workbook name of object variable WB is not equal to name of object variable WBN If WB.Name <> WBN.Name Then 'Go through all worksheets in object WB For Each SHT In WB.Worksheets 'Copy worksheet to workbook WBN and place after the last worksheet SHT.Copy After:=WBN.Sheets(WBN.Worksheets.Count) 'Continue with next worksheet Next SHT 'End of If statement End If 'Continue with next workbook Next WB 'Disable Alerts Application.DisplayAlerts = False 'Delete sheet1, sheet2 and sheet3 in the new workbook WBN WBN.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete 'Enable Alerts WBN.Application.DisplayAlerts = True 'End macro End Sub
Add workbook name to sheets
Perhaps you want to know where each sheet in the master workbook came from? The following line adds the original workbook name to the sheet name.
Copy and paste this line
WBN.Sheets(WBN.Worksheets.Count).Name = Left(WB.Name, 30 - Len(SHT.Name)) & "-" & SHT.Nameafter this line
SHT.Copy After:=WBN.Sheets(WBN.Worksheets.Count)in the macro above.
Where to put the macros?
- Press Alt+ F11 to open the Visual Basic Editor.
- Select your workbook in the Project Explorer window.
- Press with left mouse button on "Insert" on the menu.
- Press with left mouse button on "Module" to create a module.
- Paste code to module.
- Return to Excel.
Note, make sure you save the workbook with file extension *.xlsm so the code stays with your workbook.
Recommended reading
10 ways to reference Excel workbooks and sheets using VBA
Apply drop-down lists dynamically
This article demonstrates how to automatically create drop-down lists if adjacent data grows, there are two methods explained here. The […]
Auto resize columns as you type
Excel does not resize columns as you type by default as the image above demonstrates. You can easily resize all […]
Automate data entry [VBA]
This article demonstrates how to automatically enter data in cells if an adjacent cell is populated using VBA code. In […]
Basic data entry [VBA]
In this small tutorial, I am going to show you how to create basic data entry with a small amount […]
Calendar with scheduling [vba]
Here is my contribution to all excel calendars out there. My calendar is created in Excel 2007 and uses both […]
Consolidate sheets [vba]
Question: I have multiple worksheets in a workbook. Each worksheets is project specific. Each worksheet contains almost identical format. The […]
Copy a dynamic cell range [VBA]
In this blog post, I will demonstrate some VBA copying techniques that may be useful if you don't know the […]
Create a Print button [VBA]
This article describes how to create a button and place it on an Excel worksheet then assign a macro to […]
Edit invoice data [VBA]
In a previos post:Excel vba: Save invoice data we added/copied data between sheets. This post describes how to overwrite existing […]
Excel calendar [VBA]
This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]
Hide specific columns programmatically
This article describes a macro that hides specific columns automatically based on values in two given cells. I am also […]
Hide specific worksheets programmatically
This article demonstrates techniques to hide and unhide worksheets programmatically. The image above shows the Excel window and the worksheet […]
How to quickly select blank cells
In this smaller example, column D (Category) has empty cells, shown in the picture above. If your column contains thousands of […]
How to use DIALOG BOXES
A dialog box is an excellent alternative to a userform, they are built-in to VBA and can save you time […]
How to use the Scroll Bar
This article demonstrates how to insert and use a scroll bar (Form Control) in Excel. It allows the user to […]
Identify missing numbers in a column
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
Locate a shape in a workbook
This article demonstrates how to locate a shape in Excel programmatically based on the value stored in the shape. The […]
Move a shape [VBA]
This article demonstrates how to move a shape, a black arrow in this case, however, you can use whatever shape […]
Normalize data [VBA]
To be able to use a Pivot Table the source data you have must be arranged in way that a […]
Open Excel files in a folder [VBA]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
Save invoice data [VBA]
This article demonstrates a macro that copies
values between sheets. I am using the invoice template workbook. This macro copies […]
Search two related tables [VBA]
This article demonstrates a macro
that automatically applies a filter to an Excel defined Table based on the result from […]
Select and view invoice [VBA]
This post demonstrates how to view saved invoices based on the
invoice number using a userform. The userform appears when the […]
Toggle a macro on/off using a button
This article demonstrates how the user can run a macro by press with left mouse button oning on a button, […]
Working with COMBO BOXES [Form Controls]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
Working with FILES
What's on this page Copy a file Copy and rename a file Rename a file List files in a folder […]
Working with TEXT BOXES [Form Controls]
There are two different kinds of text boxes, Form controls and ActiveX Controls. Form controls can only be used on […]
Latest updated articles.
More than 300 Excel functions with detailed information including syntax, arguments, return values, and examples for most of the functions used in Excel formulas.
More than 1300 formulas organized in subcategories.
Excel Tables simplifies your work with data, adding or removing data, filtering, totals, sorting, enhance readability using cell formatting, cell references, formulas, and more.
Allows you to filter data based on selected value , a given text, or other criteria. It also lets you filter existing data or move filtered values to a new location.
Lets you control what a user can type into a cell. It allows you to specifiy conditions and show a custom message if entered data is not valid.
Lets the user work more efficiently by showing a list that the user can select a value from. This lets you control what is shown in the list and is faster than typing into a cell.
Lets you name one or more cells, this makes it easier to find cells using the Name box, read and understand formulas containing names instead of cell references.
The Excel Solver is a free add-in that uses objective cells, constraints based on formulas on a worksheet to perform what-if analysis and other decision problems like permutations and combinations.
An Excel feature that lets you visualize data in a graph.
Format cells or cell values based a condition or criteria, there a multiple built-in Conditional Formatting tools you can use or use a custom-made conditional formatting formula.
Lets you quickly summarize vast amounts of data in a very user-friendly way. This powerful Excel feature lets you then analyze, organize and categorize important data efficiently.
VBA stands for Visual Basic for Applications and is a computer programming language developed by Microsoft, it allows you to automate time-consuming tasks and create custom functions.
A program or subroutine built in VBA that anyone can create. Use the macro-recorder to quickly create your own VBA macros.
UDF stands for User Defined Functions and is custom built functions anyone can create.
A list of all published articles.