Worksheet function to return name of range?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
Is there a way in a worksheet (without using VBA) to obtain the name of a range, if one is defined?

For example, suppose I have assigned the name "HeaderRow" to $7:$7. Is there a way for me to obtain the text "HeaderRow" if I provide the row number (7)?

The Address function doesn't seem to do it and there doesn't seem to be a Name() function.

Thanks
 
Assuming this is for development and not deployment, why not just put a UDF in Personal to do this?

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td]myCol[/td][td="bgcolor:#E5E5E5"](Workbook) Sheet1!myCol Refers To: =Sheet1!B:B[/td][td]B1: =sGetname(A1)[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]myRow[/td][td="bgcolor:#E5E5E5"](Workbook) Sheet1!myRow Refers To: =Sheet1!2:2[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]relN[/td][td="bgcolor:#E5E5E5"](Workbook) Sheet1!relN Refers To: =Sheet1!B2[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]Bob[/td][td="bgcolor:#E5E5E5"]Sheet4!Bob Refers To: =Sheet4!$A$1[/td][td][/td][/tr]
[/table]
 
Last edited:
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Or this:

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td][/td][td="bgcolor:#E5E5E5"]Sheet4!Jennifer[/td][td]B1: =WhatsherName(A1)[/td][/tr]
[/table]
 
Upvote 0
The main problem is that if I change a name, the cell containing the name does not change.

Ah. Got it. You were actually on the right track. So... create a named formula (just like naming a range) called FormulaLeft

In the RefersTo box of the name definition dialog put

=GET.CELL(6,OFFSET(INDIRECT("RC",FALSE),0,-1))

Now, in A1, put a formula that references the name you're interested in with just the equals sign. Example:

=BillJelenIsASpaceCowboy

Then in B1 put the following formula:

=RIGHT(FormulaLeft,LEN(FormulaLeft)-1)

Then in C1 you can put:

=ADDRESS(ROW(INDIRECT(B1)),COLUMN(INDIRECT(B1)),1,1,MID(CELL("filename",B1),FIND("]",CELL("filename",B1))+1,255))

And C1 should tell you the address of whereever BillJelenIsASpaceCowboy points to.

Go into the Name Manager and change BillJelenIsASpaceCowboy's name to be BillJelenIsASteelyEyedMissileMan

A1's formula will update to the new name which B1 will reflect and C1 keeps working using the new name.
 
