10

I was wondering if there is anyway to return the name of the table using a formula?

I was working on a way to break down a few thousand addresses into there perspective column of information. ie.. #, Street, City, State, Zip-code and Phone#. The addresses are not in any consistent format that Text to Columns would work. I finally came up with the formulas to get the job done, but the are very long. In a post I found it suggested to use repeated parts of the formulas as a Defined Name. And it made it so much easier. Now Here is the problem.

A formula that has the table name "Table1" won't work in "Table2". Or any other table name. Column headers are the same for each table.

MAX(SEARCH(Table1[@State],Table1[@Origin]))

A way to return the name of the table is needed. Via formula or formula as Defined Name.

MAX(SEARCH(GetTableName[@State],GetTableName[@Origin]))

I prefer it to be a formula. I'm not sure if a VBA solution would be a correct answer to this question so I would not be able to choose it as THE answer, even if it does work. It will still be appreciated. I will ask in a separate post if I do not find a Formula Solution.

TY

I found this post that has a VBA solution, but I can't use it. I will post just so someone can maybe figure this out. Portland Runner Posted this CODE to get table name.

Function GetTableName(shtName As String) As String
    GetTableName = Worksheets(shtName).ListObjects(1).Name
End Function

In that Function I enter My Defined Name formula named "SheetName"

=MID(CELL("filename"),FIND("]",CELL("filename"))+1,100)

So I can use it like this.

=MAX(SEARCH(INDIRECT(GetTableName(SheetName)&"[@State]"),INDIRECT(GetTableName(SheetName)&"[@Origin]")))

However I still need this to be Formula Only. While I can run Macros on My PC, they will not run in the PC that has all the data.

This is the last thing I got using a UDF. Unfortunately I still cant use it. Plus It gets the first Table's name and not the actual table the cell is in. Good if that is the only table in sheet or if the first table is the table you want.

Function GetTableName() As String 
    GetTableName = Worksheets(ActiveSheet.Name).ListObjects(1).Name
