Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Name of Range in Excel with Formula

Tags:

excel

vba

I've named a bunch of columns in Excel.

Now I would like to be able to get the name of a column in a cell using a formula.

So for example, if A:A was named Dates, I want to be able to put a formula in a cell such that =RangeName(A:A) returns the word Dates.

I've found examples of how to do this for a single cell, but not for a range of cells.

This is what I have found for a single cell.

    Public Function CellName(cel As Range) As Variant
Dim nm As Name
    For Each nm In Names
        If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Then
            CellName = nm.Name
            Exit Function
        End If
    Next
CellName = CVErr(xlErrNA)
End Function
like image 313
bpmccain Avatar asked Oct 16 '25 15:10

bpmccain


1 Answers

It sounds like a case for Intersect:

Public Function CellName(cel As Range) As Variant
    Dim nm As Name
    For Each nm In Names
        If Not Intersect(cel, nm.RefersToRange) Is Nothing Then
            CellName = nm.Name
            Exit Function
        End If
    Next
    CellName = CVErr(xlErrNA)
End Function
like image 165
Sam Avatar answered Oct 18 '25 07:10

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!