Copy worksheets in active workbook to new workbooks
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 worksheets in active workbook to new workbooks
- List all open workbooks and corresponding sheets - VBA
1.1 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
1.2 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 Sub
1.3 Copy 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
1.3.1 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.Name
after this line
SHT.Copy After:=WBN.Sheets(WBN.Worksheets.Count)
in the macro above.
1.3.2 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.
Recommended reading
10 ways to reference Excel workbooks and sheets using VBA
2. List all open workbooks and corresponding sheets - VBA
In this post, I am going to demonstrate how to automatically create a new sheet in the current workbook and list all open workbooks and their sheets using a VBA macro.
The image above shows the new worksheet, it contains the names of the workbooks and corresponding worksheets I have currently open.
Macro code
'Name macro Sub ListWorkbooks() 'Declare variables and data types Dim wb As Workbook Dim ws As Worksheet Dim i As Single, j As Single 'Create a new worksheet and save to object ws Set ws = Sheets.Add 'Go through open workbooks For j = 1 To Workbooks.Count 'Save workbook name to cell A1 and downwards Range("A1").Cells(j, 1) = Workbooks(j).Name 'Iterate through worksheets in given workbook For i = 1 To Workbooks(j).Sheets.Count 'Save worksheet names to cell B1 and cells further right Range("A1").Cells(j, i + 1) = Workbooks(j).Sheets(i).Name 'Continue with next worksheet Next i 'Continue with next workbook Next j End Sub
How to copy the macro to your workbook
- Press Alt+F11 to open the VB Editor.
- Press with right mouse button on on your workbook in the Project Explorer.
- Press with left mouse button on "Insert" and then "Module".
- Copy macro code.
- Press with left mouse button on in code module window to see the input prompt.
- Paste code to code module.
- Return to Excel.
How to run the macro
- Go to "Developer" tab on the ribbon.
- Press with left mouse button on "Macros" button.
- Select "ListWorkbooks" macro.
- Press with left mouse button on "Run" button.
Macro category
Table of Contents Add values to a regular drop-down list programmatically How to insert a regular drop-down list Add values […]
What's on this page Auto resize columns as you type - VBA Press with left mouse button on a cell […]
This article demonstrates how to automatically enter data in cells if an adjacent cell is populated using VBA code. In […]
Excel categories
8 Responses to “Copy worksheets in active workbook to new workbooks”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
I used this macro to get me the list of all of the open workbooks. I wanted to manipulate it, but I am having trouble and maybe you would have a brilliant answer...
In each of these open workbooks there is a sheet named "Subscriber" and I want to copy the data from each of these sheets and place it into the sheet beneath the list that was created from your macro alone. Any suggestions?
thank you.
I found the code of "Copy sheets in every open workbook to a master workbook" very helpful. Thanks
Chris Fourie
You are welcome!
Hi Oscar! This is great and it worked for me but I could really use some help to add something to this. I want to copy each sheet in my active workbook to new workbooks just like you do up at the top but then I need to SAVE each new workbook that I've created into a folder and give each new workbook the name of whatever the tab/sheet name was in the original workbook.
Can you help with that do you think?
I need an excel macro to copy all worksheets in different workbooks on the shared drive that have "open" in cells L9. I would like to copy each worksheet to an individual tab and saved to a new master workbook.
Can you assist me asap?
Thanks.
Kathleen
Thanks Oscar,
The code is working cool! when one excel environment/session is open, how do we change the code to list out the workbooks from all sessions.
Regards,
Manish
Hi Oscar
May I know if I would like to copy data from a sheet to another sheet, then Split data and using subtotals in Excel
My data is like this (for example):
No. Amt State
1 24 AA
2 35 BB
3 46 CC
4 34 AA
5 78 BB
6 99 CC
I want to split the data by state and I want to get 3 data sets like below in one sheet:
Data set 1
No. Amt State
1 24 AA
4 34 AA
Data set 2
No. Amt State
2 35 BB
5 78 BB
Data set 3
No. Amt State
3 46 CC
6 99 CC
Thank you very much!
Best regards
Grace
Hello,
I am having troubling coming up with a VBA code in Excel to rename existing tabs with dates that are listed in cells
Thanks, Vickie