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)

7

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

Determining Picture Size in a Macro

When processing a document using a macro, you may need for your macro to figure out the sizes of the images in your ...

Discover More

Easily Deploying Customizations

When you create a whole set of customizations for Excel, you may want to share them with others in your office or ...

Discover More

Changing Axis Tick Marks

Create a chart in Excel, and you may find that the tick marks shown on the axes in the chart aren't to your liking. It is ...

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 (ribbon)

Running a Procedure when a Workbook is Opened

Ever want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.

Discover More

Disabling Shift Key Use when Opening a Workbook

Open up a workbook, and Excel normally runs the macros associated with that workbook. You can disable the automatic ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

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 five less than 7?

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.