Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Copying Named Ranges.
Written by Allen Wyatt (last updated January 3, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003
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 as that in the original 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, it would be best to use the macro approach.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2469) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Copying Named Ranges.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
When creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...
Discover MoreA great way to customize Excel is to add your macros to a toolbar. That way you can run them quickly and easily.
Discover MoreIf you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-12-08 05:54:55
David
I solved the problem by making sure that names are only copied from a specific tab and that the tab exists in the new workbook.
2020-12-08 04:52:08
David
The code in your example https://excel.tips.net/T002469_Copying_Named_Ranges.html generates an error (1004) in Excel 2019
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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