Jon von der Heyden
MrExcel MVP, Moderator
- Joined
- Apr 6, 2004
- Messages
- 10,912
- Office Version
- 365
- Platform
- Windows
I'm trying to validate that a given string is acceptable for a name. I know I could write code to attempt to disallow specific characters but I thought this approach would be better:
The problem I am facing has to do with the scope. Say the following names exist:
Book - scope is Workbook
Sheet - scope is Worksheet (Sheet1)
If a new name is selected, called 'Sheet', and the scope is set to Workbook, then it should be acceptable. But I notice that my existing name 'Sheet' becomes overwritten, despite the fact that it is explicitly scoped to workbook.
I checked by manually adding names. I can create the following 3 names:
Book - scope is Workbook
Sheet - scope is Worksheet (Sheet1)
Sheet - scope is Workbook
Recording the set-up of these three names generates the following:
Yet if done progamatically only two names exist. The 2nd instance of creating 'Sheet' overwrites the first.
Can anyone shed light on how to manage the scope of names please?
Code:
[color=darkblue]Public[/color] [color=darkblue]Property[/color] [color=darkblue]Get[/color] ValidName([color=darkblue]ByVal[/color] strName [color=darkblue]As[/color] [color=darkblue]String[/color]) [color=darkblue]As[/color] [color=darkblue]Boolean[/color]
[color=darkblue]Dim[/color] nme [color=darkblue]As[/color] Name
ValidName = [color=darkblue]True[/color]
[color=darkblue]If[/color] Len(strName) = 0 [color=darkblue]Then[/color]
ValidName = [color=darkblue]False[/color]
[color=darkblue]GoTo[/color] Finally
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]With[/color] Me
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
[color=darkblue]If[/color] .NameScope = NSworkbook [color=darkblue]Then[/color]
[color=darkblue]Set[/color] nme = ActiveWorkbook.Names(strName)
[color=darkblue]If[/color] [color=darkblue]Not[/color] nme [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
[color=darkblue]If[/color] nme.Parent [color=darkblue]Is[/color] ActiveWorkbook [color=darkblue]Then[/color]
ValidName = [color=darkblue]False[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]ElseIf[/color] .NameScope = NSworksheet [color=darkblue]Then[/color]
[color=darkblue]Set[/color] nme = ActiveSheet.Names(strName)
[color=darkblue]If[/color] [color=darkblue]Not[/color] nme [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
[color=darkblue]If[/color] nme.Parent [color=darkblue]Is[/color] ActiveSheet [color=darkblue]Then[/color]
ValidName = [color=darkblue]False[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0
[color=darkblue]If[/color] ValidName = [color=darkblue]False[/color] [color=darkblue]Then[/color] [color=darkblue]GoTo[/color] Finally
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
[color=darkblue]If[/color] .NameScope = NSworkbook [color=darkblue]Then[/color]
[color=darkblue]Set[/color] nme = ActiveWorkbook.Names.Add(Name:=strName, RefersTo:="Delete Me")
[color=darkblue]ElseIf[/color] .NameScope = NSworksheet [color=darkblue]Then[/color]
[color=darkblue]Set[/color] nme = ActiveSheet.Names.Add(Name:=strName, RefersTo:="Delete Me")
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0
ValidName = [color=darkblue]Not[/color] nme [color=darkblue]Is[/color] [color=darkblue]Nothing[/color]
[color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]If[/color] [color=darkblue]Not[/color] nme [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
nme.Delete
[color=darkblue]End[/color] [color=darkblue]If[/color]
Finally:
[color=darkblue]Set[/color] nme = [color=darkblue]Nothing[/color]
[color=darkblue]End[/color] [color=darkblue]Property[/color]
The problem I am facing has to do with the scope. Say the following names exist:
Book - scope is Workbook
Sheet - scope is Worksheet (Sheet1)
If a new name is selected, called 'Sheet', and the scope is set to Workbook, then it should be acceptable. But I notice that my existing name 'Sheet' becomes overwritten, despite the fact that it is explicitly scoped to workbook.
I checked by manually adding names. I can create the following 3 names:
Book - scope is Workbook
Sheet - scope is Worksheet (Sheet1)
Sheet - scope is Workbook
Recording the set-up of these three names generates the following:
Code:
[color=darkblue]Sub[/color] Macro2()
ActiveWorkbook.Names.Add Name:="Book", RefersToR1C1:="=Sheet1!R8C15"
ActiveWorkbook.Worksheets("Sheet1").Names.Add Name:="Sheet", RefersToR1C1:= _
"=Sheet1!R8C15"
ActiveWorkbook.Names.Add Name:="Sheet", RefersToR1C1:="=Sheet1!R8C15"
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
Yet if done progamatically only two names exist. The 2nd instance of creating 'Sheet' overwrites the first.
Can anyone shed light on how to manage the scope of names please?