PDA

View Full Version : Solved: Find named range that activecell is in



Kippers
01-07-2007, 09:29 PM
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?

acw
01-07-2007, 10:36 PM
Hi

This bit of code may help.


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


Tony

Bob Phillips
01-08-2007, 07:11 AM
You might need some error handling



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

Kippers
01-09-2007, 01:44 PM
Excellent these solve the problem - thanks