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

Written by Allen Wyatt (last updated March 5, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


3

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
        If n.Visible Then
            Target.Names.Add Name:=n.Name, RefersTo:=n.Value
        End If
    Next
End Sub

This macro assumes quite a bit, all the name of simplicity. First, it assumes that you have a target workbook open, and the name of that workbook is Book2.xlsx. If you don't, then the macro will crash and burn when you run it. You can, if desired, change the name of the target workbook to match your needs, or you can modify the macro so that it adds and uses a new workbook.

Second, named ranges include, by default, 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.

Note that the majority of the work in the macro is done in the For Each loop that steps through all the defined names. If the named range is visible (meaning, it isn't a hidden system-used range name), then the maco 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).

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, 2016, 2019, Excel in Microsoft 365, and 2021. 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

Making Macros Readily Available

Want to get at your macros quickly? One way is to create a Macros menu to make your most common macros immediately available.

Discover More

Best Quality for High Resolution Graphics

You want your documents to look as good as they can. If those documents include graphics, then you also need to make sure ...

Discover More

Rounding Up to a Value Ending in 9

Need to round values up to the next value that ends in 9? There are a number of ways you can accomplish the task through ...

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

Understanding Variables in VBA Macros

You can create and use all sorts of variables in your macros. This tip examines all the different data types you can specify.

Discover More

Automating Copying Macros

You can manually copy macros from one workbook to another, but what if you want to automate the copying process? Here's ...

Discover More

Selecting Columns in VBA when Cells are Merged

If you have a macro that selects different columns in a worksheet while processing information, you may get some ...

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?

2024-01-28 19:12:22

Tomek

Wouldn't it be much easier to save the workbook under new name, and then delete the content that is no longer needed? That way all the named ranges would be retained. When cleaning up, just do not delete the sheets, as this may cause the definitions of some ranges to become invalid..

One thing that such approach will not handle is when you want to copy the named ranges to a workbook that already has data, including its own named ranges. In such case you may use one of the approaches from this tip, but you may consider copying from the workbook that has less named ranges to the one with more..


2024-01-26 11:23:23

J. Woolley

For information about the named ranges in your workbook, My Excel Toolbox includes the following dynamic array function to list defined names with workbook, worksheet, or any scope, including names that are normally hidden:
    =ListNames([Scope], [SkipHidden], [SkipHeader])
The list includes the following columns: Scope, Name, Visible, Refers To, Value, Comment. When using pre-2021 versions of Excel without support for dynamic arrays, review the PDF file UseSpillArray.pdf.
My Excel Toolbox's NamesInFormulas macro lists hyperlinks to formula cells referencing each visible defined name (named range) in the active workbook. (Hidden names are ignored.) The cell's formula will be included in a comment attached to its hyperlink. The following details are also provided for each name: Scope, Name, Refers To, Value, and Comment. Results are recorded in the active workbook's 'NamesIn...' worksheet.
See https://sites.google.com/view/MyExcelToolbox


2024-01-22 13:18:04

Gene S

I'm using Excel 365. I tried a few macros on the internet, but they didn't work. Yours worked flawlessly on the first try. I had about 80 named ranges to copy. They were long and complex, so it probably would have taken me at about 2 hours to copy them if I didn't make too many mistakes. Thank you.


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.