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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8811) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Copying Named Ranges.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Disabling function keys is rather easy to do when you rely on the OnKey method in a macro. This tip looks at how you can ...
Discover MoreRandom values are often needed when working with certain types of data. When you need to generate a random value in a ...
Discover MoreIf your macro processes information on a number of worksheets, chances are good that you need your macro to figure out ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-09-08 05:51:09
Willy Vanhaelen
If your Book2.xlsx doesn't contain the sheets with the same name as your source sheet (active sheet), the macro will crash.
2018-09-07 17:22:23
Nathan
I had the same issue as Cameron Wild. When I click in the line, it looks like it is trying to pull in a function "xlfn.AVERAGEIFS" which comes back with a refers to of "=#NAME?" error which is killing the program. Why is it pulling in Excel Functions instead of just defined names? Does anyone know how to only pull in defined names? Thanks for any help you can give.
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.
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 © 2021 Sharon Parq Associates, Inc.
Comments