Is there a way to obtain the name of a range?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
Suppose I want to show the names of various named ranges in a table. In this table, for example, I have assigned the name "Price" to Column C and "Quan" to Column D. I would like to put some expression, like "=RangeName(C:C)", into C6 and D6 that would show those names and would change if I renamed those ranges.

R/CCDEF
5Unit PriceQuantityCostFormula
6(Price)(Quan)()()
7$9.98250$2,495.00E7: =Price*Quan
8$1.29500$645.00E8: =Price*Quan
9$5.49130$713.70E9: =Price*Quan

<tbody>
</tbody>

If this does not exist, can I write a UDF that will do the job?

Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
From the Immediate window

Code:
? Range("C:C").Name.Name

Depends on how the ranges are names, however.
 
Upvote 0
E.g.,

Code:
Sub JM()
  With Range("C:C")
    .Worksheet.Names.Add Name:="JM", RefersTo:=.Cells
    Debug.Print .Name.Name
    .Worksheet.Names("JM").Delete
    
    .Worksheet.Names.Add Name:="Jennifer", RefersTo:=.Cells
    Debug.Print .Name.Name
    .Worksheet.Names("Jennifer").Delete
    
    .Worksheet.Parent.Names.Add Name:="JM", RefersTo:=.Cells
    Debug.Print .Name.Name
  End With
End Sub
 
Upvote 0
From the Immediate window

Code:
? Range("C:C").Name.Name

Depends on how the ranges are names, however.

Cool! :)

Here's what I came up with:

Code:
Function RangeName(pRange As String) As String

Dim pieces() As String
RangeName = Range(pRange).Name.Name
pieces = Split(RangeName, "!")
RangeName = pieces(1)
   
End Function

It works!

Is there a way to make it simpler? Can I get the result from the Range function without the sheet name?

Thanks!
 
Upvote 0
If you want to know the name range a cell belongs to, try this UDF
eg.
=NameRange(C2)

Code:
Function NameRange(cell As Range)
    Dim wn As Variant
    Dim rng As Range
    On Error Resume Next
    For Each wn In ThisWorkbook.Names
        Set rng = Range(wn)
        If rng.Parent.Name = cell.Parent.Name Then
            If Not Intersect(cell, rng) Is Nothing Then
                NameRange = wn.Name
                Exit Function
            End If
        End If
    Next
End Function
 
Upvote 0
Here's what I came up with:
Code:
Function RangeName(pRange As String) As String

Dim pieces() As String
RangeName = Range(pRange).Name.Name
pieces = Split(RangeName, "!")
RangeName = pieces(1)
   
End Function

And here it is in action:

R/CCDEF
5Unit PriceQuantityCostFormula
6(Price)(Quan)#VALUE!C6: ="(" & rangename(Col() & ":" & Col()) & ")"
7$9.98250$2,495.00E7: =Price*Quan
8$1.29500$645.00E8: =Price*Quan
9$5.49130$713.70E9: =Price*Quan

<tbody>
</tbody>

Two more questions:

  1. If the range does not have a name assigned, I get a value error. How can I check for this so I can return a null string?
  2. Is there a simpler way to call the function than the expression in C6 "=rangename(Col() & ":" & Col())" as shown in F6?
 
Upvote 0
E.g.,

Code:
Sub JM()
  With Range("C:C")
    .Worksheet.Names.Add Name:="JM", RefersTo:=.Cells
    Debug.Print .Name.Name
    .Worksheet.Names("JM").Delete
    
    .Worksheet.Names.Add Name:="Jennifer", RefersTo:=.Cells
    Debug.Print .Name.Name
    .Worksheet.Names("Jennifer").Delete
    
    .Worksheet.Parent.Names.Add Name:="JM", RefersTo:=.Cells
    Debug.Print .Name.Name
  End With
End Sub

Hmmm... I'm not sure what this does or how to use it. Can you help?
 
Upvote 0
Without argument:

Code:
Function RangeName2()


    Dim pieces() As String
    On Error Resume Next
    RangeName2 = Columns(ActiveCell.Column).Name.Name
    pieces = Split(RangeName2, "!")
    If UBound(pieces) = 1 Then RangeName2 = pieces(1) Else RangeName2 = pieces(0)
   
End Function
 
Upvote 0
If Price & Quant are defined as RC3 and RC4, I don't think you're going to get there.

In my example, you're asking the workbook or worksheet the name of a statically-defined range. When a UDF receives a range argument, it's already been resolved by whatever process Excel uses to evaluate arguments.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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