Is range named?

doofusboy

Well-known Member
Joined
Oct 14, 2003
Messages
1,325
I know I've done this before but can not for the life of me remeber the syntax I used. Writing an If statement and need to first determine if the target cell is a named range.

If Target.HasName kinda thing.

Any suggestions?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I'm sure there must be a better way to do this, and when a more knowledgeable person steps in and gives you the one line answer, I'll have to post the redfaced icon, but, in the meantime, this was the best I was able to come up with:
Code:
Function test4Name(target As Range) As Boolean
    Dim n As Name, fullRef$
 
    fullRef = "='" & target.Parent.Name & "'!" & target.Address
 
    For Each n In ActiveWorkbook.Names
        If n = fullRef Then
            test4Name = True
            Exit Function
        End If
    Next n
End Function
This will tell you if the target range IS a named range. If you need to find out if it's part of a named range, or if it intersects a named range, then we'll need to tweak it.
 
Upvote 0
Upon further testing, I've found that I have to make the single quotes conditional, so here's a revised version:
Code:
Function test4Name(target As Range) As Boolean
    'Test if target is a Named Range.
    Dim n As Name, fullRef$, quote$
 
    With target
        quote = IIf(UBound(Split(.Parent.Name)), "'", "")
        fullRef = "=" & quote & .Parent.Name & quote & "!" & .Address
    End With
 
    For Each n In ActiveWorkbook.Names
        If n = fullRef Then
            test4Name = True
            Exit Function
        End If
    Next n
End Function
 
Upvote 0
A much shorter function :

Code:
Function test4Name(Target As Range) As Boolean

    On Error Resume Next
    
    test4Name = ObjPtr(Target.Name)

End Function
 
Upvote 0
Hey! No fair! ObjPtr() is undocumented, unsupported, and is making me look bad! It's also no longer available in VB.net. Since MS seems intent on dumping it, it doesn't look like a long-term solution.
(Did I hear someone mention XLM? Okay, fine, but, still - you never know when they're going to pull the rug out from under you...)

That rant out of the way...COOL! I like it! :cool:

I tested it, and it works, but please explain to me why it works. Target.Name returns the range, for example, Range("C7:C12").Name returns: =Sheet1!$C$7:$C$12, so how does feeding it to ObjPtr() test if it's a Named Range?
 
Upvote 0
I know I've done this before but can not for the life of me remeber the syntax I used. Writing an If statement and need to first determine if the target cell is a named range.

If Target.HasName kinda thing.

Any suggestions?


lookup Intersect


Rich (BB code):
<CODE>Worksheets("Sheet1").ActivateSet isect = Application.Intersect(Range("rg1"), Range("rg2"))If isect Is Nothing Then    MsgBox "Ranges do not intersect"Else    isect.SelectEnd If</CODE></PRE>
<CODE>
</CODE></PRE>
 
Last edited:
Upvote 0
Charles,

I think you might not have grasped doofusboy's question.
Doofusboy wants to determine if the target cell is a Named Range.
He doesn't know in advance which Named Range it might be, so Intersect isn't going to help.

As an aside, as of Excel 2007, your sample ranges "rg1" and "rg2" aren't legitimate named ranges anymore, because they're addresses.
 
Upvote 0
Example taken for the microsoft help system as a pointer to helping self

Does Doofusboy want to know the name of every Named range a cell is in?
 
Upvote 0
Since he hasn't come back to clarify, I can only assume, given the wording of his question, that he wants to know if the target is a Named Range, it's a yes or no question. My longwinded function (see above), provides the answer, but Jaafar's concise & efficient function is, by far, the better way to go.
 
Upvote 0
Yes UniMord you've correctly interpreted what I was after, and yes, Jaafar's ObjPtr(Target.Name) does work [although I'm not sure why/how either].

Thanks all for your efforts.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

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