Deleting errored named ranges deletes the wrong name

Mrock1

Board Regular
Joined
Oct 7, 2014
Messages
78
I have a workbook in which I have a temple worksheet with a local (scoped to that worksheet) named range called "Title". I am using VBA to create a new worksheet and copy a range from this template worksheet to the new one. I have two issues:
1. When the selected range (including the worksheet named range, Title) is copied a named range is created with a WORKBOOK scope, not just the new worksheet as I require;
I end up with the following in my named ranges:
Name Value Refers to Scope
Title value ='Template'!$B$6 Template
Title value ='New Sheet'!$B$6 Workbook
2. If I delete that new worksheet, the named range predictably goes to a #REF error state. I am using the code below to try to delete the erroneous named range (from a macro on the template sheet), but it deletes the named range on the template worksheet, not the that is in error.
Dim nr as Name
For Each nr In ActiveWorkbook.Names
If InStr(nr.Value, "#REF") Then nr.Delete
Next nr

The code seems to correctly find the #Ref named range, but the subsequent nr.delete command deletes the wrong one, leaving that erroneous named range leaving me with:
After deleting the new worksheet, but before running the code:
Name Value Refers to Scope
Title value ='Template'!$B$6 Template
Title value =#Ref!$B$6 Workbook
After running the code:
Name Value Refers to Scope
Title value =#Ref!$B$6 Workbook

Even though watching the code in the VBE debugger, the correct value of nr is found, the nr.Delete seems to delete the wrong named range (that isn't in error).

Frankly this makes no sense, but it must be something to do with the fact that I'm defining the same name on multiple sheets by setting the named range scope to a particular worksheet (notwithstanding issue #1).

Hope someone can help
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Just a thought, it may be wise to give the named range on your template a Workbook scope. The moment you copy this template, the new named range with the exact same name automatically gets a worksheet scope on each of the new copies. If such a new worksheet is deleted and no new references to that worksheet have been created in the meantime, there is a very small chance you will end up with invalid named ranges (#REF).
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top