Hi,
I have a template sheet that contains a number of named ranges that I want to make unique when I copy the sheet, but I'm having trouble with it and could use some help. Let me explain
The Template Sheet contains named ranges like
xxx_var1
xxx_thingy2
I have a function to copy the sheet and I want to rename the ranges so that; (copy TemplateSheet to Copy1)
xxx_var1 ===> Copy1_var1
xxx_thingy2 ===> Copy1_thingy2
That way I can uniquely reference any of these named ranged from a summary sheet with references like;
'Copy1'!Copy1_var1
'Copy2'!Copy2_var1
'Copy3'!Copy3_var1
The copy works fine, but it is the rename I'm having trouble with. I've referred to this existing post (http://www.mrexcel.com/forum/excel-...e-ranges-using-visual-basic-applications.html), but am still having trouble.
When I rename the ranges they all get a '[Sheet Name]'! prefix which is then hard-coded into the cell. This means that the cell name for example is "'Copy1'!Copy1_var1" and to reference it I would have to do something like "'Copy1'!'Copy1'!Copy1_var1" which doesn't work.
Below is a snippet of my rename code. s1 correctly removes the xxx prefix. s2 seems to fail such that Nm.Name ends up being named "'Copy1'!xxx_var1" which makes no sense as I've already removed the xxx prefix. I have a second s2 commented out which replaces the Sheet reference with a hard string "Fluffy" and this works such that a resulting range is "Fluffy_var1" and can be referenced with "'Fluffy'!Fluffy_var1".
Any insights into what is going on and how to fix it would be appreciated.
Sincerely, Paul.
I have a template sheet that contains a number of named ranges that I want to make unique when I copy the sheet, but I'm having trouble with it and could use some help. Let me explain
The Template Sheet contains named ranges like
xxx_var1
xxx_thingy2
I have a function to copy the sheet and I want to rename the ranges so that; (copy TemplateSheet to Copy1)
xxx_var1 ===> Copy1_var1
xxx_thingy2 ===> Copy1_thingy2
That way I can uniquely reference any of these named ranged from a summary sheet with references like;
'Copy1'!Copy1_var1
'Copy2'!Copy2_var1
'Copy3'!Copy3_var1
The copy works fine, but it is the rename I'm having trouble with. I've referred to this existing post (http://www.mrexcel.com/forum/excel-...e-ranges-using-visual-basic-applications.html), but am still having trouble.
When I rename the ranges they all get a '[Sheet Name]'! prefix which is then hard-coded into the cell. This means that the cell name for example is "'Copy1'!Copy1_var1" and to reference it I would have to do something like "'Copy1'!'Copy1'!Copy1_var1" which doesn't work.
Below is a snippet of my rename code. s1 correctly removes the xxx prefix. s2 seems to fail such that Nm.Name ends up being named "'Copy1'!xxx_var1" which makes no sense as I've already removed the xxx prefix. I have a second s2 commented out which replaces the Sheet reference with a hard string "Fluffy" and this works such that a resulting range is "Fluffy_var1" and can be referenced with "'Fluffy'!Fluffy_var1".
Code:
'Rename the named ranges (variables) on the template sheet so they are unique for the new sheet
Dim Nm As Name
For Each Nm In ActiveSheet.Names
If (UBound(Split(Nm, "!")) > 0) Then 'Only process names local to the worksheet
s1 = WorksheetFunction.Substitute(Nm.Name, "xxx", "") ' Remove the xxx generic prefix
s2 = WorksheetFunction.Substitute(s1, "'" & ProjectName & "'!", ProjectName) ' This doesn't
's2 = WorksheetFunction.Substitute(s1, "'" & ProjectName & "'!", "Fluffy") ' This works?
Nm.Name = s2
End If
Next Nm
Any insights into what is going on and how to fix it would be appreciated.
Sincerely, Paul.