+ Reply to Thread
Results 1 to 6 of 6

Determine what Named Range the Target Address is and return Named Range Name

Hybrid View

jordan2322 Determine what Named Range... 06-18-2012, 09:47 PM
Leith Ross Re: Determine what Named... 06-18-2012, 09:50 PM
jordan2322 Re: Determine what Named... 06-18-2012, 09:59 PM
mikerickson Re: Determine what Named... 06-18-2012, 10:23 PM
jordan2322 Re: Determine what Named... 06-18-2012, 10:29 PM
MarvinP Re: Determine what Named... 06-18-2012, 10:49 PM
  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Determine what Named Range the Target Address is and return Named Range Name

    Hi Guys,

    is there a function to return the name of a named range (if it exists) of the Target.Address?

    Regards

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Determine what Named Range the Target Address is and return Named Range Name

    Hello jordan2322,

    No there is not. But one could be written using VBA.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Determine what Named Range the Target Address is and return Named Range Name

    Something like this?
    How would i modify this such that the target.address only needs to be within the named range - not match the named range address exactly.
    As the named ranges are more than a single cell this (as written in the code below) would be impossible as the user can only double click a single cell not a range.
    (note: I want this to be called using the double click event)

    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    
        
        If Target.Address = Range("Fruit").Address Then
        MsgBox (Target.Address)
        End If
        
        End Sub

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Determine what Named Range the Target Address is and return Named Range Name

    Try this

    Dim oneName As Name
    Dim foundName As Name
    
    For Each oneName In ThisWorkbook.Names
        On Error Resume Next
        If oneName.RefersToRange Is Nothing Then
            On Error GoTo 0
            Rem name is not range
        Else
            On Error GoTo 0
            If oneName.RefersToRange.Parent.Name = Target.Parent.Name Then
                If Not Application.Intersect(Target, oneName.RefersToRange) Is Nothing Then
                    Set foundName = oneName
                End If
            End If
        End If
    Next oneName
    
    If foundName Is Nothing Then
        MsgBox "not inside a named range"
    Else
        MsgBox "target is inside named range, " & foundName.Name
    End If
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Determine what Named Range the Target Address is and return Named Range Name

    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim nName As Name
        For Each nName In ThisWorkbook.Names
    
                If Not Intersect(Range(nName.Name), Target) Is Nothing Then
                    Cancel = True
                    MsgBox (nName.Name)
                    Exit For
                End If
    
        Next nName
    End Sub
    Solved using the above. Thank you Mikerickson for your reply!

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,246

    Re: Determine what Named Range the Target Address is and return Named Range Name

    What happens if the cell you click on is in more than one named range? Do you want all named ranges that contain the double clicked cell to appear?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1