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.
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
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:
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.
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!
Want to know how much of a time difference there is between your machine and a different machine? This tip provides some ...
Discover MoreNeed a quick way to change the default drive and directory in a macro you are writing? Here's the commands to do it and a ...
Discover MoreExcel doesn't allow you to run a macro while editing the contents of a cell. The only solution is to get out of Edit ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments