如何获取 Excel 单元格的 "Name Box" 名称?
How to get the "Name Box" name of an Excel cell?
我在 Excel 中有一个单元格,我使用 Excel 名称框为其分配了一个变量名 this_cells_name
到一个单元格 D2
.
这是 Excel 名称框的示例:
我希望能够指向该单元格并将变量名称作为 return 值。
我知道如何做到以下几点:
- 使用
=CELL("address",D2)
并得到 "$D"
作为 return 值,
- 使用
=CELL("address",this_cells_name)
并得到 "$D"
作为 return 值。
我想执行以下操作:
- 使用
=some_function(D2)
并得到 "this_cells_name"
作为 return 值。
我该怎么做? VBA 解决方案就可以了。
考虑:
Public Function WhatsInAName(r As Range) As String
WhatsInAName = ""
For Each n In ThisWorkbook.Names
If Range(n).Address(0, 0) = r.Address(0, 0) Then
WhatsInAName = n.Name
End If
Next n
End Function
例如:
您也可以使用
Dim var as variant
on error Resume Next
var=Range("D2").Name.Name
on error goto 0
if IsEmpty(var) then msgbox "Cell has no name"
报错是为了处理单元格没有名字的情况
我在 Excel 中有一个单元格,我使用 Excel 名称框为其分配了一个变量名 this_cells_name
到一个单元格 D2
.
这是 Excel 名称框的示例:
我希望能够指向该单元格并将变量名称作为 return 值。
我知道如何做到以下几点:
- 使用
=CELL("address",D2)
并得到"$D"
作为 return 值, - 使用
=CELL("address",this_cells_name)
并得到"$D"
作为 return 值。
我想执行以下操作:
- 使用
=some_function(D2)
并得到"this_cells_name"
作为 return 值。
我该怎么做? VBA 解决方案就可以了。
考虑:
Public Function WhatsInAName(r As Range) As String
WhatsInAName = ""
For Each n In ThisWorkbook.Names
If Range(n).Address(0, 0) = r.Address(0, 0) Then
WhatsInAName = n.Name
End If
Next n
End Function
例如:
您也可以使用
Dim var as variant
on error Resume Next
var=Range("D2").Name.Name
on error goto 0
if IsEmpty(var) then msgbox "Cell has no name"
报错是为了处理单元格没有名字的情况