Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Copying Worksheets in a Macro.

Copying Worksheets in a Macro

Written by Allen Wyatt (last updated March 8, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


1

When organizing data in workbooks, it is not uncommon to copy worksheets from one workbook to another. Indeed, the Edit | Move or Copy Sheet command 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, instead, 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 (2784) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Resetting a Single Shortcut Key

Need to get rid of a keyboard shortcut key you previously defined? It's easy to do if you follow these steps.

Discover More

Determining the Least Common Multiple

Need to figure out the least common multiple of a range of values? It is a snap when you use the LCM function, described ...

Discover More

Turning Off Worksheet Tabs

Look at the bottom of a worksheet and chances are you will see tabs for all the worksheets in the current workbook. Want ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...

Discover More

Hiding Entries in an InputBox

Requiring users to input a password in Excel increases the security of the worksheet and can prevent someone from running ...

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?

2017-11-30 19:54:05

Larry

These are all good but there is an option that could be added if it can be done.

Worksheet(1) is named "Sun"

Copy entire worksheet "Sun" to SAME workbook and rename to "Sun Is Hot".

I understand how to copy the worksheet to desired location, or new workbook.
But the trouble is if you copy "Sun", excel will rename it to "Sun (1)", or "Sun (2)" if "Sun (1) exists. Then how could you rename that most recent worksheet you copied?

Thanks in advanced.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.