In a UDF, obtain the name of a passed named range

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I am trying to obtain the name of a named range passed to a UDF. I want to use it in error messages.

Based on previous discussions and an Internet search, I came up with this, which doesn't work.
Code:
Public Function ShowRangeName(rng As Range)
Dim rngname As String
rngname = rng.Name.Name
MsgBox "The name of this range is '" & rngname & "'"
End Function

I also tried
Code:
rngname = rng.Names.Name
rngname = rng.Name

What is the correct code?

Thanks
 
I expect it only works for statically-defined ranges -- not dynamic (or relative) ranges.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Now after reading the suggestion from Marcelo, the code is:

Code:
Public Function ShowRangeName(rng As Range) As String

Dim rngname
On Error Resume Next
rngname = rng.Name.Name
On Error GoTo 0
If IsEmpty(rngname) Then
  MsgBox "rngname is empty"
Else
  MsgBox "The name of this range is '" & rngname & "'"
End If

ShowRangeName = rngname
End Function

This code works. The only minor glitch is that it includes the sheet name, which I can remove.

It includes the sheet name only when the scope of the range is a worksheet, not Workbook.

M.
 
Upvote 0
I expect it only works for statically-defined ranges -- not dynamic (or relative) ranges.

Yep, that's it. Here's my code:

Code:
Public Function RngName(rng As Range) As String
Dim rname As String
On Error Resume Next
rname = rng.Name.Name          'Works for absolute ranges
'rname = Range(rng).Name.Name   'Doesn't work for any ranges
On Error GoTo 0
RngName = "[" & rname & "]"
End Function

Here's a table from a test sheet. Three ranges are named as shown. Range1 & Range2 are absolute. RangeR has absolute columns, but relative rows. The code above returns the results in F9:F12 using the expressions shown in G9:G12.

R/CCDEFGH
6abcRange1 = "=Sheet1!$C$6:$E$6"
7xyzRange2 = "=Sheet1!$C$7:$E$7"
8123RangeN = "=Sheet1!$C7:$E7"FormulasObjective
9[Sheet1!Range1]F9: =rngname(Range1)[Range1]
10[Sheet1!Range2]F10: =rngname(Range2)[Range1]
11[]F11: =rngname(RangeR)[RangeR]
12[]F12: =rngname(RangeR)[RangeR]

<tbody>
</tbody>

Is there an expression that I can use in VBA that will return the values in H9:H12?

Sorry for the confusion earlier. I'm struggling to get my head around ranges and Excel's range notation.
 
Upvote 0
See if this does what you need

Code:
Function GetRngName(r As Range) As String
    Dim n As Name, sFor As String, pos As Long, str2 As String
    
    sFor = "=" & r.Parent.Name & "!" & r.Address
    For Each n In ThisWorkbook.Names
        pos = InStr(Application.ConvertFormula(n.RefersTo, xlA1, xlA1, xlAbsolute), "]")
        str2 = "=" & Replace(Mid(Application.ConvertFormula(n.RefersTo, xlA1, xlA1, xlAbsolute), pos + 1), "'", "")
        If sFor = str2 Then
            GetRngName = "[" & n.Name & "]"
            Exit Function
        End If
    Next n
End Function

Sheet1

C
D
E
F
6
a​
b​
c​
7
d​
e​
f​
8
1​
2​
3​
9
[Range1]​
10
[Range2]​
11
[RangeR]​

Range1 = $C$6:$E$6
Range2 = $C$7:$E$7
RangeR = $C8:$E8 (relative row)

F9
=GetRngName(C6:E6)

F10
=GetRngName(C7:E7)

F11
=GetRngName(C11:E11)
Since the range has been defined with relative row, you must enter the funtion's argument this way, that is: Formula in row x use Cx:Ex

M.
 
Upvote 0
EDIT
Range1 = Sheet1!$C$6:$E$6
Range2 = Sheet1!$C$7:$E$7
RangeR = Sheet1!$C8:$E8 (relative row)

M.
 
Upvote 0
See if this does what you need

I think I did exactly what you suggest. For me, the function exits on the first execution of the For statement before any iterations.

Is this function searching all named ranges in the entire workbook for an exact match on the passed range? If so, it's probably not what I need. I only want to check the calling sheet.

But there must be a simpler way.

This code works for all absolute ranges ($C$R:$C$R). It just doesn't work for relative ranges ($CR:$CR).

Code:
Public Function RngName(rng As Range) As Variant
RngName = rng.Name.Name
End Function

Isn't there some similarly simple statement that will work for all ranges?
 
Upvote 0
I think I did exactly what you suggest. For me, the function exits on the first execution of the For statement before any iterations.

I tested many times and the function worked perfectly.
I don't know why is not working for you

M.
 
Upvote 0
A relative range is a named formula, not a named range per se.
 
Upvote 0

Forum statistics

Threads
1,214,901
Messages
6,122,157
Members
449,068
Latest member
shiz11713

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