Excel structured reference table syntax
I try to avoid using Excel too much, 开发者_JAVA百科but when I do I like using structured references as they seem a lot cleaner to write.
If I create a table called "table1" with columns "col1" and "col2" how would I reference the first row in "col1" using a structured reference in another table? I have tried the syntax =table1[[#this row],[col1]]
, and just get an error. Is there a syntax like =table1[1,1] or =table1[1,[col1]]
? Of course, this doesn't work either, but what's the equivalent?
It's very annoying, as it seems like this should be simple.
Table1[[#This Row][Column1]]
does work, but the formula must be on the same row as the table row you wish to reference.
To reference the first row, elsewhere, use either COUNTIFS(criteria_range1, criteria1 [, criteria_rangen, criterian])
Or the slightly more complex SUMIFS()
if you need numeric values instead of counts, as mentioned by studgeek:
SUMIFS(sum_range1, criteria_range1, criteria1 [, criteria_rangen, criterian])
You will of course need a unique row criteria by which to select the row. So, for example:
Table1
ID Value Name
1 2 Two
2 4 Four
3 8 Eight
SUMIF(Table1[Value], Table1[ID], 2)
... returns a value of 4 (or zero if ID=2 not found). If your value is not numeric, then you can't use this method, obviously.
However, akuhn almost hit the real answer, but he didn't go quite far enough in his explanation/example, IMO.
INDEX(Table1[Name], 2)
returns "Four"
INDEX(Table1, 1, 1)
returns 1
try
=INDEX(col1,1)
you can even address cells in a 2-dim table, using
=INDEX(reference,row_num,column_num)
The trick in such cases is to use Excel OFFSET function:
- Accessing 1st row of column named Column1 in the same table:
OFFSET([Column1],0,0,1)
- Accessing 2nd row
OFFSET([Column1],1,0,1)
etc.
Of course you can use this to accces another table and column by just prefixing it with the table name. For example OFFSET(Table2[Column3],4,0,1)
will access the 4th row of the column 'Column3' of 'Table2'
There does not seem to be an explicit way of using structured referencing to particular rows in a table. As Adrian says, you can use INDEX.
Or you can use implicit intersection to reference the same row: if table1 is on row 5:10 and table 2 is also on row 5:10 then using a structured reference with column names will implicitly intersect the same row.
Or you can enter the structured reference as a multirow array formula (select multiple cells, enter the formula and use Ctrl-shift-Enter) in different rows and it will work.
Instead of INDEX I would suggest SUMIF. It will let you use table values rather than explicit row numbers (which can break if you start filtering or ordering). For example (from the following link), this sums the Amount column and only include those rows where Type equals Check and where Account equals Utilities:
=SUMIFS(Table1[Amount],Table1[Type],“Check”,Table1[Account], “Utilities”)
See this link more info: http://office.microsoft.com/en-us/excel-help/using-structured-references-with-excel-tables-HA010155686.aspx
It would be nice if a table could have a column designated as the primary key (which could be numeric or string), and then a structured ref could include a way to reference a row by it's primary key.
This would be syntax sugar around VLOOKUP, but the table could know if it was sorted on the primary key, and do efficient lookups only in that case. Seems that VLOOKUP embeds evil in it that finds the wrong row if you depend on sorted, especially when tables have a convenient way to sort rows.
精彩评论