Advertisement
AlanElston

Named Ranges scope defines the default path used for a named

Nov 11th, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.98 KB | None | 0 0
  1. Sub NamedRangeScopes()
  2. 10    Call FukOffNames
  3. 20    Call getWbNames
  4. 30   Rem 1 Add 3 named ranges, 1(i) '_-in the Workbooks name object collection, and 1(ii) in the first worksheet name object collection and 1(iii) '_-in the second worksheet name object collection
  5. 40   '1(i) Add a Workbook names object in the Workbook name object collection of this workbook
  6. 50    ThisWorkbook.Names.Add Name:="Name1", RefersTo:=ThisWorkbook.Worksheets.Item(1).Range("A1")   '_-in the Workbooks name object collection
  7. 60    'The form above is like   ThisWorkbook.Names.Add Name:="Name1", RefersTo:=Worksheets(Sheet1).Range("A1")
  8. 70   '1(ii) Add a name object in the first worksheet's name object collection
  9. 80    ThisWorkbook.Worksheets.Item(1).Names.Add Name:="Name1", RefersTo:=ThisWorkbook.Worksheets.Item(1).Range("A1")  '_-in the first worksheet name object collection
  10. 90    'The form above is like   Worksheets("Sheet1).Names.Add Name:="Name1"  ,    RefersTo:=Sheet1.Range("A1")
  11. 100  '1(iii) Add a name object in the second worksheet's name object collection
  12. 110   ThisWorkbook.Worksheets.Item(2).Names.Add Name:="Name2", RefersTo:=ThisWorkbook.Worksheets.Item(2).Range("A1")  '_-in the second worksheet name object collection
  13. 120   'The form above is like   Worksheets("Sheet2).Names.Add Name:="Name2"  ,    RefersTo:=Sheet2.Range("A1")
  14. 130  Rem 2 Change the string name of a named range
  15. 140   Call GetChaNameObjects(140) ' Check out Info for all Name objects
  16. 150  '2a) Use Workbook names objects to Change the worksheet names object name that has the same name as the workbook names object name, change it twice, first using the workbook names object collection and then the worksheet names object collection
  17. 160   Let ThisWorkbook.Names(ThisWorkbook.Worksheets.Item(1).Name & "!" & "Name1").Name = "Name1_1"
  18. 170   '       The form above is like             ThisWorkbook.Names("Sheet1!Name").Name = "Name1_1"
  19. 180   Call GetChaNameObjects(180)
  20. 190   Let ThisWorkbook.Worksheets.Item(1).Names(ThisWorkbook.Worksheets.Item(1).Name & "!" & "Name1_1").Name = "Name1_2"
  21. 200   Call GetChaNameObjects(200)
  22. 210   Let ThisWorkbook.Worksheets.Item(1).Names("Name1_2").Name = "Name1_3"
  23. 220   Call GetChaNameObjects(220)
  24. 230  '2b) use a Worksheet's (in this example the second worksheet's) name objects to Change the second worksheet's names object, ( we gave it "Name2", but Excel adds a bit so it looks like  Sheet2!Name2" which you can get from a VBA code line like  ThisWorkbook.Worksheets.Item(2).Name & "!" & "Name2"   I do this just in case your second worksheet has a tab name other than  Sheet2
  25. 240   Let ThisWorkbook.Worksheets.Item(2).Names("Name2").Name = "Name2_2"
  26. 250   ' Note: you could have equally done this:     Let ThisWorkbook.Worksheets.Item(2).Names(ThisWorkbook.Worksheets.Item(2).Name & "!" & "Name2").Name = "Name2_2"  , which is like   Let ThisWorkbook.Worksheets.Item(2).Names("Sheet2!Name2").Name = "Name2_2"
  27. 260   Call GetChaNameObjects(260)
  28. 270  Rem 3 Change the string name of a named range, for example the one in the second worksheet names collection whichg we just renamed to "Name2_2" ,(which Excel holds as like  "Sheet2!Name2_2")
  29. 280  '3a) Use Workbook names objects
  30. 290   Let ThisWorkbook.Names(ThisWorkbook.Worksheets.Item(2).Name & "!" & "Name2_2").RefersTo = ThisWorkbook.Worksheets.Item(2).Range("Z123")
  31. 300   Call GetChaNameObjects(300)
  32. 310  '3b) Use the second worksheets's  names objects
  33. 320   Let ThisWorkbook.Worksheets.Item(2).Names("Name2_2").RefersTo = ThisWorkbook.Worksheets.Item(2).Range("X23")
  34. 330   Call GetChaNameObjects(330)
  35. End Sub
  36. Sub getWbNames()
  37. Dim Nme As Name, Cnt As Long
  38.     For Each Nme In ThisWorkbook.Names
  39.      Let Cnt = Cnt + 1
  40.     Dim strNames As String: Let strNames = strNames & Cnt & "   "
  41.         If TypeOf Nme.Parent Is Worksheet Then '   https://stackoverflow.com/questions/8656793/progammatically-determine-if-a-named-range-is-scoped-to-a-workbook
  42.          Let strNames = strNames & """" & Nme.Name & """  refers to the range ref  """ & Nme & """  and  can be referenced only from worksheet with tab Name  """ & Nme.Parent.Name & """ ( Worksheet Scope ). ( That worksheet is in the workbook  """ & Nme.Parent.Parent.Name & """  )" & vbCrLf & vbCrLf ' To get the ref range you can do Nme.RefresTo or Nme.Value or if you just do Nme , then it will default probably to Nme.Value
  43.         Else
  44.          Let strNames = strNames & """" & Nme.Name & """  refers to the range ref  """ & Nme & """  and can be referenced from any sheet in the Workbook  """ & Nme.Parent.Name & """  ( Workbook Scope )" & vbCrLf & vbCrLf
  45.         End If
  46.     Next Nme
  47.     If strNames = "" Then
  48.      MsgBox prompt:="I don't think you have any Names at the moment luvy"
  49.     Else
  50.      MsgBox prompt:=strNames, Title:="Spreadsheet Named range objects in " & ThisWorkbook.Name & " are:-": Debug.Print strNames
  51.     End If
  52. End Sub
  53. Sub FukOffNames()
  54. Dim Nme As Name
  55.      For Each Nme In ThisWorkbook.Names
  56.       Nme.Delete
  57.      Next Nme
  58. End Sub
  59. Sub GetChaNameObjects(ByVal CodLn As Long)
  60. Dim Nme As Name, strOut As String
  61. ' Name objects belonging in Workbook Names Colection (Workbooks scope)
  62.      For Each Nme In ThisWorkbook.Names
  63.         If InStr(1, Nme.Name, "!", vbBinaryCompare) > 0 Then ' we will see that a name for a worksheet scope, has an extra bit added onto the name we gave it which includes a "!"
  64.          Let strOut = strOut & "Name object Name is  """ & Nme.Name & """ (you gave """ & Mid(Nme.Name, 1 + InStr(1, Nme.Name, "!", vbBinaryCompare)) & """)" & vbCrLf & "It has worksheet scope and" & vbCrLf & "it refers to range  """ & Nme.RefersTo & """" & vbCrLf & vbCrLf & vbCrLf
  65.         Else ' we will see that a name for a workbook scope, remains just as we gave it
  66.          Let strOut = strOut & "Name object Name is  """ & Nme.Name & """ (the same as you gave)" & vbCrLf & "It has workbook scope and" & vbCrLf & "it refers to range  """ & Nme.RefersTo & """" & vbCrLf & vbCrLf & vbCrLf
  67.         End If
  68.     Next Nme
  69.  MsgBox prompt:="Workbook names situation at Code Line " & CodLn & vbCrLf & vbCrLf & strOut, Title:="Name objects in Workbook """ & ThisWorkbook.Name & """ Names Colection are:-": Debug.Print "Name objects in Workbook """ & ThisWorkbook.Name & """ Names Colection are:-" & vbCr & strOut
  70. ' Name objects belonging in Workbooks Names Colection (Worksheets scope)
  71. Dim Ws As Worksheet: Let strOut = ""
  72.     For Each Ws In ThisWorkbook.Worksheets
  73.         For Each Nme In Ws.Names
  74.          Let strOut = strOut & "Name object name is  """ & Nme.Name & """ (you gave """ & Mid(Nme.Name, 1 + InStr(1, Nme.Name, "!", vbBinaryCompare)) & """)" & vbCrLf & "It has worksheets scope and" & vbCrLf & "it belongs to the Names collection of worksheet """ & Ws.Name & """" & vbCrLf & "and it refers to range  """ & Nme.RefersTo & """" & vbCrLf & vbCrLf
  75.         Next Nme
  76.     Next Ws
  77.  MsgBox prompt:="Worksheets names situation at Code Line " & CodLn & vbCrLf & vbCrLf & strOut, Title:="Name objects in all the worksheets Names Colections are:-": Debug.Print "Name objects in all the worksheets Names Colections are:-" & strOut
  78. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement