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

Copying Named Ranges

by Allen Wyatt
(last updated November 7, 2015)

9

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, in the target 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, you'd 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 (8811) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel 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

Sorting by Headings

Headings are a great way to organize your document. If, after getting your headings in place, you want to sort by those ...

Discover More

Sending Drawing Objects to the Back or Front

Drawing objects can be placed in a document in such a way that they overlap with each other. If you want to arrange those ...

Discover More

Self-Deleting Macros

Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...

Discover More

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!

More ExcelTips (ribbon)

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...

Discover More

Inserting the Current Time with Seconds

If you need to insert the current time, with seconds, then you'll need the macro discussed in this tip. It's easy to use ...

Discover More

Forcing Manual Calculation For a Workbook

If you have a large, complex workbook, you may want to make sure that it is always calculated manually instead of ...

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}] 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 seven less than 8?

2018-09-08 05:51:09

Willy Vanhaelen

If your Book2.xlsx doesn't contain the sheets with the same name as your source sheet (active sheet), the macro will crash.


2018-09-07 17:22:23

Nathan

I had the same issue as Cameron Wild. When I click in the line, it looks like it is trying to pull in a function "xlfn.AVERAGEIFS" which comes back with a refers to of "=#NAME?" error which is killing the program. Why is it pulling in Excel Functions instead of just defined names? Does anyone know how to only pull in defined names? Thanks for any help you can give.


2018-06-04 06:25:37

Cameron Wild

Doesn't work in Excel 2016 :(

Says syntax of the name is incorrect - highlights this line:

Target.Names.Add Name:=n.Name, RefersTo:=n.value


2017-02-13 12:04:26

allen@sharonparq.com

Andrew: The "subscript out of range" error means that you don't have a workbook open that is named Book2.xlsx.

-Allen


2017-02-13 12:01:30

Andrew

Failing on the line

Set Target = Workbooks("Book2.xlsx")

with

Runtime Error '9': Subscript out of range.

Just to validate I was using the right destibnation file, I copied the full posix compliant path to Workbooks(""). I also opened up the target file and use the "immediate window" and ran


? activeworkbook.fullname

and got
Macintosh HD:Users:my_userid:Documents:Oly:OlyJunk.xlsm

and pasted that into the Workbooks() function.. Same result.

What could be the problem? The target has the exact-same named worksheets and data ranges as the source.


2016-05-16 10:23:29

Dontbflat88

Keeping this page with the macro open, open Excel and the file you are working.
Click the Developer tab
Click Macros. This will open up VBA.
Type "CopyNames" (without the quotes) in the Macro Name box.
Click Create
Highlight and copy the macro in the web page
Go back to the macro and Select All(Ctrl + a) and then paste the macro (Ctrl + v).
You should then be able to run the macro.


2016-05-13 10:26:45

Deven

Hi,

what is the syntax for the ("Book2.xlsx").

Do I need to enter the full root of the file, e.g. user/desktop/book2.xlsx;

or just the open workbook ("Book2.xlsx")


2016-04-29 15:42:36

Matthew

I'm having the same problem as Ahmedin. any help would be appreciated. Thanks


2016-01-06 18:21:42

Ahmedin

Please how do i use this code, what are the procedures to implement so as to make it work.


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.