参考工作簿名称

时间:2018-04-25 13:38:07

标签: excel-vba reference vba excel

我在通过给定引用(Workbook)获取RefersTo对象的名称时遇到问题。代码如下:

Sub add()

  ThisWorkbook.Names.add Name:="Letee", RefersTo:="Nazwa2"
  MsgBox ThisWorkbook.Names(RefersTo:="Nazwa2")

End Sub

1 个答案:

答案 0 :(得分:1)

确保 Nazwa2 是有效名称:

Sub add()

Sheets(1).Range("A1").Name = "Nazwa2"
ThisWorkbook.Names.add Name:="Letee", RefersTo:="Nazwa2"
MsgBox ThisWorkbook.Names(1)

End Sub

要查看名称链:

Sub addx()

    Sheets(1).Range("A1").Name = "Nazwa2"
    ThisWorkbook.Names.add Name:="Letee", RefersTo:="Nazwa2"

    i = 1
    For Each nm In ActiveWorkbook.Names
         Cells(i, 1).Value = nm.Name
         Cells(i, 2).Value = "'" & nm.RefersTo
     i = i + 1
    Next

End Sub

enter image description here