+ Reply to Thread
Results 1 to 6 of 6

Cell in named range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-03-2009
    Location
    Florida
    MS-Off Ver
    Excel 365
    Posts
    295

    Cell in named range

    Is there a way to determine if a cell is part of one or more named ranges?
    Thanks

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,933

    Re: Cell in named range

    One possible way would be to use VBA. The example code below will loop through the named ranges and see if the selected cell appears anywhere within a named range and will throw up a message box with the name of the range if it does.
    You could change Selection to a specific range if that would work better. Not much info to go on...

    Sub NamedRangeCheck()
        Dim n As Name
        
        For Each n In ThisWorkbook.Names
            If Not Intersect(n.RefersToRange, Selection) Is Nothing Then
                MsgBox n.Name
            End If
        Next
    End Sub
    BSB

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Cell in named range

    Hi there,

    Take a look at the attached workbook and see if it does what you need. It uses the following code:

    
    
    
    Option Explicit
    
    
    Sub CheckCell()
    
        Dim vaRangeNames    As Variant
        Dim sCellAddress    As String
        Dim sRangeNames     As String
        Dim rActiveCell     As Range
        Dim iRangeNo        As Integer
        Dim wks             As Worksheet
    
        If TypeOf Selection Is Range Then
    
              If Selection.Cells.CountLarge = 1 Then
    
                    vaRangeNames = Array("ptrRange_A", "ptrRange_B", "ptrRange_C")
    
                    Set rActiveCell = ActiveCell
                    Set wks = ActiveSheet
                    sCellAddress = rActiveCell.Address(ColumnAbsolute:=False, _
                                                       RowAbsolute:=False)
    
                    sRangeNames = vbNullString
    
                    For iRangeNo = LBound(vaRangeNames) To UBound(vaRangeNames)
    
                        If Not Intersect(rActiveCell, _
                                         wks.Range(vaRangeNames(iRangeNo))) Is Nothing Then
    
                            sRangeNames = sRangeNames & vbLf & vbTab & vaRangeNames(iRangeNo)
    
                        End If
    
                    Next iRangeNo
    
                    If sRangeNames <> vbNullString Then
    
                          MsgBox "Cell " & sCellAddress & " intersects with the " & _
                                 "following named range(s):" & vbLf & sRangeNames, _
                                  vbInformation
    
    
                    Else: MsgBox "Cell " & sCellAddress & " does not intersect with " & _
                                 "any of the specified named ranges", vbInformation
    
                    End If
    
              Else: MsgBox "Please select a single cell before using this feature", vbExclamation
    
              End If
    
        Else: MsgBox "Please select a cell before using this feature", vbExclamation
    
        End If
    
    End Sub
    The highlighted values may be altered to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Cell in named range

    Hi again,

    Following on from BSB's approach, the attached workbook will check for intersections with ALL of the named ranges contained in a workbook. It uses the following code:

    
    
    
    Option Explicit
    
    
    Sub CheckCell()
    
        Dim sCellAddress    As String
        Dim sRangeNames     As String
        Dim rActiveCell     As Range
        Dim wks             As Worksheet
        Dim nam             As Name
    
        If TypeOf Selection Is Range Then
    
              If Selection.Cells.CountLarge = 1 Then
    
                    Set rActiveCell = ActiveCell
                    Set wks = ActiveSheet
                    sCellAddress = rActiveCell.Address(ColumnAbsolute:=False, _
                                                       RowAbsolute:=False)
    
                    sRangeNames = vbNullString
    
                    For Each nam In ThisWorkbook.Names
    
                        If Not Intersect(rActiveCell, _
                                         wks.Range(nam.RefersTo)) Is Nothing Then
    
                            sRangeNames = sRangeNames & vbLf & vbTab & nam.Name
    
                        End If
    
                    Next nam
    
                    If sRangeNames <> vbNullString Then
    
                          MsgBox "Cell " & sCellAddress & " intersects with the " & _
                                 "following named range(s):" & vbLf & sRangeNames, _
                                  vbInformation
    
    
                    Else: MsgBox "Cell " & sCellAddress & " does not intersect with " & _
                                 "any of the specified named ranges", vbInformation
    
                    End If
    
              Else: MsgBox "Please select a single cell before using this feature", vbExclamation
    
              End If
    
        Else: MsgBox "Please select a cell before using this feature", vbExclamation
    
        End If
    
    End Sub

    Hope this helps,

    Regards,

    Greg M
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    11-03-2009
    Location
    Florida
    MS-Off Ver
    Excel 365
    Posts
    295

    Re: Cell in named range

    Thank you all, I appreciate it.

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Cell in named range

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 10-28-2019, 04:14 PM
  2. Listbox displaying named range B but Adding named range A to cell
    By ikkenieikke in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-05-2018, 02:27 PM
  3. [SOLVED] Assign named range to one cell depending on named range in another
    By x65140 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2015, 11:04 AM
  4. [SOLVED] determining if cell is part of named range and what that named range is
    By stnkynts in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-16-2014, 07:56 PM
  5. [SOLVED] Determining if the value of a cell can be a named range, then assigning named ranges after
    By Romulo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2013, 06:05 PM
  6. Replies: 1
    Last Post: 06-03-2006, 10:55 PM
  7. [SOLVED] If any cell in named range = 8 then shade named range
    By JJ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-26-2005, 07:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1