Excel How can I direct columns by their headline? - excel

Currently I have a huge formula in my excel sheet:
=SUM(SUMIF(INDIRECT(A9&"!$F:$F"),"working";INDIRECT(A9&"!$B:$B"))+SUMIF(INDIRECT(A9&"!$F:$F");"Open";INDIRECT(A9&"!$B:$B"))+SUMIF(INDIRECT(A9&"!$F:$F");"internal",INDIRECT(A9&"!$B:$B"))+(SUMIF(INDIRECT(A9&"!$F:$F"),"finished";INDIRECT(A9&"!$AP:$AP"))))
In A9 is the name of the excel sheet that the formula has to use. So for example in A3 there's '20140612', so it has to use the excel sheet with the name '20140612'. Furthermore it sums up some values depending on some conditions.
My question is: I would like to direct the columns in the other sheets by their headlines, not their positions in the sheet. So for example le column AP doesn't always has to be in the position AP, but has always the headline 'Points'.
Can you think of an adaption of the formula that can direct the column by their headline?
I though of the MATCH formula. But I'm not to sure where I have to put this in.

I think the simplest answer would be to use named ranges within your sheet. In this way you could name a range (currently in column AP) as 'Points', change your formula to use 'Points' instead of '$AP:$AP' & if you move your points data about the formulas would be unchanged.
If you are planning to keep changing your header row values then you could use HLOOKUP to match up the header column probably in conjunction with MATCH & INDEX.

To answer your question about the usage of MATCH(), it's worth thinking of it as half of a VLOOKUP() or HLOOKUP(), i.e. it's the bit that finds the row or column containing the value you're searching for, then you can use INDEX() to get that row / column from a range you specify.
So, if you know that one of your column headings is "Points", then you could find it by using:
=MATCH("Points", A1:Z1, FALSE)
...which would return 10 if "Points" were the heading of column J for example. If you wanted to then use that column for a lookup, you can use OFFSET() to define that column as a range to use for a lookup, so let's say I wanted to find the text "foo" in the "Points" column, I could use:
=MATCH("foo", OFFSET(A1:Z1000, 0, MATCH("Points", A1:Z1, FALSE) - 1, 1000, 1), false)
... which uses the column index I found before as an input to OFFSET() in order to dynamically reference J1:J1000 and then search for "foo" in that column.
For your example in the question, rather than A1:Z1000 you could use a call to INDIRECT() that would return the entire range of interest from your source sheet.

Related

Sum of index matches for dynamic range of columns

On Sheet2 I am trying to sum the values in a row of a range on Sheet1 over a dynamic range of columns based on a lookup value for the from column and a fixed variable for how many columns to right of that lookup value for the to column.
I am using INDEX-MATCH to find the from cell based on certain reference, INDEX-MATCH with a reference added to the column lookup to find the to column, and CELL to get the position rather than value of the results.
What I have is the following:
=SUM(
CELL("address",
INDEX(Sheet1!$B$4:$BA$36,MATCH($A$1,Sheet1!$A$4:$A$36),MATCH(D$3,Sheet1!$B$3:$BA$3,0)))
&":"&
CELL("address",
INDEX(Sheet1!$B$4:$BA$36,MATCH($A$1,Sheet1!$A$4:$A$36),MATCH(D$3,Sheet1!$B$3:$BA$3,0)+'Control Panel'!$C$2)))
Control Panel!$C$2 is my variable for how many columns to the right of the from column I want the to column to be.
Obviously, this is not working. I suspect it's because the concatenated text in the SUM() reference the full file name rather than 'Sheet1'![from]:[to]. Not sure if this is the case, but also can't figure out how to get just the A1 cell position for the to.
Any ideas how I can get this to work?
Figured it out:
=SUM(INDEX(Sheet1!$B$4:$BA$36,MATCH($A$1,Sheet1!$A$4:$A$36),MATCH(D$3,Sheet1!$B$3:$BA$3,0))):INDEX(Sheet1!$B$4:$BA$36,MATCH($A$1,Sheet1!$A$4:$A$36),MATCH(D$3,Sheet1!$B$3:$BA$3,0)+'Control Panel'!$C$2))
Literally just needed a colon between the two index-matches. Still not sure why this works, as the index-match should return a value.

VBA: vlookup through multiple columns

