excelvbanamed-ranges

How can I refer to specific cell within non contiguous named range?


I am trying to create a new named range with reference to a specific cell in a non-contiguous named range. Please see the example picture:

enter image description here

Named_range consists of two ranges, K4:K7 and L8:L10. I cannot find a way to refer to specific cells, for example cells 4 (K7) and 6 (L9). I have tried to use index, =index(Named_range,4) but it works only for cell 4. For cell 6 it refers K9 instead of L9.

The desired ending is something like this:

ThisWorkbook.Names.Add Name:="Named_range_cell_6", RefersTo:=Index("Named_range", 6)

I cannot find a way to create a named range with reference to already existing Named_range, like in the "INDEX(Named_range,6)" attempt. Things like Range("Named range").Item(6) do exactly the same thing, refer to things within one row or column. I want to refer to THAT one specific cell based on its index regardless if it's contiguous or non-contiguous range.

Is there any way to achieve what I am trying to do?


Solution

  • If the range is severely disjoint, you would need a loop:

    Sub luxation()
        Dim i As Long, r As Range
    
        Union(Range("K4:K7"), Range("L8:L10")).Name = "Named_range"
        i = 1
        For Each r In Range("Named_range")
            If i = 6 Then
                r.Name = "Named_range_6"
            End If
            i = i + 1
        Next r
    
        MsgBox Range("Named_range_6").Address(0, 0)
    End Sub
    

    enter image description here