![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Feb 2002
Posts: 2
|
I'm struggling to find, through VBA, how one can find out if a cell is within a range, and then return that range name. For example if cell A1 is within a larger range of "A1:A20" named "RANGE1", how do you retrieve that name through code?
|
|
|
|
|
|
#2 |
|
Legend
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
|
Something like EDC's test work here?
Function InRange(Range1 As Range, Range2 As Range) As Boolean ' returns True if Range1 is within Range2 Dim InterSectRange As Range Set InterSectRange = Application.Intersect(Range1, Range2) InRange = Not InterSectRange Is Nothing Set InterSectRange = Nothing End Function Sub TestInRange() If InRange(ActiveCell, Range("A1:D100")) Then ' code to handle that the active cell is within the right range MsgBox "Active Cell In Range!" Else ' code to handle that the active cell is not within the right range MsgBox "Active Cell NOT In Range!" End If End Sub HTH. Cheers, Nate |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Try something like this:
Code:
Sub RangeNames()
Dim nm As Name
For Each nm In ActiveWorkbook.Names
If Not Intersect(Range("C3"), nm.RefersToRange) Is Nothing Then
MsgBox nm.Name
End If
Next
End Sub
Russell |
|
|
|
|
|
#4 |
|
New Member
Join Date: Feb 2002
Posts: 2
|
I appreciate your fast reply, but I think the code you gave me assumes I know the name of the range I'm trying to find. My problem is that I need to retrieve the name of the range that the cell is located in. I know the cell will be contained in a range. I just don't know which one.
|
|
|
|
|
|
#5 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|