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 Named Ranges.

Copying Named Ranges

Written by Allen Wyatt (last updated January 3, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Graeme has a workbook that has a large number (120+) named ranges defined within it. He would like to copy the range names and definitions to a different workbook. Thus, after copying, the range named MyRange1, which refers to the range C7:H22 in the original workbook, will exist in the target workbook and refer to the same range as that in the original workbook. Nothing else should be copied from the original workbook to the target—just the range names and definitions.

The easiest way to do this is with a macro that steps through each of your defined names and copies the name definition to the target workbook. Here's an example:

Sub CopyNames()
    Dim Source As Workbook
    Dim Target As Workbook
    Dim n As Name

    Set Source = ActiveWorkbook
    Set Target = Workbooks("Book2.xlsx")

    For Each n In Source.Names
        Target.Names.Add Name:=n.Name, RefersTo:=n.Value
    Next
End Sub

Note that the majority of the work in the macro is done in the For Each loop that steps through all the defined names. It creates the name in the target workbook and gives it the same assignment as it had in the source workbook (contained in the Value property).

It should be noted that, by default, named ranges include the name of the worksheet in the Value property. If the source workbook has a named range that refers to, say, Sheet4 and there is no Sheet4 in the target workbook, then the addition of the name fails. The macro doesn't generate an error; it simply doesn't create the new named range. The solution is to either (a) make sure that the target workbook contains the same sheet names as the source workbook or (b) modify the macro so that it recognizes that there are missing sheets and takes whatever action is appropriate.

If you prefer to not create a macro, then the easiest method may be to copy your worksheets from the source workbook to a target workbook. Excel generally copies the named ranges along with the worksheets. The only time this would not be a satisfactory approach is if the target workbook already has worksheets with the same names as those worksheets you might want to copy. In that case, it would be best to use the macro approach.

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 (2469) 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 Named Ranges.

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

Ordering Search and Replace

The wildcard searching available in Word is very powerful. Here's how you can use ordering in your search efforts to make ...

Discover More

Inserting Different Dashes

Excel supports several types of dashes. This tip describes those different types and explains how to enter them in a cell.

Discover More

Specifying Font Color in a Formula

Do you need to change text color based on the result of a formula? This tip provides a couple of ways you can accomplish ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Deleting a File in a Macro

Macros give you a great deal of control over creating, finding, renaming, and deleting files. This tip focuses on this ...

Discover More

Mouse Click Event in VBA

Need to know if a particular cell is clicked with the mouse? Excel has no particular event handler for clicking in this ...

Discover More

Changing the Default Drive

Do you have a macro that need to read and write files? If so, then there is a good chance you need to specify the default ...

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 2 + 2?

2020-12-08 05:54:55

David

I solved the problem by making sure that names are only copied from a specific tab and that the tab exists in the new workbook.


2020-12-08 04:52:08

David

The code in your example https://excel.tips.net/T002469_Copying_Named_Ranges.html generates an error (1004) in Excel 2019


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.