Excel Return Table name using Formula?

时间:2016-04-12 00:33:06

标签: excel formula defined tablename

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

4 个答案:

答案 0 :(得分:3)

这是一个VBA解决方案,因为你说你想看到它。这是您使用VBA创建的UDF(用户定义函数),但在单元格内用作公式。将代码保存在标准代码模块中,而不是表单模块或" ThisWorkbook"模块。

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

保存到模块后,您可以在像这样的单元格公式中使用它:

=GetTableName(A1)

或者

=GetTableName(B:B)

或者

=GetTableName(B2:W900)

如果您使用的范围与多个表重叠,则会返回第一个表的名称。

答案 1 :(得分:2)

您需要两个单元格才能获取表名。我的表标题从第2行开始,第3行开始表数据,因此我将两个公式分别放在单元格A1和B1中。

第一个单元格应该引用表格的左上角标题单元格。对我来说,这个公式最终会读到:

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

并等同于" State"。

第二个单元格的公式应为:

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

并等同于" My2016Data"。

答案 2 :(得分:0)

=MID(
    FORMULATEXT(<CurrentCell>),
    53+2*<LengthOf<CurrentCell>>),
    LEN(FORMULATEXT(<CurrentCell>))-(53+2*<LengthOf<CurrentCell>>))-3
)&IF(0,<TableName>,"")

答案 3 :(得分:0)

如果表格都在单独的工作表上,并且工作表名称与表格名称相同,您可以执行以下操作:

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

这将返回表格所在的工作表名称,该名称将与表格名称匹配。