简体   繁体   English

如何通过在excel中使用结构化引用来获取表列的索引?

[英]How do get the index of a table's column by using a structured reference in excel?

I have a table with 3 columns. 我有一个有3列的表。 I want to write a formula that, given a structured reference, returns the index of the column. 我想编写一个公式,给定结构化引用,返回列的索引。 This will help me write VLookup formulas using the structured reference. 这将帮助我使用结构化引用编写VLookup公式。

So, for example, for the table MyTable with columns A , B , C I'd like to be able to write: 因此,例如,对于包含ABC列的表MyTable ,我希望能够编写:

=GetIndex(MyTable[C])

and have it return 3. 并让它返回3。

Right now I just make sure the table range starts on the sheet's first column and I write 现在我只是确保表格范围从工作表的第一列开始,然后我写

=Column(MyTable[C])

but I want something a more robust. 但我想要一些更强大的东西。

A suitable formula based on your example would be 根据你的例子,一个合适的公式是

=COLUMN(MyTable[C])-COLUMN(MyTable)+1

The first part of the forumla COLUMN(MyTable[C]) will return the column number of the referenced column. 论坛栏的第一部分COLUMN(MyTable[C])将返回引用列的列号。

The second part of the formula COLUMN(MyTable) will always return the column number of the first column of the table. 公式COLUMN(MyTable)的第二部分将始终返回表的第一列的列号。

Another solution to the question you asked (or something close to it) is to use something like =MATCH("C",MyTable[#Headers],0) , which will return 3 in the example you posted. 你问的问题(或接近它的问题)的另一个解决方案是使用类似=MATCH("C",MyTable[#Headers],0) ,它将在你发布的例子中返回3。

However, if you used INDEX instead of VLOOKUP, you wouldn't need to do this. 但是,如果您使用INDEX而不是VLOOKUP,则不需要执行此操作。 For example, if you wanted to find the value of C in the row (assumingly there is no more than one) where A is equal to 2, you could use a formula like =INDEX(MyTable[C],MATCH(2,MyTable[A],0)) , which is nicely self-documenting. 例如,如果你想在行中找到C的值(假设不超过一个),其中A等于2,你可以使用类似=INDEX(MyTable[C],MATCH(2,MyTable[A],0))的公式=INDEX(MyTable[C],MATCH(2,MyTable[A],0)) ,这是很好的自我记录。

Do you mean: 你的意思是:

Dim r As Range
MyLetter ="AA"
Set r = Range(MyLetter & "1")
MyIndex= r.Column

Edit re comment 编辑重新评论

Function GetRelativeColumn(Letter, RangeName)
Dim r As Range
Dim ColStart, ColRequired, ColTemp
Set r = Range(RangeName)

ColStart = r.Column
ColRequired = Range(Letter & "1").Column
ColTemp = ColRequired - ColStart + 1
If ColTemp < 1 Or ColTemp > r.Columns.Count Then
    MsgBox "Ooutside range"
Else
    GetRelativeColumn = ColTemp
End If
End Function

=对eJames的响应稍作修改:= COLUMN(MyTable [*]) - MIN(COLUMN(MyTable))+ 1,其中*是您想要索引的列。

你可以使用: =COLUMN(MyTable[ * ]) - COLUMN(MyTable[A]) + 1 ,其中*是你想要的索引的列。

What is your end goal? 你的最终目标是什么? You may be better off using SUMIF (as I describe here ) or INDEX (as I describe here ) to access your values rather than jumping back to row/column... 你可能最好使用SUMIF(我在这里描述)或INDEX(我在这里描述)来访问你的值而不是跳回到行/列......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在表[#Data](或类似)的结构化引用中排除列 - How to Exclude a Column(s) in a Structured Reference to Table[#Data] (or similar) Excel结构化参考表语法 - Excel structured reference table syntax 如何使用 excel 中结构化引用表的特定连续列在 vba 中设置范围? - How to set a range in vba using specific contiguous columns of structured reference table in excel? 如何将excel中的结构化引用转换为范围? - How do you convert a structured reference in excel to a range? 如何使用他的姓名和列引用循环 excel 2010 表? - How do I loop an excel 2010 table by using his name & column reference? Excel 结构化引用动态表名称 - Excel Structured Reference Dynamic Table Name Excel:保存 Excel 文件后结构化表参考不会更新 - Excel: Structured Table Reference doesn't get updated after Excel File is saved 表中标头单元格范围的索引/匹配计算列公式中的结构化引用 - Structured Reference in an Index/Match Calculated Column Formula for Header Cell Range in a Table Excel查找功能可查找结构化的表和列 - Excel lookups function to find a structured table and column 如何使用 Excel 表结构化引用创建运行总计? - How to create running total using Excel table structured references?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM