Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default How to get name of named range where activecell is

All,

Lots of people want to know if activecell is part of named range.

My question is a bit different; How to get name of named range where
activecell is.

Looking through Range object, but, mmmm, cannot find any.

Can anyone know how to do this?
---
Tetsuya, Singapore
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default How to get name of named range where activecell is

Hi,
same question innit??

From Chip Pearsons site....

http://www.cpearson.com/excel/excelM.htm

Public Function CellInNamedRange(Rng As Range) As String

Dim N As Name
Dim C As Range
Dim TestRng As Range
On Error Resume Next

For Each N In ActiveWorkbook.Names
Set C = Nothing
Set TestRng = N.RefersToRange
Set C = Application.Intersect(TestRng, Rng)
If Not C Is Nothing Then
CellInNamedRange = N.Name
Exit Function
End If
Next N
CellInNamedRange = ""

End Function


Public Sub SelectRange()

Dim RngName As String
Dim R As Range
Set R = ActiveCell
Dim Msg As String

Msg = "Active Cell Is Not In A Named Range."
RngName = CellInNamedRange(R)
If RngName < "" Then
Range(RngName).Select
Msg = "Range: " + RngName + " Selected."
End If

Application.StatusBar = Msg

End Sub


Hth,
OJ

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default How to get name of named range where activecell is

Tetsuya

One way:

Sub GetName()
'Leo Heuser April 8, 2005
Dim Nam As Name

For Each Nam In ActiveWorkbook.Names
If Not Intersect(ActiveCell, Range(Nam.RefersTo)) Is Nothing Then
MsgBox Nam.Name
End If
Next Nam

End Sub


--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"Tetsuya Oguma" <Tetsuya skrev i en
meddelelse ...
All,

Lots of people want to know if activecell is part of named range.

My question is a bit different; How to get name of named range where
activecell is.

Looking through Range object, but, mmmm, cannot find any.

Can anyone know how to do this?
---
Tetsuya, Singapore



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default How to get name of named range where activecell is



and yet another (more elaborate?) way:

:Uses NameLocal
:Gets dynamic ranges too...


Option Explicit


Function GetRangeName(oRange As Range, Optional bExpand As Boolean) As
String
Dim rg As Range, nm As Name

On Error Resume Next
Set nm = oRange.Name
If Not nm Is Nothing Then
GetRangeName = nm.NameLocal
End If
If bExpand Then
'part of larger named range?
'(also dynamic names with index of offset formulas)
For Each nm In oRange.Parent.Parent.Names
Set rg = Range(nm.Name)
If Not rg Is Nothing Then
If Not Intersect(oRange, rg) Is Nothing Then
GetRangeName = GetRangeName & ";" & nm.NameLocal
End If
End If
Next
If Left$(GetRangeName, 1) = ";" Then
GetRangeName = Mid$(GetRangeName, 2)
End If
End If

End Function

Sub Demo()
MsgBox "Activecell (not expanded)" & vbTab & _
GetRangeName(ActiveCell, False) & vbLf & _
"Activecell (expanded) " & vbTab & _
GetRangeName(ActiveCell, True) & vbLf & _
"Selection (not expanded)" & vbTab & _
GetRangeName(ActiveWindow.RangeSelection, False) & vbLf & _
"Selection (expanded) " & vbTab & _
GetRangeName(ActiveWindow.RangeSelection, True)
End Sub




--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Tetsuya Oguma wrote :

All,

Lots of people want to know if activecell is part of named range.

My question is a bit different; How to get name of named range where
activecell is.

Looking through Range object, but, mmmm, cannot find any.

Can anyone know how to do this?
---
Tetsuya, Singapore

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default How to get name of named range where activecell is

Leo,

your code will produce unhandled errors if book contains names with
either constants or formulas

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Leo Heuser wrote :

Tetsuya

One way:

Sub GetName()
'Leo Heuser April 8, 2005
Dim Nam As Name

For Each Nam In ActiveWorkbook.Names
If Not Intersect(ActiveCell, Range(Nam.RefersTo)) Is Nothing
Then MsgBox Nam.Name
End If
Next Nam

End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default How to get name of named range where activecell is

You're right. I "forgot" a line with
On Error Resume Next

Thanks!

LeoH


"keepITcool" skrev i en meddelelse
.com...
Leo,

your code will produce unhandled errors if book contains names with
either constants or formulas

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Leo Heuser wrote :

Tetsuya

One way:

Sub GetName()
'Leo Heuser April 8, 2005
Dim Nam As Name

For Each Nam In ActiveWorkbook.Names
If Not Intersect(ActiveCell, Range(Nam.RefersTo)) Is Nothing
Then MsgBox Nam.Name
End If
Next Nam

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActiveCell in a Range? Filo Excel Discussion (Misc queries) 3 May 22nd 07 09:52 PM
inserting a named range into new cells based on a named cell Peter S. Excel Discussion (Misc queries) 1 June 4th 06 03:53 AM
Name of range containing ActiveCell? Ed Excel Programming 13 January 4th 05 12:55 AM
Copy named range contents to activecell position Neal Excel Programming 2 October 28th 04 02:20 PM
Finding if the activecell is withing a named range Seamus Conlon Excel Programming 8 August 28th 04 12:10 AM


All times are GMT +1. The time now is 06:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"