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)

6

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.

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

Increasing Row Height for Printing

You may have a need to increase the height of the rows in your worksheet to "spread out" the data when it is printed. This ...

Discover More

Formulas Don't Calculate as Formulas

Enter a formula (starting with an equal sign) and you may be surprised if Excel doesn't calculate the formula. Here's a good ...

Discover More

Creating an Index Entry

In order to create an index, you first need to create the entries that will be used to compile the index. Here's how you do ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Trimming Spaces from Strings

Need to get rid of extraneous spaces before or after the text in a string? VBA provides three different functions you can use ...

Discover More

Deleting Zero Values from a Data Table

Want to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can accomplish ...

Discover More

Using the Status Bar

When developing a macro, you may want to display on the status bar what the macro is doing. Here's how to use this important ...

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

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.