Consulting

Results 1 to 4 of 4

Thread: Solved: Find named range that activecell is in

  1. #1
    VBAX Regular
    Joined
    Nov 2006
    Posts
    7
    Location

    Solved: Find named range that activecell is in

    I have 9 named ranges that do not overlap
    i.e.
    Named range
    If i select a cell how can i get the named range that it is in?

  2. #2
    Hi

    This bit of code may help.

    [VBA]
    Sub aaa()
    For i = 1 To Names.Count
    If Not Intersect(ActiveCell, Range(Names(i))) Is Nothing Then
    MsgBox Names(i).Name
    End If
    Next i
    End Sub
    [/VBA]

    Tony

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,455
    Location
    You might need some error handling

    [vba]

    Public Function CellsName(rng As Range) As String
    Dim sh As Worksheet
    Dim refRange As Range
    Dim nme As Name
    CellsName = ""
    On Error GoTo ErrHandler
    For Each nme In ThisWorkbook.Names
    Set refRange = Nothing
    On Error Resume Next
    Set refRange = nme.RefersToRange
    On Error GoTo ErrHandler
    If Not refRange Is Nothing Then
    If rng.Parent.Name = refRange.Parent.Name Then
    If Not Intersect(rng, refRange) Is Nothing Then
    CellsName = nme.Name
    Exit Function
    End If
    End If
    End If
    Next
    Exit Function
    ErrHandler:
    CellsName = "*** error ***"
    End Function
    [/vba]

  4. #4
    VBAX Regular
    Joined
    Nov 2006
    Posts
    7
    Location
    Excellent these solve the problem - thanks

Posting Permissions

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