使用 EXCEL 中的函数通过地址或引用获取单元格的名称
Get the Name of a cell via its address or reference by using a function in EXCEL
我搜索了很多,但没有找到任何好的答案!
Is there any internal Function returning the Name
of a cell by passing its address as a parameter?
如果不是,获得我们为单元格定义的 name
的最简单方法是什么???
例如,我将 'test_name' 定义为单元格 B4
的 name
,其内容为:'test'.
我想要 excel 中的函数,例如:CellName(adr) 并使用它,例如 CellName(B4) 和 returns 'test_name'
我也搜索了很多以找到内置的 excel 函数,但没有找到任何东西!
您可以使用UDF
(用户自定义函数)在Excel
中实现Cell Name
对于这一幕,我们将使用视觉基础语言 (vb)
我发现了一些定义好的函数,我将解释如何添加。
I've appended 3 types of VB Codes you can choose one of them by your own and add it.
步骤
首先,打开需要的项目:你可以看到这一步here或者按照下面的步骤:
在 excel 中打开一个新的 工作簿 。
您应该在 visual basic for applications (VBA)
中定义所需的函数
打开VB使用快捷键alt+F11
向您的工作簿添加一个新的模块。
一个新的 模块 window 将打开,用于在其中添加您的代码。
其次,附加代码:
复制只是一个这些三个代码:
1. CellName1(cell): takes the cell address and returns the cell's name
Public Function CellName1(cel As Range) As Variant
Dim nm As name
For Each nm In Names
If nm.RefersTo = "=" & cel.Parent.name & "!" & cel.Address Then
CellName1 = nm.name
Exit Function
End If
Next
CellName1 = CVErr(xlErrNA)
End Function
2. CellName2(cell): takes the cell address and returns the cell's name
Function CellName2(cel As Range) As String
Dim rng As Range
On Error Resume Next
Set rng = cel
If Len(rng.name.name) < 0 Then
CellName2 = "No named Range"
Exit Function
End If
CellName2 = rng.name.name
If InStr("CellName2", "!") > 0 Then
CellName2 = Right(CellName2, Len(CellName2) - InStr(CellName2, "!"))
End If
End Function
3. CellName3(row,col): takes the cell row and column and returns the cell's name
Function CellName3(r As Long, c As Long) As String
Dim rng As Range
Set rng = Cells(r, c)
On Error Resume Next
If Len(rng.name.name) < 0 Then
CellName3 = "No named Range"
Exit Function
End If
CellName3 = rng.name.name
End Function
不要忘记保存您粘贴的函数!
例子
考虑具有名称的单元格 B4:"test_name",现在您可以通过上面定义的每个函数获取它的名称...
现在假设另一个像 D7 这样的单元格。
我们对该单元格使用第一个函数 (CellName1
),对于结果,'B4' 单元格的名称将是 'D7'
的内容
所以,我们将B4
作为参数传递给函数,并获取其名称作为'D7'单元格的内容。
CellName1(B4)
或
CellName2(B4)
或
CellName3(4,2)
我搜索了很多,但没有找到任何好的答案!
Is there any internal Function returning the
Name
of a cell by passing its address as a parameter?
如果不是,获得我们为单元格定义的 name
的最简单方法是什么???
例如,我将 'test_name' 定义为单元格 B4
的 name
,其内容为:'test'.
我想要 excel 中的函数,例如:CellName(adr) 并使用它,例如 CellName(B4) 和 returns 'test_name'
我也搜索了很多以找到内置的 excel 函数,但没有找到任何东西!
您可以使用UDF
(用户自定义函数)在Excel
Cell Name
对于这一幕,我们将使用视觉基础语言 (vb)
我发现了一些定义好的函数,我将解释如何添加。
I've appended 3 types of VB Codes you can choose one of them by your own and add it.
步骤
首先,打开需要的项目:你可以看到这一步here或者按照下面的步骤:
在 excel 中打开一个新的 工作簿 。
您应该在 visual basic for applications (VBA)
中定义所需的函数
打开VB使用快捷键alt+F11
向您的工作簿添加一个新的模块。
一个新的 模块 window 将打开,用于在其中添加您的代码。
其次,附加代码:
复制只是一个这些三个代码:
1. CellName1(cell): takes the cell address and returns the cell's name
Public Function CellName1(cel As Range) As Variant Dim nm As name For Each nm In Names If nm.RefersTo = "=" & cel.Parent.name & "!" & cel.Address Then CellName1 = nm.name Exit Function End If Next CellName1 = CVErr(xlErrNA) End Function
2. CellName2(cell): takes the cell address and returns the cell's name
Function CellName2(cel As Range) As String Dim rng As Range On Error Resume Next Set rng = cel If Len(rng.name.name) < 0 Then CellName2 = "No named Range" Exit Function End If CellName2 = rng.name.name If InStr("CellName2", "!") > 0 Then CellName2 = Right(CellName2, Len(CellName2) - InStr(CellName2, "!")) End If End Function
3. CellName3(row,col): takes the cell row and column and returns the cell's name
Function CellName3(r As Long, c As Long) As String Dim rng As Range Set rng = Cells(r, c) On Error Resume Next If Len(rng.name.name) < 0 Then CellName3 = "No named Range" Exit Function End If CellName3 = rng.name.name End Function
不要忘记保存您粘贴的函数!
例子
考虑具有名称的单元格 B4:"test_name",现在您可以通过上面定义的每个函数获取它的名称...
现在假设另一个像 D7 这样的单元格。
我们对该单元格使用第一个函数 (CellName1
),对于结果,'B4' 单元格的名称将是 'D7'
所以,我们将B4
作为参数传递给函数,并获取其名称作为'D7'单元格的内容。
CellName1(B4)
或
CellName2(B4)
或
CellName3(4,2)