Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How do I use VBA to find which named ranges a cell belongs to?

I'm tring to find code which allows me to verify which named ranges any
single cell belongs to
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default How do I use VBA to find which named ranges a cell belongs to?

Give this a try...

Sub test()
Dim nme As Name
Dim rng As Range

Set rng = Range("D4")

For Each nme In ThisWorkbook.Names
If Not Intersect(rng, nme.RefersToRange) Is Nothing Then MsgBox
nme.Name
Next nme
End Sub
--
HTH...

Jim Thomlinson


"Charis Custard" wrote:

I'm tring to find code which allows me to verify which named ranges any
single cell belongs to

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default How do I use VBA to find which named ranges a cell belongs to?

To the OP
To avoid 1004 errors, you might add code check that the two ranges are on
the same worksheet before try to intersect them.

For example
Intersect(worksheets(1).Rows(1),worksheets(2).colu mns(1)).Address

raises a 1004 error.

Another approach is to error trap. Here is some code I posted back in
October 1999 (slightly modified) that demonstrates that approach:

Sub TestName()
Dim rng As Range
definedName as Name
On Error Resume Next
For Each definedName In ThisWorkbook.Names
Set rng = Nothing
Set rng = definedName.RefersToRange
If Not rng Is Nothing Then
If Not Intersect(rng, ActiveCell) Is Nothing Then
MsgBox ActiveCell.Address & " is in a range named: " & definedName.Name
End If
End If
Next
End Sub

This has the advantage that it accounts for defined names that are not
ranges as well. (a defined name does not have to refer to a range).

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote:

Give this a try...

Sub test()
Dim nme As Name
Dim rng As Range

Set rng = Range("D4")

For Each nme In ThisWorkbook.Names
If Not Intersect(rng, nme.RefersToRange) Is Nothing Then MsgBox
nme.Name
Next nme
End Sub
--
HTH...

Jim Thomlinson


"Charis Custard" wrote:

I'm tring to find code which allows me to verify which named ranges any
single cell belongs to

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default How do I use VBA to find which named ranges a cell belongs to?

I think you need to add Dim (or the line wrapped)...

Dim definedName As Name

Otherwise it is better code in all respects.
--
HTH...

Jim Thomlinson


"Tom Ogilvy" wrote:

To the OP
To avoid 1004 errors, you might add code check that the two ranges are on
the same worksheet before try to intersect them.

For example
Intersect(worksheets(1).Rows(1),worksheets(2).colu mns(1)).Address

raises a 1004 error.

Another approach is to error trap. Here is some code I posted back in
October 1999 (slightly modified) that demonstrates that approach:

Sub TestName()
Dim rng As Range
definedName as Name
On Error Resume Next
For Each definedName In ThisWorkbook.Names
Set rng = Nothing
Set rng = definedName.RefersToRange
If Not rng Is Nothing Then
If Not Intersect(rng, ActiveCell) Is Nothing Then
MsgBox ActiveCell.Address & " is in a range named: " & definedName.Name
End If
End If
Next
End Sub

This has the advantage that it accounts for defined names that are not
ranges as well. (a defined name does not have to refer to a range).

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote:

Give this a try...

Sub test()
Dim nme As Name
Dim rng As Range

Set rng = Range("D4")

For Each nme In ThisWorkbook.Names
If Not Intersect(rng, nme.RefersToRange) Is Nothing Then MsgBox
nme.Name
Next nme
End Sub
--
HTH...

Jim Thomlinson


"Charis Custard" wrote:

I'm tring to find code which allows me to verify which named ranges any
single cell belongs to

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default How do I use VBA to find which named ranges a cell belongs to?

Thanks for the addendum.

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote:

I think you need to add Dim (or the line wrapped)...

Dim definedName As Name

Otherwise it is better code in all respects.
--
HTH...

Jim Thomlinson


"Tom Ogilvy" wrote:

To the OP
To avoid 1004 errors, you might add code check that the two ranges are on
the same worksheet before try to intersect them.

For example
Intersect(worksheets(1).Rows(1),worksheets(2).colu mns(1)).Address

raises a 1004 error.

Another approach is to error trap. Here is some code I posted back in
October 1999 (slightly modified) that demonstrates that approach:

Sub TestName()
Dim rng As Range
definedName as Name
On Error Resume Next
For Each definedName In ThisWorkbook.Names
Set rng = Nothing
Set rng = definedName.RefersToRange
If Not rng Is Nothing Then
If Not Intersect(rng, ActiveCell) Is Nothing Then
MsgBox ActiveCell.Address & " is in a range named: " & definedName.Name
End If
End If
Next
End Sub

This has the advantage that it accounts for defined names that are not
ranges as well. (a defined name does not have to refer to a range).

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote:

Give this a try...

Sub test()
Dim nme As Name
Dim rng As Range

Set rng = Range("D4")

For Each nme In ThisWorkbook.Names
If Not Intersect(rng, nme.RefersToRange) Is Nothing Then MsgBox
nme.Name
Next nme
End Sub
--
HTH...

Jim Thomlinson


"Charis Custard" wrote:

I'm tring to find code which allows me to verify which named ranges any
single cell belongs to

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
How do I find all named ranges in VB.NET? John Brock Excel Discussion (Misc queries) 5 September 2nd 05 04:39 PM
How do I find all named ranges in VB.NET? John Brock Excel Programming 5 September 2nd 05 04:39 PM
Named Cell Ranges Blackcat Excel Discussion (Misc queries) 1 December 9th 04 09:57 AM
Given a cell, find the named range it belongs to Dag Johansen[_5_] Excel Programming 3 September 27th 03 12:39 AM


All times are GMT +1. The time now is 09:56 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"