Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Copying Worksheets in a Macro.
by Allen Wyatt
(last updated October 10, 2015)
When organizing data in workbooks, it is not uncommon to copy worksheets from one workbook to another. Indeed, the Move or Copy Sheet command (visible when you right-click on a worksheet tab) is one that I use quite often, and I'd be willing to bet that others use it just as often.
How, then, is one to copy worksheets within a macro? The answer is to use the Copy method with an individual worksheet or group of worksheets. For instance, the following macro code will copy the currently selected worksheet to a new workbook:
That's it; a single line is all that is necessary to copy the worksheet to a new, unnamed workbook. After executing the line, the new workbook is selected and you can save it using code similar to the following. The first line in the code saves the workbook, and the second closes it.
ActiveWorkbook.SaveAs Filename:="MyNewFile.xlsm", _ FileFormat:=xlOpenXMLWorkbookMacroEnabled ActiveWindow.Close
If you want to copy a specific sheet to another workbook, you do it by specifying the name of the sheet you want to copy, instead of using the ActiveSheet object:
This example copies the worksheet named Sheet1, from the Sheets collection, to a new workbook. You can then save the new workbook, as already discussed.
The Copy method, when used with worksheets, is not limited to copying a single sheet at a time. If you have a group of sheets selected, you can still use a single command line to copy all of them to a new workbook. That is what is done in this macro:
Sub CopyWorkbook() Dim sCopyName As String sCopyName = "My New Workbook.xlsm" SelectedSheets.Copy ActiveWorkbook.SaveAs Filename:=sCopyName, _ FileFormat:=xlOpenXMLWorkbookMacroEnabled End Sub
Note the use of the Copy command. The macro will work whether you have one worksheet selected or fifty; it doesn't matter. If you wanted to copy all of the worksheets from one workbook to another, all you need to do is make a single change in the macro, to the line where the Copy method is invoked:
This copies the entire Sheets collection, which consists of all the worksheets in the workbook.
It should be noted that the Copy method isn't just for copying worksheets to a new workbook; it can also be used to copy worksheets within the same workbook. The only thing you need to do is specify where in the current workbook you want to make the copy:
This code line copies the active worksheet into the same workbook so that it appears after the worksheet named Sheet7. If it is more appropriate for your needs, you could instead specify the worksheet before which the copy should be placed:
This results in the worksheet being placed before Sheet7 instead of after it.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11856) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Copying Worksheets in a Macro.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Got a macro that you need to run on each of a number of workbooks? Excel provides a number of ways to go about this task, ...Discover More
When developing macros, you can create subroutines. This is a great way to reuse common code and make your programming ...Discover More
VBA provides a few different ways you can search for information within strings. This tip looks at the most efficient ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.