Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Given a cell, find the named range it belongs to

Hi,

how can I reliably and efficiently determine if a cell "is
a member of" a named range, i.e. if it is part of a named
range?

It should work looping trough all Names, determine if the
name refers to a range, and if so loop through the cells
of the range and compare with the "pivot" (the given cell)
using the Is operator. If a match is found, return the
named range (i.e. stop looping immediately); if loop
terminates, return Nothing.

But is there a better way? It seems to me if there are
lots of/huge named ranges in a workbook this method might
be a little slow.

Any ideas?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Given a cell, find the named range it belongs to

Hi again,

I found what I was looking for. To anyone else in need of
the same or something similar: The Range has a property
called CurrentRegion for this purpose.

Function GetNamedRange(subRange As Range) As Range
On Error Goto NOT_IN_NAMED_RANGE
Set GetNamedRange = subRange.CurrentRegion.Name
Exit Function
NOT_IN_NAMED_RANGE:
Set GetNamedRange = Nothing
End Function

Example: The range [A1:C5] has been named "myRange". Thus
GetNamedRange( Range("A3") ) returns the "myRange" range
[A1:C5]. Quite useful!

Happy coding!

Dag
-----Original Message-----
Hi,

how can I reliably and efficiently determine if a

cell "is
a member of" a named range, i.e. if it is part of a named
range?

It should work looping trough all Names, determine if the
name refers to a range, and if so loop through the cells
of the range and compare with the "pivot" (the given

cell)
using the Is operator. If a match is found, return the
named range (i.e. stop looping immediately); if loop
terminates, return Nothing.

But is there a better way? It seems to me if there are
lots of/huge named ranges in a workbook this method might
be a little slow.

Any ideas?

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Given a cell, find the named range it belongs to

While this may work for your special case, it certianly doesn't do what you
described in your original post. Also, you are doing some implicit
conversions

? Range("A1").CurrentRegion.address
$A$1:$C$5
? Range("A1").CurrentRegion.name
=Sheet1!$A$1:$C$5
? Range("A1").CurrentRegion.name.name
MyRange


--
Regards,
Tom Ogilvy

"Dag Johansen" wrote in message
...
Hi again,

I found what I was looking for. To anyone else in need of
the same or something similar: The Range has a property
called CurrentRegion for this purpose.

Function GetNamedRange(subRange As Range) As Range
On Error Goto NOT_IN_NAMED_RANGE
Set GetNamedRange = subRange.CurrentRegion.Name
Exit Function
NOT_IN_NAMED_RANGE:
Set GetNamedRange = Nothing
End Function

Example: The range [A1:C5] has been named "myRange". Thus
GetNamedRange( Range("A3") ) returns the "myRange" range
[A1:C5]. Quite useful!

Happy coding!

Dag
-----Original Message-----
Hi,

how can I reliably and efficiently determine if a

cell "is
a member of" a named range, i.e. if it is part of a named
range?

It should work looping trough all Names, determine if the
name refers to a range, and if so loop through the cells
of the range and compare with the "pivot" (the given

cell)
using the Is operator. If a match is found, return the
named range (i.e. stop looping immediately); if loop
terminates, return Nothing.

But is there a better way? It seems to me if there are
lots of/huge named ranges in a workbook this method might
be a little slow.

Any ideas?

.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Given a cell, find the named range it belongs to

Hi Dag,

The following I found in the VBA help system seems to
address your need:
"
Example
This example selects the intersection of two named ranges,
rg1 and rg2, on Sheet1. If the ranges don't intersect, the
example displays a message.

Worksheets("Sheet1").Activate
Set isect = Application.Intersect(Range("rg1"), Range
("rg2"))
If isect Is Nothing Then
MsgBox "Ranges do not intersect"
Else
isect.Select
End If
"

Best Regards,
Walt

-----Original Message-----
Hi again,

I found what I was looking for. To anyone else in need of
the same or something similar: The Range has a property
called CurrentRegion for this purpose.

Function GetNamedRange(subRange As Range) As Range
On Error Goto NOT_IN_NAMED_RANGE
Set GetNamedRange = subRange.CurrentRegion.Name
Exit Function
NOT_IN_NAMED_RANGE:
Set GetNamedRange = Nothing
End Function

Example: The range [A1:C5] has been named "myRange". Thus
GetNamedRange( Range("A3") ) returns the "myRange" range
[A1:C5]. Quite useful!

Happy coding!

Dag
-----Original Message-----
Hi,

how can I reliably and efficiently determine if a

cell "is
a member of" a named range, i.e. if it is part of a

named
range?

It should work looping trough all Names, determine if

the
name refers to a range, and if so loop through the cells
of the range and compare with the "pivot" (the given

cell)
using the Is operator. If a match is found, return the
named range (i.e. stop looping immediately); if loop
terminates, return Nothing.

But is there a better way? It seems to me if there are
lots of/huge named ranges in a workbook this method

might
be a little slow.

Any ideas?

.

.

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
Find in Named Range problem (2nd Try) G.R. New Users to Excel 2 February 27th 08 08:17 PM
Find Row in a named range Raul Excel Worksheet Functions 3 November 24th 07 12:41 AM
Find Cell in Named Range David Excel Worksheet Functions 3 May 8th 07 06:00 AM
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
How to find all formulas that used a certain named range Laurence Lombard Excel Discussion (Misc queries) 2 October 25th 05 08:15 AM


All times are GMT +1. The time now is 07:53 PM.

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

About Us

"It's about Microsoft Excel"