17

Is there a way of displaying the cell name of a particular cell in another cell? I would like to display the cell name in the adjacent cell so that the user is able to identify the cell name without clicking it.

12 Answers 12

10

This function would give the name of the NamedRange the cell belongs to:

Public Function CellName(oCell As Range) As Variant  
Dim oName As Name  
For Each oName In ThisWorkbook.Names  
    If oName.RefersToRange.Parent Is oCell.Parent Then  
        If Not Intersect(oCell, oName.RefersToRange) Is Nothing Then  
           CellName = oName.Name  
           Exit Function  
        End If  
    End If  
Next  
CellName = CVErr(xlErrNA)  
End Function

It loops through all the names in the workbook, and then for each name it checks if it refers to any thing in this the sheet input parameter is from. If it is then it checks if the input cell and the rages referred by the name intersect. If they do it returns the name of the range.

3
  • 1
    As I use your function, and referring to my question it might be useful to add Application.Volatile just after the function declaration.
    – Voitcus
    May 17, 2013 at 9:19
  • I didn't know about this setting at all.. Learning for the day!!
    – Adarsha
    Jul 8, 2014 at 11:16
  • I used this code, it works well until the range is covered by multiple names, so I added On Error Resume Next: If oCell.Name.RefersTo = oName.RefersToRange.Name Then Exit Function Plus I also moved the default to before the loop Oct 22, 2021 at 18:48
9

ADDRESS(ROW(),COLUMN()) will give you the address, e.g. $A$1 of the current cell. Add/subtract from the row/column values (numbers) to reference the cell you are after.

If you don't want the $ then you can find and replace it with SUBSTITUTE(ADDRESS(ROW(),COLUMN()),"$","") and get just A1 for example

1
  • awesome, excactly what I was looking for +1
    – gsharp
    Feb 18, 2014 at 8:55
8

In Excel 2013, and maybe in some older versions too, ADDRESS() accepts third parameter that defines the format of the address to be returned with following values:

1 - Absolute (default)

2 - Absolute row/Relative column

3 - Relative row/Absolute column

4 - Relative

so lets say in cell A1

ADDRESS(ROW();COLUMN()) //outputs $A$1
ADDRESS(ROW();COLUMN();1) //outputs $A$1
ADDRESS(ROW();COLUMN();2) //outputs A$1
ADDRESS(ROW();COLUMN();3) //outputs $A1
ADDRESS(ROW();COLUMN();4) //outputs A1

If you want to display the name of cell D3 in cell A1 type:

ADDERSS(ROW(D3);COLUMN(D3);4) //outputs text D3 in cell A1
2

Technically, you could combine the SUBSTITUTE() and new FORMULATEXT() functions to do this, if you don't mind adding a hidden column in your sheet.

Assume cell A1 is named FOO Add a simple reference formula in B1 to the named cell =FOO Add a formula in C1 =SUBSTITUTE(FORMULATEXT(B2),"=","") Hide Column B

Cell C1 will contain the value FOO (or the RC reference for unnamed cells)

Adjust as needed, your mileage (or kilometerage) may vary.

1
  • I can't thank you enough for this. It's always been absolute hell dealing with invisible variables.
    – typo
    Nov 26, 2020 at 2:20
1

It doesn't seem to be possible, which is weird. You'd think that the cell() function should provide a way to get the name, but it doesn't. Bummer.

3
  • What if the name referenced a range? Or a formula (OFFSET, say) that happened to include the cell sometimes, but sometimes not? I think MS just declared the problem to be "too hard" and got on with something easier. ;-) Apr 9, 2009 at 9:04
  • Good point ... I guess I'd expect to get the name that "covers" the cell, if the cell is in a named range. There could of course be other functions to help resolve any such issues, I'm surprised there aren't.
    – unwind
    Apr 9, 2009 at 9:11
  • I suspect it's one of the areas where 80% don't know about the feature and of those that do, only 20% care. So there's not much pressure to come up with a solution. Much better to redesign the UI. Apr 9, 2009 at 9:21
1
ActiveWorkbook.Sheets.Item(1).Cells(row, col).Name.Name
1

I took some 'advice' from the answer above from Adarsha. I got a similar result to the code below, with that loop and a few refinements. However my 'tip for excel' macros is to make your debugger your best friend.

Function name_of(clls)
'
    name_of = ""
' 
    Dim  nam As String
    Dim  rg  As Range
    Set  rg = clls
'
    nam = rg.Name.Name
'
    name_of = nam
'
End Function 'name_of

A little bit of patience and perseverance, gave me exactly what I was looking for -- A user defined function to give me the defined name of a cell. What happens if the same Cell has more than one name? Try that and see. It is a simple test, so I want to leave that for you to experiment, learn and pass on your new knowledge.

1
  • I'm disappointed. That appears to work ONLY when a cell has-a name defined. It does appear the only reliable way to do this is to scan all the names, and return the first or last match.
    – will
    Nov 21, 2011 at 21:01
1

You can use =CELL("address",K6)

will give you $k$6

0

This will work in very basic circumstances:

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

It won't work if the cell is part of a named range, it won't show multiple names for the cell, it won't work for cells included in named formulae (like =OFFSET() ranges, for example).

The

"=" & cel.Parent.Name & "!" & cel.Address

thing is pretty clunky, too. There may be a better way to do the check. Creating a Range object from the RefersTo and using Intersect() might work.

0

Excel does have a function "Cell()" that you can get certain properties from.

You can use =Cell("row", K9) and get back row number 9 and there's an equivalent "col" parameter but it returns the column number (11) rather than the letter.

-1

Reference the named cell in another cell, E12 in this case, and then use this formula: ="'"&FORMULATEXT(E12). This puts an apostrophe in front so it will show the name or formula as text.

-1

You can place your cursor in an empty cell, type = then click on the named cell. It will display the cell contents. Then you change the format of the cell to text and it will show =

1
  • Doesn't work as I try - just shows the result of evaluating the formula, albeit left-aligned.
    – Stewart
    Sep 20, 2018 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.