Get Named Range Name if cell is known

reneuend

Board Regular
Joined
May 20, 2009
Messages
155
I know the exact cell, but I can't get VBA to return the named range that is defined for this cell. I've seem examples on other posts for doing this, but I'm getting a 1004 error: "Application-defined or Object-defined error".

I wrote code to select the cell and then ran this code.

Code:
    For Each nm In ThisWorkbook.Names
        Set rng = Intersect(ActiveCell, Range(nm.RefersToRange.Address))
        If Not rng Is Nothing Then
            MsgBox ActiveCell.Address & " is in the named range " & nm.Name
            Exit For
        End If
    Next nm

The error occurs on the line "set rng = Intersect(..........).

Any ideas on how to get the named range for a specified cell would be greatly appreciated.

Thanks!!!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Not all named ranges actually refer to a range..
Therefor may not have an address
Causing this to error
Range(nm.RefersToRange.Address)

Use On Error Resume next..

On Error Resume Next
Set rng = Intersect(ActiveCell, Range(nm.RefersToRange.Address))
On Error Goto 0


Hope that helps.
 
Upvote 0
Seems to work for me fine
Check that rng is a range if previously declared
also depending on which version of Excel you're using the scope of a Named range can be sheet or workbook

are all you're REFERENCES ok?
 
Upvote 0
Thanks CCC. all my references seem to be fine.
nm is defined as a Name type

I can't seem to find a way to determine what reference is causing an error. I'll do some more debugging to see if I can figure it out.

Anyway, using Jonmo's resume on error seems to be working.
 
Last edited:
Upvote 0
Try this crude message box..

Code:
Dim rng As Range
For Each nm In ThisWorkbook.Names
    On Error Resume Next
    Set rng = Intersect(ActiveCell, Range(nm.RefersToRange.Address))
    If Err.Number = 1004 Then MsgBox "The range named " & nm.Name & " is causing an error"
    On Error GoTo 0
    If Not rng Is Nothing Then
        MsgBox ActiveCell.Address & " is in the named range " & nm.Name
        Exit For
    End If
Next nm
 
Upvote 0
I was wrong CCC! :oops:

Using JonMo's advice, I put in the condition to see what was causing the error. There was a bad reference!

Thanks guys for taking the time to help me out!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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