I try to avoid using Excel too much, 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.
Related
I have multiple rows 34 columns wide with a reference Designator(RefDes) in each one that correlates to a value. I am trying to find a way to match each RefDes to its value and sum those together without the use of a helper cell for each column to give each row a total value.
My current formula will work with a helper cell but that adds 34 more columns and makes it very messy and hard to read.
=INDEX(FullFlow2[[Category Times]:[Category Times]],MATCH(N4,FullFlow2[[REFDES]:[REFDES]]))
Followed by a =sumproduct()in another column to collect the values.
Not sure if this is something I will need to use VBA for or if I can make it work without it.
This is the RefDesTable below
This is the FullFlow2 Table below
Use SUMIFS wrapped in a SUMPRODUCT:
=SUMPRODUCT(SUMIFS(FullFlow2[[Category Times]:[Category Times]],FullFlow2[[REFDES]:[REFDES]],YourFirstTableName[1st]))
Replace YourFirstTableName with the correct name for the table.
In the sheet1 i have a table called working days of the countries as shown in the below image.
In the Sheet2 i have 10 columns in that based on the country and month by referring the this table i am trying to populate the values, When i tried doing by Vlookup the first row alone getting populated, but in the second row the header from F1:T1 is getting changed to F2:T2 so rest of the cells showing as #NA.
Anyone can you please give a solution for my issue. Here is the formula i have used.
=VLOOKUP(I1,Sheet1!F2:T7,MATCH(Sheet2!M1,Sheet1!F1:T1,0))
Thanks in Advance.
You are missing the symbol $ to lock the ranges, and the false condition to match exact values in the VLOOKUP.
It should be like:
=VLOOKUP(I1,Sheet1!$F$2:$T$7,MATCH(M1,Sheet1!$F$1:$T$1,0),0)
Or instead of VLOOKUP use HLOOKUP like:
=HLOOKUP(M1,Sheet1!$F$1:$T$7,MATCH(I1,Sheet1!$F$2:$F$7,0),0)
In general, combining the INDEX and MATCH functions is a superior option to VLOOKUP. For example, =INDEX(Sheet1!F:F,MATCH(Sheet2!M1,Sheet1!F1:T1,0)). This allows you to go left-to-right or right-to-left as well.
Snip for Index Match functions Using Vlookup won't work here because in the 2nd table you are repeating the country, unlike the first table. Use a combination of index match function, this is little trickier than the vlookup but it will fulfill your requirements.
Since I don't have the exact table you shared so I created a table on my end and sharing the snip here.
In the dummy table below, I can use the formula below to lookup a value in the third column:
=VLOOKUP(1, Table1[A]:Table1[C],3)
I am wondering if there is any way to define the lookup range as just column-A and column-B using a union operator?
The formula works fine - the motivation for using only two columns is when working with more complex tables that may change, I would always know that the lookup index is 2.
The short answer is, no, there is no way to do two disconnect columns.
So we do:
=VLOOKUP(1,Table1[A]:Table1[C],COLUMNS(Table1[A]:Table1[C]),TRUE)
Now it will count the number of column that that range reference encompass. In your example it would be 3, if another column is added between it would be 4 thus always referring to the correct column.
I'm having a very difficult time with transitioning my VLOOKUP statement from simply referencing sheets and ranges (which work like a charm), to instead using table and column names.
I'm trying to make the VLOOKUP a bit more robust as the sheet where it is pulling from will constantly be changing data, so column numbers will change frequently. Thus, I'd like to just reference a column by its name.
I have converted the source data sheet to a table.
I have named all columns appropriately and double checked spelling.
This VLOOKUP works great (currently):
=VLOOKUP(E6,'Costs'!$A$2:$AE$84,19,FALSE)
However, what I would like to do is make it look this:
=VLOOKUP(E6,tblCosts[Order Number],tblCosts[June 2017], FALSE)
I have been fiddling with also trying to use MATCH which is not working either:
=VLOOKUP(E31,tblCosts[Order Number],MATCH(F4,tblCosts[June 2017],FALSE),FALSE)
UPDATE
This formula now works but it is returning the order number...not the cost for the month.
=VLOOKUP(E31,(tblCosts[Order Number]),(tblCosts[June 2017]),FALSE)
Normally I would keep trying this myself...but I am feeling overwhelmed and have been trying for hours. Any advice would be great.
Thank you!!
This should work for you:
=VLOOKUP(E31,tblCosts[#All],COLUMN(tblCosts[Jun-17]),false)
The COLUMN(TblCosts[Jun-17]) returns the column number of the field you want.
The VLookup searches the first column of tblCosts[#All] - (all the data in the table) for the data (in E31) and returns the data in the column you want.
This formula will continue to work even if you add columns or rows to the data.
Make sure that any heading which looks like a date in your spreadsheet eg "Jun-17" is stored as Text and not as a Date, as it will not match the string "Jun-17" when you use VLookup.
=VLOOKUP(E37,tblCosts[#All],match("Jun-17", tblCosts[#Headers]),FALSE)
This match is looking for the column name in all column headers and will return the number of columns to the right of the first column rather than the column number in the sheet. This avoids having to use COLUMN(tblCosts[Jun-17]) - COLUMN(tblCosts[Order Number]) + 1 when your table doesn't start in column A.
Ref: https://www.excel-university.com/vlookup-hack-4-column-labels/
I don't know if I'm going about this the wrong way but it seems like it should be simple. Column A has a list of Names. Along each row is several "W"'s. Another separate field has a drop down representing Column A names. I want to count the number of "W"'s in a row corresponding to what name I select. I've tried using VLOOKUP and COUNTIF but I can't figure out how to select the entire array and then single out the one row that matches my selected name. I can get it working with a bunch of IF statements but thats far too time consuming as I'm manually matching the name to the row (and it isn't future proof).
There are a few ways to first 'narrow in' on the row you're looking for, after which point you can use a simple COUNTIFS to check the number of W's in that row.
One method would be to simply use INDIRECT, and create the row reference on the fly, like so [assumes your search cell is C1]:
=COUNTIFS(INDIRECT(MATCH(C1,A:A,0)&":"&MATCH(C1,A:A,0)),"W")
This first uses MATCH to find the appropriate row, and then builds a reference to that row [like "24:24"], which becomes the row that INDIRECT passes to COUNTIFS, which counts that row for W's.
For only one use of INDIRECT, the high computing costs of INDIRECT should not be an issue.
Another method would be to point out the full possible box that data could be contained in [let's assume that at most only column H would be used], and then use INDEX to give us the appropriate row number, like so:
=COUNTIFS(INDEX(A:H,MATCH(C1,A:A,0)),0,"W")
This again uses MATCH to find the row which contains the value found in C1 within column A. Then it takes the full possible box from INDEX, and returns all columns from the particular row [note that telling index to return 0 for the column # actually returns all columns instead].
Other methods would be possible [for example OFFSET], but I believe these two show the principle fairly well.
You could use the "Helper" Column method:
In the helper column:
=COUNTIF(B2:H2,"W")
Then use SUMIF() in the totals column:
=SUMIF($A$2:$A$9,K2,$I$2:$I$9)