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.
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!
There are a variety of ways that you might want to count the cells in your worksheet. One way is to figure out how many ...
Discover MoreWant to know how much of a time difference there is between your machine and a different machine? This tip provides some ...
Discover MoreWhen writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2023 Sharon Parq Associates, Inc.
Comments