[英]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: 因此,例如,对于包含A
, B
, C
列的表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*
是你想要的索引的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.