VBA/Excel/Access/Word/Excel/Name Create

Материал из VB Эксперт
Перейти к: навигация, поиск

Add the name to the Names collection associated with the worksheet, which only includes the names that are local to that worksheet

   <source lang="vb">

Sub nameRef()

    Worksheets("Sheet1").Names.add name:="Costs", RefersTo:="=Sheet1!$F$10:$F$12"

End Sub "Using the Name Property of the Range Object Sub name()

    range("A1:D10").name = "SalesData"

End Sub

</source>
   
  


create a global name that refers to a range using the Add method of the Workbook object"s Names collection

   <source lang="vb">

Sub add()

    Names.add name:="Data", RefersTo:="=Sheet1!$D$10:$D$12"

End Sub

</source>
   
  


Create a local name

   <source lang="vb">

Sub localDemo()

    Names.add name:="Sheet1!Sales", RefersTo:="=Sheet1!$E$10:$E$12"

End Sub

</source>
   
  


Create a name based on a Range

   <source lang="vb">

Sub nameName()

   range("A1:F6").name = "Fruits"

End Sub

</source>
   
  


Define a name by referring a workbook

   <source lang="vb">

Sub DefineName3()

 ActiveWorkbook.names.Add Name:="WorkArea", RefersTo:="=" + ActiveWorkbook.Sheets("sheet1").[A1].CurrentRegion.Address(External:=True)
 Debug.Print Range("WorkArea").Address(External:=True)

End Sub

</source>
   
  


Define a name by referring the selection cell address

   <source lang="vb">

Sub DefineName2()

 ActiveWorkbook.Activate
 [A1].CurrentRegion.Activate
 ActiveWorkbook.Names.Add Name:="WorkArea", RefersTo:="=" + Selection.Address
 Debug.Print Range("WorkArea").Address(External:=True)

End Sub

</source>
   
  


renames an existing name:

   <source lang="vb">

Sub nameName2()

   Names("Fruits").name = "Produce"

End Sub

</source>