0

I have a structured table called MyTable and the column called MyColumn.

MyColumn
aaa
bbb
ccc

I can write Excel formulas acting on some parts of this table that I select, like this: =Counta([MyTable][#All]) =Counta(FeaturesTbl[#Headers])

What I want is an Excel formula that will return the structured-table text of the cells I am selecting. Like this:

= MyFormula(SomeCellsISelected) this should return: "[MyTable][#All]" if I selected the entire table including headers.

The reason why I want this formula is that this table comes from a power query step, and its name may change from time to time without the user knowing. So I want to show to users what the current table name is, as well as use it dynamically in my VBA steps.

However, I want a native Excel formula for this.

I tried looking at CELL() but so far, it does not return the structured-table reference of the target cell.

Any ideas?

1 Answer 1

3

There are few methods to get the Table name.

  1. All tables available, are listed in the Name Box.
  2. Table's names can be found in the Formula Bar also.

But the most efficient method to get all Table's name form the current Workbook is VBA macro.

How it works:

  1. Hit Alt+F11 to open the VB editor window.

  2. From VB editor's window menu, click Insert then Module.

  3. Copy & Paste below shown VBA code.

    Sub ListAllTables()
    
    Dim xTable As ListObject
    Dim xSheet As Worksheet
    Dim I As Long
    I = -1
    Sheets.Add.Name = "Table Name"
    For Each xSheet In Worksheets
        For Each xTable In xSheet.ListObjects
            I = I + 1
            Sheets("Table Name").Range("A1").Offset(I).Value = xTable.Name
        Next xTable
    Next
    End Sub
    

N.B

In this code new sheet name and cell reference for Table List (A1) are editable.

  1. Press Alt+Q to return to file.

  2. Save the Workbook as Macro Enabled.

  3. Finally RUN the Macro.

You get a new sheet named as Table Name and list of Tables.


:Edited:

If you want to get one Table name at time, using the cell references, then you may use this UDF (User Define Function).

Function GetTableName(cellInTable As Range) As String
    Dim tblName As String
    tblName = vbNullString
    On Error Resume Next
    tblName = cellInTable.ListObject.Name
    GetTableName = tblName
End Function

How it works:

  • Copy & Paste this VBA code as Module.
  • Use this formula:

=GetTableName(H28)

enter image description here

  • Where H28 is cell reference of top left most cell from where the Table starts.

Using formula to get Table's name is bit complicated as well it deal with one Table as a time, but you can use this one.

How it works:

  • In any blank cell, for example in J37, enter = sign and click the top left most cell of the Table, for example = and then click H28.

  • You get this =Table6[[#Headers],[data]].

  • Finally use this formula:

     =MID(FORMULATEXT(J37),2,FIND("[",FORMULATEXT(J37))-2)
    
  • You get the Table name.

Adjust cell references in the formula as needed.

4
  • Thanks! Formulatext is the way to go. I did not want a VBA solution.
    – Freelensia
    Commented May 24, 2020 at 4:33
  • 1
    @Freelensia,, glad to help you,, I've suggested 3 different ways, and two of it are formula based, now you may pick any one you like,,, also if you feel then you may accept this as an Answer as well up vote too ,, keep asking ☺ Commented May 24, 2020 at 5:26
  • How do I accept as Answer? Ah found it. Thanks!
    – Freelensia
    Commented May 24, 2020 at 5:29
  • @Freelensia,, thanks you have encouraged me to do better & better ☺ Commented May 24, 2020 at 5:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .