VBA - is this cell in a given range?

mbahr2

New Member
Joined
Feb 27, 2002
Messages
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?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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
 
Upvote 0
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

Hope this helps,

Russell
 
Upvote 0
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.
 
Upvote 0
On 2002-02-28 08:54, mbahr2 wrote:
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.

See my post - it should work for you.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,490
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top