End Function
Mouthpear
  • 199
  • 1
  • 3
  • 13
  • 2
    cross-posted here: http://superuser.com/questions/1064308/return-table-name-using-formula - Please do not post the same question on more than one site. – teylyn Apr 12 '16 at 00:47
  • Still VBA would be an easy solution. You could create a UDF with inputs like `=MyFunction(TableName,Field,someOtherValuesYouWant,....)`. This way you get simple "copy/paste" formulas to work with instead of endless formulas ;) – Dirk Reichel Apr 12 '16 at 00:49
  • @DirkReichel Yes I'm sure that VBA is a better and easier option, however the computer that the information is on does not belong to me and Macros are disabled due to company policy. Extracting the Data and doing it on my own PC is not an option due to privacy issues. However if you have a VBA solution I would like to see it. But in this case a formula is needed. – Mouthpear Apr 12 '16 at 10:04
  • @DirkReichel Also you do realize a function where I add the name of the table is just the same situation that I have now. The table name Is not known. the function would have to work like `ADDRESS(ROW(),COLUMN(),4,1)` returns the address of the cell. I need to return the Table Name where the Cell is located. – Mouthpear Apr 12 '16 at 10:15
  • can you tell what you want to do in detail? How to know which table you are working with? – Dirk Reichel Apr 12 '16 at 19:53
  • @DirkReichel I don't know what possible detail there is that I have not already mentioned. Is there anyway to return the name of the table using a formula? But let me put it this way. "Table1" is A1:A2, Header is [Column1]. In the one and only cell in the body range of table A2, insert a FORMULA that RETURNS the NAME of TABLE It is in. Cell value is "Table1". If the table name changes to "TableWhatever" the value in A2 is "TableWhatever". Or if the formula is used in "Table2" then A2="Table2". SO there anyway to return the name of the table using a formula? – Mouthpear Apr 13 '16 at 01:10
  • It is not possible in a way you may think of... a "table" is just a named range. having A1:B2 and B2:C3 is possible and allows B2 to be in 2 different named ranges / tables. Also the lookup would need to be backwards -> check each named range/table if the cell is inside it which is possible but not in a real useable way :( – Dirk Reichel Apr 13 '16 at 01:55
  • @DirkReichel You Said "check each named range/table if the cell is inside it which is **possible** but not in a real useable way :( " Well if you know Of such a way Present it and I will investigate if it is usable or not. Also you are wrong tables cannot overlap each other. "having A1:B2 and B2:C3 is possible and allows B2 to be in 2 different named ranges / tables" Named ranges can overlap each other and even exist inside of a table. However a table cannot overlap nor be inside another table. – Mouthpear Apr 13 '16 at 02:24
  • A table is just a bit more special then a named range, however the "intersect" is what i would use here like `=CELL("address",(Table3 B8))`. if B8 is outsite of the table it will return a `#NULL!` error. This way you could check against each table with a simple `=FIND("1",ISREF(Table1 C8)*1&ISREF(Table2 C8)*1&ISREF(Table3 C8)*1&ISREF(Table4 C8)*1)`... this will output 1 if it is in table 1 and 2 if in table 2..... with something like `INDIRECT` you can use this to build the table-name... (or use it as first parameter in `CHOOSE` and then enter the table-names one after another) – Dirk Reichel Apr 13 '16 at 02:38
  • @DirkReichel no disrespect intended, but you obviously don't understand that the Table name is Variable and is not known, so it is futile to hard code table names into the formula. The name of the tables could very well be named "Cat", "Dog", or "Whatever".... They may, at anytime, be changed to "WhatHaveYou" or "ForAllYouKnow". Not to mention addition of tables. – Mouthpear Apr 14 '16 at 09:36
  • In that case, it is not possible... as far as I know, there is no way to get a "name" without VBA... even XL4 macros can't do (but I would be happy if someone proves me to be wrong) ;) – Dirk Reichel Apr 14 '16 at 11:08
  • 1
    Well @DirkReichel be happy then. In my edit, of my original post, I wrote just such a macro. Macro provided by another person. I combined it with with a formula i already had and it makes it possible to get the Name of the table. Reduced it to this. 'Function GetTableName() As String GetTableName = Worksheets(ActiveSheet.Name).ListObjects(1).Name End Function' Unfortunately I still can't Use it. Plus it only gets the FIrst table's name , name not the actual table name you are in. but is the table is the only one then it's all good. – Mouthpear Apr 15 '16 at 20:23

6 Answers6

9

You will need two cells to get the table name. My Table Headers start in row 2 and Table Data in row 3, so I put my two formulas in cells A1 and B1, respectively.

The first cell should reference the top left header cell of your table. For me, the formula ends up reading:

=My2016Data[[#Headers],[State]]

and equates to "State".

The second cell's formula should be:

=MID(FORMULATEXT(A1),2,FIND("[",FORMULATEXT(A1))-2)

and equates to "My2016Data".

Brad Emerick
  • 91
  • 1
  • 3
  • 3
    I was surprised to find that this strategy actually works in practice, although the answer could be a little more clear. Basically, your first formula needs to be `=[[#Headers],[]]`, and the second needs to be `=MID(FORMULATEXT(),2,FIND("[",FORMULATEXT())-2)`. Because Excel automatically updates table names in formulas when the names change, this second formula will always show the table's current name, provided you don't change the first cell yourself. – Ben Seymour Mar 09 '18 at 17:36
  • 1
    Note: after changing a table name, you may need to force Excel to perform a recalculation. You can do this via _Formulas->Calculate Now_, or by opening the cell of the second formula for editing and pressing Enter. – Ben Seymour Mar 09 '18 at 18:04
  • This will not work. The table name is variable and i will not know the name. I was trying to write a formula that would be entered a computer that has macros disabled. The PC is not mine nor does it belong to my friend who uses it. It belongs to the company he works for. I (he) needs the formula to work in whatever table it happens to be in. – Mouthpear Nov 30 '18 at 09:20
6

Here's a VBA solution since you said you want to see it. This is a UDF (user defined function) which you create with VBA but use as a formula inside a cell. Save this code in a standard code module (not in a sheet module and not in the "ThisWorkbook" module):

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

Once saved to a module, you can use it in a cell formula like this:

=GetTableName(A1)

or this

=GetTableName(B:B)

or this

=GetTableName(B2:W900)

If you use a range that overlaps more than one table it will return the name of the first table.

ChrisB
  • 3,024
  • 5
  • 35
  • 61
  • 1
    Thank you. I have tried it and it works. I still can't use it for the original project I was working on.The computer with the information is a company computer and the use of macros or VBA was disabled. But I did find use for your UDF. I would really like to Check yours as the answer but I'm not sure if it is allowed since the question was about a formula solution. I wonder if the Moderators would allow it. Thank you very much either way. – Mouthpear Jun 13 '17 at 09:41
2

In a single cell, you can use this =TableName() formula to return a table name as text. For example, =TableName(TblAccounts) returns "TblAccounts" and updates as the table's name is updated!

With the introduction of the LAMBDA function in Excel, returning table name can be achieved with this function in the Name Manager:

Formula

=LAMBDA(reference, LET(thisFormula,FORMULATEXT(INDIRECT(ADDRESS(ROW(),COLUMN()))),
TEXTBEFORE(TEXTAFTER(thisFormula,"("),IF(ISERROR(FIND("[",ThisFormula)),")","["))))

Create a Name in the Name Manager (e.g., called "TableName") with the above formula in the "Refers to:" field. This custom function then takes a reference to the table and returns the table's name. This can use any table reference that includes the table name, as @Marat points out:

You can use any part of the table, not only Table1[#Headers]. For example, it could be: Table1[#All] or Table1[Column1] or Table1[[#Headers],[Column1]].

This solution offers broader compatibility for using this functionality in Excel Online web version, where VBA does not work.

If this formula does not automatically update after changing a table name, click "Calculate Now" under the Formulas tab on the ribbon to get the value to update. It is dynamic and will return whatever you name your tables, just as Excel changes the names in every other formula referencing a table!

How to use

After entering this formula in the Name Manager as TableName, type

=TableName(x)

where x is a reference that includes the name of the table. E.g., if my table is called "TblAccounts", x could be "TblAccounts", "TblAccounts[#Headers]", "TblAccounts[@Colulmn]", etc.

Explanation

Here is the code from above with indentation:

=LAMBDA(reference,
    LET(
        thisFormula, FORMULATEXT(INDIRECT(ADDRESS(ROW(), COLUMN()))),
        TEXTBEFORE(
            TEXTAFTER(thisFormula,"("),
            IF(ISERROR(FIND("[",ThisFormula)),
                ")",
                "["
            )
        )
    )
)

This LAMBDA function never actually references its parameter. Instead, the FORMULATEXT function returns the cell's formula, which looks like "=TableName(Table1[#Headers])."

The nested TEXTBEFORE(TEXTAFTER(... functions get the reference argument and trim down to just the table name. In case the table is referenced without square brackets, the IF function trims by the ")" character instead.

  • 1
    Also, I tried doing `=TableName(Address(Row()+1, Column()))` with the plan of just putting it directly above each table, but that won't work. After looking a little closer at how the function works with the string parsing, I understand why, but in case others try to use this blindly, as I did, just FYI. – Alex Kwitny Apr 18 '23 at 16:05
1

Following formula returns table name, referenced in it:

=LET(x, Table1[#Headers], y, FORMULATEXT(INDIRECT(ADDRESS(ROW(),COLUMN()))), z, LEFT(y, FIND("[", y)-1), TRIM(RIGHT(z, LEN(z)-FIND("x", z)-1)))

Note: you can use any part of the table, not only Table1[#Headers].
For example it could be: Table1[#All] or Table1[Column1] or Table1[[#Headers],[Column1]].
The last one is useful, because you can just click a cell with a column name to paste it.

Marat
  • 31
  • 2
  • Love this, albeit 2 small caveats - it uses volatile function `INDIRECT`, and it does not immediately update when table name changes. I made this expanded version to (1) add "Table: " text prefix and (2) convert underscores to spaces: `="Table:" & MID(SUBSTITUTE(LET(x,Manifold_Descriptions[#All], y, FORMULATEXT(INDIRECT(ADDRESS(ROW(),COLUMN()))), z, LEFT(y, FIND("[", y)-1), TRIM(RIGHT(z, LEN(z)-FIND("x", z)-1))), "_", " "), 6, 999)`. This returns `Table: Manifold Descriptions`, and (eventually) automatically updates with table name changes. – Micah Lindstrom Jun 03 '22 at 12:50
0
=MID(
    FORMULATEXT(<CurrentCell>),
    53+2*<LengthOf<CurrentCell>>),
    LEN(FORMULATEXT(<CurrentCell>))-(53+2*<LengthOf<CurrentCell>>))-3
)&IF(0,<TableName>,"")
  • Welcome to Stack Overflow! Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks. – 4b0 Sep 02 '18 at 04:20
  • I will check it out ASAP – Mouthpear Nov 30 '18 at 09:21
0

If the tables are all on separate sheets, and the sheet names are the same as the table names, you could do something like this:

=LET(filename,CELL("filename",Table[#Headers]),RIGHT(filename,LEN(filename)-FIND("]",filename)))

This will return the sheet name that the table is on, which will match the table name.

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
PipM
  • 11
  • 2