Last edited:
Upvote 0
It just occurred to me that there might be another even more arcane work-around. I have a GetFormula UDF that I wrote some time ago that will return the formula in a cell.
But you said "without using VBA" in Message #1 ... if that has changed, then you can use this UDF to get the Defined Name for the range wholly containing within, or exactly equal to, an actual range that has a Defined Name. So, if you have a Defined Name covering, say, the range G5:H10, then specifying any single cell, or multiple range of cells, that are wholly contained within the range G5:H10 (so, for example, you could specify G8, or G6:H9 or G5:H10 as the argument to my UDF, and it would return the Defined Name containing those cells. If you change the defined name in the Name Manager, then the formula containing my UDF will change accordingly. Would such a UDF be helpful to you? If so, here is the code for it...
Code:
[table="width: 500"]
[tr]
	[td]Function GetName(Rng As Range) As String
  Dim Nm As Name
  Application.Volatile
  For Each Nm In Names
    If Not Intersect(Rng, Nm.RefersToRange) Is Nothing Then
      If Union(Rng, Nm.RefersToRange).Address = Nm.RefersToRange.Address Then
        GetName = Nm.Name
        Exit For
      End If
    End If
  Next
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Assuming this is for development and not deployment, why not just put a UDF in Personal to do this?

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #C0C0C0"][/TD]
[TD="bgcolor: #C0C0C0"]
A​
[/TD]
[TD="bgcolor: #C0C0C0"]
B​
[/TD]
[TD="bgcolor: #C0C0C0"]
C​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #C0C0C0"]
1​
[/TD]
[TD]myCol[/TD]
[TD="bgcolor: #E5E5E5"](Workbook) Sheet1!myCol Refers To: =Sheet1!B:B[/TD]
[TD]B1: =sGetname(A1)[/TD]
[/TR]
[TR]
[TD="bgcolor: #C0C0C0"]
2​
[/TD]
[TD]myRow[/TD]
[TD="bgcolor: #E5E5E5"](Workbook) Sheet1!myRow Refers To: =Sheet1!2:2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #C0C0C0"]
3​
[/TD]
[TD]relN[/TD]
[TD="bgcolor: #E5E5E5"](Workbook) Sheet1!relN Refers To: =Sheet1!B2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #C0C0C0"]
4​
[/TD]
[TD]Bob[/TD]
[TD="bgcolor: #E5E5E5"]Sheet4!Bob Refers To: =Sheet4!$A$1[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

This is for personal use only, so it doesn't need to pass any international code standards. :eeek:

I guess I'm too dense. I don't understand what to put in the UDF. Is there a VBA function that will return the name given the range? Maybe something like:

Code:
Dim sName as String
sName = RangeName("$2:$2")

I tried these:
Code:
?range("$2:$2").Address
$2:$2
?range("$2:$2").ApplyNames
?range("$2:$2").ID

?range("$2:$2").ListNames
True
?range("$2:$2").name
='1 loss'!$2:$2
?range("$2:$2").Range

Some got errors. None returned "HeaderRow".

Here's a simplified version of my sheet:

[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[TD="align: center"]H[/TD]
[TD="align: center"]I[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: right"]HdrRow[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]Sum[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: right"]NumEvents[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"]7[/TD]
[TD="align: center"]11[/TD]
[TD="align: center"]38[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: right"]Percent[/TD]
[TD="align: center"]8%[/TD]
[TD="align: center"]13%[/TD]
[TD="align: center"]32%
[/TD]
[TD="align: center"]18%[/TD]
[TD="align: center"]29%[/TD]
[TD="align: center"]100%[/TD]
[/TR]
</tbody>[/TABLE]

The labels in Col C are the names assigned to those rows. For example, "$4:$4" is assigned the name "HdrRow".

I would like to replace the literal text in Col C with a formula or function that will return the exact same result.
 
Upvote 0
But you said "without using VBA" in Message #1 ...
I should have said, "preferably without using VBA". I was hoping for a simple worksheet function. I seem to be writing UDFs for everything these days and many of them should, IMNSHO, be built into Excel.

if that has changed, then you can use this UDF to get the Defined Name for the range wholly containing within, or exactly equal to, an actual range that has a Defined Name. So, if you have a Defined Name covering, say, the range G5:H10, then specifying any single cell, or multiple range of cells, that are wholly contained within the range G5:H10 (so, for example, you could specify G8, or G6:H9 or G5:H10 as the argument to my UDF, and it would return the Defined Name containing those cells. If you change the defined name in the Name Manager, then the formula containing my UDF will change accordingly. Would such a UDF be helpful to you? If so, here is the code for it...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function GetName(Rng As Range) As String
  Dim Nm As Name
  Application.Volatile
  For Each Nm In Names
    If Not Intersect(Rng, Nm.RefersToRange) Is Nothing Then
      If Union(Rng, Nm.RefersToRange).Address = Nm.RefersToRange.Address Then
        GetName = Nm.Name
        Exit For
      End If
    End If
  Next
End Function[/TD]
[/TR]
</tbody>[/TABLE]

Let me play with that a bit. Is there a way to get it to only return a result if the passed range matches the name exactly?

Thanks
 
Upvote 0
Is there a way to get it to only return a result if the passed range matches the name exactly?
Sure...
Code:
Function GetName(Rng As Range) As String
  Dim Nm As Name
  Application.Volatile
  For Each Nm In Names
    If Rng.Address = Nm.RefersToRange.Address Then
      GetName = Nm.Name
      Exit For
    End If
  Next
End Function
 
Upvote 0
...(without using VBA)...

...The main problem is that if I change a name, the cell containing the name does not change....

I should have said, "preferably without using VBA".

Were you able to try out the non-VBA-based solution I provided in post #13 on this thread? As far as I understand your requirements, that ought to provide the information you seek.

Also, the use of OFFSET() in the definition of FormulaLeft was overkill. FormulaLeft can be simplified to simply be defined as:

=GET.CELL(6,INDIRECT("RC[-1]",FALSE))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top