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
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