Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 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.

Copying Worksheets in a Macro

Written by Allen Wyatt (last updated May 15, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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:

ActiveSheet.Copy

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:

Sheets("Sheet1").Copy

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:

    Sheets.Copy

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:

ActiveSheet.Copy After:=Sheets("Sheet7")

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:

ActiveSheet.Copy Before:=Sheets("Sheet7")

This results in the worksheet being placed before Sheet7 instead of after it.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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 Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Copying Worksheets in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Customizing Quick Access Toolbar Icons

The standard way to customize Excel is to add tools to the Quick Access Toolbar. The program provides only a limited ...

Discover More

Converting Footnotes to Endnotes

When you spend a lot of time creating footnotes, how can you convert all of them to endnotes without entering them all ...

Discover More

Working with Multiple Printers

If you have multiple printers accessible to your computer, you may need a way to quickly print your worksheet on a ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to ...

Discover More

Making a Macro Button Stay Put

Excel allows you to add buttons to your worksheet that can be used to trigger macros. If you don't want those buttons to ...

Discover More

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2021-12-30 13:26:59

Bruce Watson

If you want to insert a copy of a 'template' sheet for each week, add the following to the code:
NOTE: your workbook should have only ONE sheet (your 'template') before running this code. You will end up with 52 exact copies, each tab named for each week from your input starting date.

Sub YearWorkbook2()
Dim iWeek As Integer
Dim sht As Variant
Dim sTemp As String
Dim dSDate As Date
Dim n As Integer
On Error Resume Next

sTemp = InputBox("Date for the first worksheet:", "End of Week?")
dSDate = CDate(sTemp)

Application.ScreenUpdating = False

n = 52

If n >= 1 Then
For numtimes = 1 To n
ActiveSheet.Copy After:=ActiveWorkbook.Sheets(Worksheets.Count)
Next
End If

For Each sht In Worksheets
sht.Name = Format(dSDate, "dd-mmm-yyyy")
dSDate = dSDate + 7
Next sht
Application.ScreenUpdating = True
End Sub


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.