Forum Discussion
Duplicated named ranges on sheet copy
Hello all,
I have spent a couple of days building an excel workbook that is used by technical staff to calculate manufacturing/costing data. The book contains a master data sheet with a number of tables and a number of named ranges (global in scope) from columns in the tables. There is a master calculation sheet that users would copy/rename and then edit. Data in the master data sheet tables and items in the named ranges are then used in (auto-populated) formulas to calculate outputs.
Everything works great! except..............
Every time the master calculation sheet (or a sheet previously created from a copy) is copied, Excel duplicates the named ranges (scoped to the new sheet).
If I could simply stop Excel creating new named ranges all would be well!!
I have done extensive reading, and it seems this is normal behaviour?
I have seen solutions including manually deleting the new named ranges, to using vba to copy a sheet and then remove all sheet scoped named ranges, but everything seems a 'hack'.
Is there no way for me to stop Excel creating these locally scoped ranges at each sheet copy?
Thanks
- So solving this was relatively simple; rather than copying the sheet I copied the usedrange.
formats/formulas/tables etc all copy corretly but excel doesnt copy the named ranges.
Hopefully this is useful to someone else, as it seems to be a common query
- ITTom365Brass ContributorSo solving this was relatively simple; rather than copying the sheet I copied the usedrange.
formats/formulas/tables etc all copy corretly but excel doesnt copy the named ranges.
Hopefully this is useful to someone else, as it seems to be a common query- ecovonreinIron ContributorHow is not doing a thing that is easily within Excel's design envelope a "best response"? I revive this thread because I was literally about to post the same. I have the identical setup - a clean master Worksheet gets copied in order for the copy to be instantiated/used in production.
So question once more to the Excel boffins here: Why is this happening?
I cannot find a rhyme or reason. Many of my affected names are global Lambdas. But also a couple of regular (albeit external/linked) ranges are affected.
I will probably have to write a macro to support copying the clean module. Since Excel refuses point-blank to copy my Worksheet across Workbooks, I am thinking I should write a macro that simply takes a dump (snapshot) of all names, then deletes all names, copies the Worksheet and then reinstates all the names in new Worksheet.
It is simply frustrating how many such blooming VBA macros I need to write to work around all manner of Excel bugs. (Don't get me started on Break Links.)- PeterBartholomew1Silver Contributor
I would respectfully suggest that rather than fighting Excel it is better to exploit what it offers.
If you wish to use a sheet as a template to be copied within the workbook do not use that sheet to hold data. Its sole purpose is to initialise copies with sheet-local names and with any appropriate formatting. Do not copy such a sheet between workbooks or you will create inter-workbook links.
If you prefer to use a 'January' sheet (say) as the template for 'February' etc, avoid any defined names that are scoped to the workbook, use only sheet-scoped names.
The named Lambda functions should not duplicate when sheets are copied within a workbook but, if copied to a different workbook, should create a usable function scoped to the receiving workbook with no references to the source.
Something that may prove useful is to use workbook scoped names for 3D ranges, for example
='January 24:February 24'!$E$5:$E$75
These work well with dynamic array functions such as TOCOL and VSTACK.
- Ron_OliveiraCopper Contributor
I have the same problem with copying sheets, but I don’t think that your solution works in my case. I use a lot of groups on some sheets. I don’t know of any way to Include the groups in the copy except by the Move or Copy option on the sheets tab. Do you have a way to include groups?
- ChrisDaveyCopper Contributor
Solution:
- In the source workbook, identify the formulas and/or functions that refer to globally scoped named ranges.
- Within each worksheet, open the Name Manager and create a locally scoped named range that contains the globally scoped named range. You will reference the global name using the indirect function: For example, if the range “Colors” is a global range stored on Sheet 1, and “Colors” is used in a formula or function on Sheet 2, create a local name on Sheet 2 (let’s call it Colors2) defined as =indirect(“Colors”). Do this for all instances on all worksheets you desire to copy / move to other workbooks.
- Where global names are referenced, substitute the local name you defined. Confirm your formulas still function. Continuing the example, if on Sheet 2 there is a function or formula that refers to the range “Colors” directly, substitute “Colors2” to refer to that range indirectly. Instead of =vlookup([value],Colors,1,false), use =vlookup([value],Colors2,1,false). If you receive a #REF effort, go back and check the Name Manager for misspellings.
- Before moving any worksheet from your source workbook to other destination workbooks, you must remove all global names. In the example, even if Sheet 2 were copied to another workbook after completing step 3, all global names from the source workbook will be transferred to the destination, which will duplicate the ranges that already exist at the destination. I suggest copying or moving each worksheet to their own separate workbooks. Continuing with the example, after you have changed the references on Sheet 2 to “Colors2”, copy or move the sheet into a separate new workbook (let’s call it Workbook_Sheet2). Save this new workbook, open the Name Manager, sort by scope, and remove all names that are scoped to the source workbook. Depending on whether you save > close > reopen, or just save the workbook, the global ranges will appear with the scope “workbook” or show the file name of the source workbook from where it was copied.
- From Workbook_Sheet2, you will now be able to move or copy the desired worksheet to other destination workbooks. The formulas and functions will update automatically, as long as the range(s) at destination is(are) called exactly the same as they are at the source (i.e. “Colors”). There will be no external links back to the source workbook. The only ranges that will be added to the Name Manager at the destination are those that were locally scoped to the sheet you copied, so you will not have duplicates in the Name Manager. You will also note that, because there are no external links, Excel will not substitute #REF into the local ranges that are copied with the sheet. You will however see #REF when you copy the sheet to a new workbook (to remove global names) before you move it to the destination. Lastly, and perhaps what’s the nicest feature of this process, is that the parameters of the named ranges in the destination workbook are preserved. Meaning, if the range Colors in the source workbook was stored on Sheet 1 in cells A1:A5, and the range Colors in the destination workbook is stored on Sheet 10 in cells A10:A20, the copied worksheet will preserve the named range of Colors at the destination (Sheet 10 cells A10:A20).
Explanation:
It may not be a literal definition, but I think of indirect as a function that converts text into a reference; you feed it a text string, and Excel looks for an object (whether it’s a named range, table, graph, etc.) that matches the text.
Using indirect to refer to global names means that when the formulas/functions are copied/moved from one workbook to another, you aren’t copying references - you’re copying text strings. When those text strings arrive at the destination, the indirect function will look for an object at the destination that matches the text. On either end of the process – meaning at both the source workbook and the destination workbook – the indirect function successfully finds a reference to an object. In between the source and destination, the reference is preserved as a text string.
Disclaimer:
Indirect is a volatile function, and by storing it in the name manager, it may result in slow workbook performance if that name is used repetitively. This may motivate you to reconsider your functions/formulas that are most efficient. There’s a volume of good practices for ways to write efficient formulas, as well as information on how, among similar formulas (i.e. lookup formulas), certain functions are more efficient than others (for example, xlookup tends to be more efficient than vlookup or xlookup). You may also wish to take it a step further, and remove the indirect functions once the sheet(s) are copied to the destination.
I haven’t found any other responses for this question online. What inspired my approach to this was a post that said “do not fight Excel, but instead exploit what it has to offer.” Please let me know if this solution works or if you have any feedback.