views:

385

answers:

4

I have a table with 3 columns. 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.

So, for example, for the table MyTable with columns A, B, C I'd like to be able to write: =GetIndex(MyTable[C]) and have it return 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.

Thanks

A: 

You could use: =COLUMN(MyTable[*]) - COLUMN(MyTable[A]) + 1, where * is the column whose index you want.

e.James
Is there a way to get the first column in a table? I don't rather not be locked into having "A" always be at the first index. Thanks
TheDeeno
+1  A: 

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
Remou
I think this is making 2 many assumptions. Basically I'd like a way to define a table "MyTable" which is any range. I then want to be able to get the index of a named column relative to the first column in "MyTable" can this function do that? Remember I'm using structured references. Either way ty
TheDeeno
A: 

=slight modification to eJames' respones: =COLUMN(MyTable[ * ]) - MIN(COLUMN(MyTable)) + 1, where * is the column you want the index of.

jadurda
A: 
Robert Mearns