this is my first question so I will try my best to format this.
I would like to use the vlookup function in more than just one column. It seems impossible to do is in the formula for the large data set, so maybe someone with VBA knowledge can help me.
In my excel file I've two spreadsheets:
Sheet1 with one column "A" with 16868 different processes ID's.
Sheet2 with columns from "A" to "HX", where "A:HW" are different processes ID's (each column have different numbers of rows) and in "HX" column I have a region for the particular row of processes.
What I am trying to achieve is to do vloop for sheet1 column "A" that will look for each record in table sheet2 and return "HX" when found it.
In example:
I'm looking for "A2" cell in sheet1 in table_array sheet2 "A:HX". If found it then return cell in the same row but from column "HX". The trick here is when the looking value is not in the sheet2 column "A", then it should vlookup in column "B", then "C" and so on till "HW". There is an option where the looking value might not be in sheet2 at all and then the formula should return "0".
Is it possible to do this in VBA or excel formula?
Firstly, please NEVER use VLOOKUP. Ever.
An INDEX/MATCH combination is always better in every way. It is faster, more flexible and more robust as it does not break if you insert columns. It is also (arguably) easier to use as you do not need to count columns.
Anyway, back to the problem at hand. You can use an INDEX/SUMPRODUCT combination for this:
=INDEX(<range of return values>),SUMPRODUCT(--MAX((<range to search in>=<value to search for>)*ROW(<range to search in>)))-(ROW(<range to search in>)-1))
The SUMPRODUCT returns the last row number where the value is found.
The last bit -(ROW(<range to search in>)-1) is just to make the returned value relative to the searched range (rather than an absolute row number.
The INDEX is then uses this value to select a value from the HX column.

Excel - Check if value X and value Y is on the same row

I have an excel document with two sheets, data and edu-plan. The sheet data has the following information:
The sheet edu-plan looks like this:
My question is: how do i create an excel formula that checks if the target group on the specific row in edu-plan! has the course name in question on the same row as the target group in sheet data!, i.e. if Sales and Sales course is on the same row in the sheet data!?
In reality, the data sheet as a couple of hundred rows and will change over time, so i am trying to develop a formula that i can apply easily on all rows/columns in edu-plan!.
The desired result in edu-plan would look like this:
A pivot table might be a good way to go.
If you would like to do it by formula, then you can just use a COUNTIFS
=IF(COUNTIFS(data!$A$2:$A$10,$A2,data!$B$2:$B$10,B$1),"X","")
A possible way to solve your issue with an array formula:
Write in B2 of sheet edu-plan
{=IFERROR(IF(MATCH('edu-plan'!$A2&'edu-plan'!B$1,data!$A$2:$A$6&data!$B$2:$B$6,0)>0,"x",""),"")}
Since it is an array formula, you need to hit shift + ctr + enter.
Here is the formula broken down:
MATCH('edu-plan'!$A2&'edu-plan'!B$1,data!$A$2:$A$6&data!$B$2:$B$6,0)
checks whether the combination of row header and column header is in the data table. MATCH returns the index of the found combination. Since we are not interested in the location, we only ask IF(MATCH > 0, "x", "") to write an "x" if a match was found. If MATCH finds nothing, it returns an error, which is why we add an IFERROR(VALUE, "") around the construct.

Excel | Searching table in different sheet and bringing value from column in that row

I am trying to lookup a table in one of my sheets. my table consists of three columns and an unlimited amount of rows.
My table can be seen here:
In my second sheet I wish to write a formula which searches all rows in the table and looks for an exact match in column A and column B, this means it must find the row where column a has a value of "jan" and in that same row the second column must have value "y". Should it find this match, it should return the value of column C.
I tried researching hlookup but that is for horizontal tables so i dont believe this would work. I looked into Vlookups also but that only allows one criteria search instead of looking for two matches.
Can anyone shed some light here please?
You can use index and match with multiple criteria
=INDEX($A$1:$C$1000, MATCH("Jan"&"y", $A$1:$A$1000&$B$1:$B$1000, 0),3)
press CTRL + SHIFT + ENTER when entering this formula.
Convert the range to a table (ctrl-t) and then use SUMIFS to search the table based on two criteria
=SUMIFS(Table1[Alteration],Table1[Month],"Jan",Table1[Products],"y")
This is saying "give me [Alteration] where [Month] = "Jan" and [Products] = 'y'...this returns 364.
You can point the criteria at separate cells containing your criteria.
Be aware that if there is more than one row with identical data (ie more than one row with both 'Jan' and 'y'), column C will be summed together.
The INDEX function has the syntax
INDEX(array, row_num_in_array, [column_num_in_array])
And MATCH returns in the index location of a logic match
MATCH(lookup_value, lookup_array, [match_type])
Combining the two is a flexible technique, and surprisingly powerful -- the logic in a match lookup_value can be a complex condition.
SIMPLEST CODE
Operate on the whole column
INDEX(C:C, MATCH("Jan"&"y", A:A&B:B, 0), 1)
nb./ A:A is excel code for "all of column A". You can instead use:
RESTRICTED CODE
Operates on a subset of the sheet.
INDEX($C$2:$C$1000, MATCH("Jan"&"y", $A$2:$A$1000&$B$2:$B$1000, 0), 1)
Note that you MUST use identical row length arrays (eg. rows 2:1000) or the formula will not work. MATCH only knows how many rows into its lookup_array it got, you need to ensure its rows match those in INDEX's array
PS. apologies this is close to the previous answer, but the details were too long for a comment.
PPS. I missed the clarifications to the first answer. That will work, but there is no need to use all three columns as the array in the INDEX function. You are only returning data from column C after all.

Retrieving a formula with VLOOKUP from table with row numbers pointing to another sheet

I would like to create a table with products and formulas on a sheet (Sheet2). For different products, different formulas apply.
I would like to retrieve the formula from that table but use the row numbers from the row in Sheet1
How do I enter a formula which is evaluated with the correct row numbers from Sheet1.
I have a UDF eval that can evaluate a text string:
=eval(vlookup(Product;Table;2;false)
The formula retrieved from the Table should use the row number of the actual row that the eval() is on.
I tried the following:
="D"&ROW(Sheet2!$A16)&"/G"&ROW(Sheet2!$A16)&"/F"&ROW(Sheet2!$A16)&"*5"
This retrieves the formula but the eval() does not calculate the result.
In your example, product is located on Sheet2 in the 16th row of the first column of table. If you want the 16th row on the worksheet, you can use the MATCH function on a full column reference, discarding the structured references of the ListObject table or you can use MATCH on the structured table reference and compensate for the 'position within' table by adjusting by the table header row.
That's probably confusing so here is an example.
           
To find the actual row-on-the-worksheet where bcd resides you would use one of these formulas.
=MATCH("bcd", Sheet2!B:B, 0)
=MATCH("bcd", Table2[a], 0)+ROW(Table2[#Headers])
The first simply returns row 6 on the worksheet. The second returns 2 since bcd is in the second row of the ListObject's .DataBodyRange property and this is adjusted by the row that the .HeaderRowRange property is in; e.g. 2 + 4 = 6.
Now that everything is clear, all you need to do is use the result from one of those formulas as the row_number parameter in an INDEX function.
=INDEX(D:D, MATCH("bcd", Sheet2!B:B, 0))/
INDEX(G:G, MATCH("bcd", Sheet2!B:B, 0))/
INDEX(F:F, MATCH("bcd", Sheet2!B:B, 0))*5
'or,
=INDEX(D:D, MATCH("bcd", Table2[a], 0)+ROW(Table2[#Headers]))/
INDEX(G:G, MATCH("bcd", Table2[a], 0)+ROW(Table2[#Headers]))/
INDEX(F:F, MATCH("bcd", Table2[a], 0)+ROW(Table2[#Headers]))*5
        
Your own formula could have worked with a series of INDIRECT functions that convert constructed strings to actual cell references. However, INDIRECT is considered volatile and best avoided if possible.
=INDIRECT("D"&ROW(Sheet2!$A6))/INDIRECT("G"&ROW(Sheet2!$A6))/INDIRECT("F"&ROW(Sheet2!$A6))*5
This solution is based on the capability of Excel to reference the cells in R1C1 style. In order to use it, you will have to go to File -> Options -> "Formulas" tab, and check the box "R1C1 reference style" in "Working with Formulas" group.
In addition, your eval() function will have to be able to evaluate such kind of formulas.
Once, all of these is done, you only have to retrieve the formula.
In order to make the things clear, I will focus on an example.
The table below is the contents of Sheet1 (the "Database" of products).
The column "Formula" contains the relevant formula:
for apple: RC[-2]+RC[-1]
for banan: RC[-2]*RC[-1]
for lemon: RC[-2]/RC[-1]
The table below is the contents of Sheet2:
The column "Formula" here contains the formula, retrieved from Sheet1. The formula of the column "Formula" is as follows:
=FORMULATEXT(INDEX(Sheet1!R2C1:R4C4,MATCH(RC[-3],Sheet1!R2C1:R4C1,0),4))
The description of each of the functions, used here (formulatext, index and match) can be found in Excel help.
As it can be seen, the retrieved formula, represented in R1C1 style is correct in context of Sheet2, were the products are arranged differently, and may appear more than once. The only remaining work to do is to apply the eval() function, after it was adapted to evaluate R1C1-style referenced formulas.
I hope it helps.

Resources