MS Excel vlookup a cell, get row, get the rightmost column from that row - excel

Similar question seems to be this. However in my case I need an additional step (I think). What I can't get done is combine these two steps.
Let's say the data structure is as below
Col A | Col B | Col C | Col D | Col E |
Row 1 Val1 | Val2 | Val3 |
Row 2 Val4 | Val5 | Val6 |
Row 3 Val7 | Val8 | Val9 | Val10 | Val11
The formula I am looking for should return results as per below scenerios.
1) In a separate cell, If I enter a value (Val4); Do a VLOOKUP for Val4 in Col A (which is in Row 2) and then return the right most value which is Val6 from Col C
2) In a separate cell, If I enter a value (Val7); Do a VLOOKUP for Val7 in Col A (which is in Row 3) and then return the right most value which is Val10 from Col D
Basically the column values may grow to the right. So I need to lookup the first value -> get the row -> get the rightmost value in the same row....
Any help is much appreciated.
EDIT - 1
After the guidance given here, I came halfway to the solution. First thing to mention is that values in a row can grow (In my sample data set it goes up to column D. But it can grow to Column E,F etc... in any row)
Now, let's assume the value I am searching is in H1. I put Val4 in H1 and the formula:
OFFSET(A1,MATCH($H$1,$A:$A,0),2,1,1). //Since I have hard coded 3rd parameter to 2; it'll give me Val6 (the last column in that row)
Here by MATCH($H$1,$A:$A,0) it returns the row. In my example it'll return Row 2 since I am searching for Val4 (In H1)
Now, I should search columns in Row 2 and stop in ColC since its the last value in Row 2.
In the OFFSET function, 3rd parameter looks for the number of columns. So if I can vary that value based on the row; problem is solved. For example; if I search for Val7, in my OFFSET formula, it'll first find Row 3. But now in OFFSET 3rd parameter must 4 (to get the last value - Val11).
As I search the rows using MATCH($H$1,$A:$A,0); if I can pass that row to CountA() problem is solved like:
CountA(MATCH($H$1,$A:$A,0) : MATCH($H$1,$A:$A,0))
This is invalid. So now trying to use INDIRECT() function like:
COUNTA(INDIRECT(MATCH($H$1,$A:$A,0)&":"&MATCH($H$1,$A:$A,0)))
which basically builds row:row as a string and feeds to CountA()
This works on its own, but when I put this to OFFSET() function 3rd parameter, it doesn't work the way it should.
EDIT - 2 (ANSWER)
Got it to work with formula
OFFSET(A1,MATCH($H$1,$A:$A,0)-1,COUNTA(INDIRECT(MATCH($H$1,$A:$A,0)&":"&MATCH($H$1,$A:$A,0)))-1,1,1)
Only issue is when searching for columns in a row, if you have an empty cell it won't search further using COUNTA().... but otherwise it works the way I want

Assume your data housed in A1:D3
Criteria put in A5:A6, (layout similar to JvdV's table)
If your data is text value, formula in B5 copied down :
=LOOKUP("zzz",INDEX($1:$3,SUMPRODUCT(($1:$3=A5)*ROW(A$1:A$3)),0))
If your data is numeric value, formula in B5 copied down :
=LOOKUP(9.9E+307,INDEX($1:$3,SUMPRODUCT(($1:$3=A5)*ROW(A$1:A$3)),0))
If your data is mixed in text+numeric value, formula in B5 copied down :
=INDEX($1:$3,SUMPRODUCT(($1:$3=A5)*ROW(A$1:A$3)),AGGREGATE(14,6,COLUMN($1:$1)/(INDEX($1:$3,SUMPRODUCT(($1:$3=A5)*ROW(A$1:A$3)),0)<>""),1))

Got it to work with formula
OFFSET(A1,MATCH($H$1,$A:$A,0)-1,COUNTA(INDIRECT(MATCH($H$1,$A:$A,0)&":"&MATCH($H$1,$A:$A,0)))-1,1,1)
Only issue is when searching for columns in a row, if you have an empty cell it won't search further using COUNTA().... but otherwise it works the way I want

Related

Conditional subtraction from multiple cells

A |B |C |....|K |L |M |
Tom |0 | |....|Tom |Jim |Dave |
Jim |1000 | |....|15000|14000|12000|
Dave |3000 | |....| | | |
Using Google Sheets for this one. I would like the values in columns K, L, and M to read from column B, detect if the corresponding cell from A reads one of 'Tom', 'Jim', or 'Dave' for example, and then subtract the amount from the correct column to reduce a running total. I've had some trouble figuring it out and tried to use conditional formatting to solve it but can't seem to quite get there. Is there a formula I can use that will read column B and subtract the amount shown from the correct column based on the name in column A?
So to pseudo-code it:
read(column B cell);
if(column B cell - 1 column = "Tom")
{
column K - (value of column B cell)
}
else if(column B cell - 1 column = "Jim")
{
column L - (value of column B cell)
}
etc.
Is there a simple method I can use to generate this result? Also thought about changing the formatting of a cell based on the name in the cell next to it and subtracting the value of any cell with that colour but this becomes unwieldy if names are added. Any assistance would be greatly appreciated!
would there be a way to constrain the formula to a single cell so I can have the total in a single location rather than down the columns? My plan is to have the total boxes scroll with the sheet and always be visible.
Let's assume that Columns A and B continue on down the sheet, with further name + amount entries. You want to have a single row of balances for each of the people.
The balance is then some initial value less the sum of amounts for that person. Say the amounts you've shown in row 2 are the initial values; here's how you could have row 3 reflect the remaining balance for each person (This is for "Tom", copy for the others):
=K2-sumif($A$2:$B,"="&K$1,$B$2:$B)
Alternative solution
This doesn't do exactly what you want, but it is more appropriate for a running total scenario, so others may find it useful to adapt to ledgers, etc.
The IF() function can be used to decide whether or not a value in Column B applies to one of the "total" columns, K to M.
Use this formula in K3, copy to the rest of the range:
=K2-if($A2=K$1,$B2,0)
This example is in Google Sheets, but the same formula works in Excel and other "compatible" offerings.
What you do is put the formula in each column.
Column K subtracts value of column B if column A = "Tom"
Column L subtracts value of column B if column A = "Jim"
etc

Excel - return value based on two inputs

I have two input values, that I want to use to return a third value.
Input 1: Y (lets say "Y" is in cell B1)
Input 2: 15 (15 in cell B2)
In another database sheet the input 1 values are sorted in the top row, the input 2 values are listed in a column in front of the wanted values.
| | X | Y | Z |
|16| a | g | k |
|15| b | h | l |
|14| c | i | l |
Fx. X,Y,Z are in row 3 and column 2,3,4.
I want a formula that returns "h" from the two inputs, Y & 15. How is this possible?
In your example above:
=INDIRECT(B1&B2) will return the value in [Y15] which is "h"
Assuming 16 is in Row4, please try:
=INDIRECT(CHAR(CODE(B1)-22)&6-MOD(B$2,14))
Here's a formula for you:
=INDEX(MatrixRange,MATCH(RowInput,MatrixStartColumn,0),MATCH(ColumnInput,MatrixStartRow,0))
MatrixRange = whatever the range of your data matrix is (in entirety) so if your data matrix starts in A3 and ends in D20 this would be replaced with A3:D20
RowInput = whatever cell you are getting search value for to find the row of appropriate data (This is Input 2 on your example)
MatrixStartColumn = Whatever column (or range) your Matrix data index starts in (these would be the numbers in your example). If the numbers on the left side of your data example are in Column A, this would be changed to A:A or A1:A50 (or wherever the last value is). The important thing for this is to use a range from the start of the column otherwise your row count will be off. If you must use a sub-range to avoid matches outside of the matrix, be sure to add the appropriate number to the end of the Match statement. For example if you are specifying MatrixStartColumn as "A3:A44", you will need to add +2 for the first 2 rows being skipped (A1 and A2). So the Index statement becomes (MatrixRange,MATCH(RowInput,MatrixStartColumn,0)+2,...
ColumnInput = whatever cell your column search value is in (your Input 1 data)
MatrixStartRow = Same as StartColumn above but for the header index of your matrix (the XYZ letters in your example). Just as above, if you must use only the range of the matrix, be sure to add your offset numbers so you get the right column.
Assuming that your inputs are at B1 (row), and B2 (Column) here is your formula:
=INDEX(B4:D6,MATCH(B1,A4:A6,0),MATCH(B2,B3:D3,0))
Here is how the formulas work:
INDEX(area with values, row , column) returns value based on row and column you provide.
MATCH(value to find, range to search) returns row/column where value was found.
Note that Match will accept only one row or column for 'range to search'.

VLOOKUP for multiple entries

I have a few columns in a sheet. First column being first names and the fifth being their respective age. If I want to search the age column for a particular age say '12' and return their corresponding first names in a separate sheet, what should i do? I tried VLOOKUP but I could not figure out the logic. Can someone help me out?Thank you.
Unfortunately VLOOKUP will not work in this situation,as the Vlookup function cannot reference details on left side, however you can use a combination of INDEX and MATCH functions thou. Lets say you have following table
A B
mark 11
john 23
Selly 30
Youbaraj 45
and you want to get the value of A based on the value of B, you can use something like
=INDEX(A1:A20,MATCH(1,B1:B20,0))
You can use index and match to do HLOOKUP and VLOOKUP looking into any column and getting values of any side.
You can use an Index and Double match .. to get answers from a column by entering its name.
Example:
A B C D
1 col1 col2 col3 col4
2 val1 val2 val3 val4
3 val5 val6 val7 val8
Consider in cell C10 ColumnName:
And in cell C11, you enter the name of a column
Now see what would happen with this function
=index(A1:D3,Match(C11,A1:D1,0),Match(val-to-look-for, Column (a,b,c,d),0))
You can dynamically type a name of a column to get that column name's look up value
Vlookup is very easy to use, however the first column must be arranged alphabeticaly before in order for it to work properly. (and usualy i use only the exact match argument).

Excel: Matching the closest date from range of dates

I'm trying to display the header of the column that its cell that contains a date, is the closest (out of 3 other cells with dates, B2:D2) to the a specific cell (A2)
So here's my sheet:
A B C D
1 | T1 | T2 | T3 | T4 |
2 | 5/1/2013 | 5/31/2013 | 2/5/2013 | 3/2/2013 |
A2 = a date.
B2:D2 = 3 dates, can also be blank
I've tried using the following array formula that I found online:
=IF(COUNT($B2:$D2)>0,INDEX($B$1:$D$1,MATCH(MIN(ABS($B2:$D2-$A2)),ABS($B2:$D2-$A2),0)),"")
But it sometimes when there is only one or two dates, it gives me the wrong header. I would also like to avoid using array formulas, but couldn't convert it to a regular one.
Would highly appreciate assistance on this matter
Can you give an instance were the array formula you are using isn't working ? It worked for the cases I tried.
You can avoid using the array formula if you are prepared to have another row with the difference in the dates and your date cell A2.
Push your rows down one row, so the headings are on row 2 and the dates on row 3 and enter the following formula across row 1, starting at cell B1
=ABS(B3-$A$3)
Drag this across and enter the following HLOOKUP to get your header row
=HLOOKUP(MIN(B1:D1),B1:D3,2,FALSE)
if you can't move your rows down for some reason you can use a combination of MATCH and INDEX (which is slower) . If you were to put the difference function =ABS(B2-$A$2) across row 7 the formula would be
=INDEX(B1:D1,1,MATCH(MIN(B7:D7),B7:D7,0))
Okay so the answer was fairly simple.
I went back to the sheet to investigate the scenarios in which this happens.
It seems that when A2 is blank (= no date to make the comparison) it just gives out the only date available from B2:D2.
By simply adding another condition to the IF statemented that would work only if A2 has a value resolved the issue. So we're checking if both A2 and B2:D2 has values in them (with B2:D2 needs to be at least one:
=IF(AND(COUNT($B2:$D2)>0,$A2>0),......)
and here is the full formula
=IF(AND(COUNT($B2:$D2)>0,$A2>0),INDEX($B$1:$D$1,MATCH(MIN(ABS($B2:$D2-$A2)),ABS($B2:$D2-$A2),0)),"")
Thanks for making me go back to the investigate! :D
For a non-array alternative, you could try:
=LOOKUP(2,1/FREQUENCY(0,ABS(B2:D2-A2)),B$1:D$1)
in place of the INDEX(...) part of the formula.

Excel Function Help - Compare 2 Cells in Worksheet to 2 Cells in another worksheet, if they match, return value from a different column

I'm wondering if someone would be able to offer some advice on the best way to do this please:
Data in Worksheet # 1
Col A | Col B | Col C
Part-1 | 8 | 2
Part-2 | 7 | 7
Part-7 | 9 | 4
Part-8 | 2 | 6
Data in Worksheet # 2
Col A | Col B | Col C
Part-1 | 8 | *Return value* (If Part-1 is found and 8 matches, return 2)
Part-2 | 7 | *Return value*
Part-3 | 8 | *Return value*
In Worksheet#2 in Cell C2 - I would like to check if the Part-1 in A1 is found in Col A in Worksheet#1. If it is, then I would also like to make sure the Number is B2 in Worksheet#2 matches the Qty in Col B next to the same part#, if both the Part# and Qty match, then i would like to copy across the value from the corresponding cell in Col C in Worksheet#1, to Col C in Worksheet#2. If it does not match, I would like it to return a 0.
Here is the a variation on Reinier's second approach in a form that will work in any version of Excel. You can use references to the specific data ranges, as I have done here, or to entire columns.
=SUM((A2=Sheet1!$A$2:$A$5)*(Sheet2!B2=Sheet1!$B$2:$B$5)*Sheet1!$C$2:$C$5)
This is an array formula, so it needs to be entered with the Control-Shift-Enter combination. It performs the same operation as the SUMPRODUCT. (There are several other ways to do this, such as using MATCH with INDEX or OFFSET.)
Essentially, what you are doing is a lookup based on values in two columns. Looking at it from that angle, you can use a the SUMPRODUCT function, which is supported in any version of Excel. Enter the following in Sheet2, cell C2:
=SUMPRODUCT((Sheet1!$A:$A=$A2)*(Sheet1!$B:$B=$B2)*Sheet1!$C:$C)
Select and drag down the right-bottom corner of C2 to complete column C.
This only works by virtue of the fact that the values in Sheet1, column C, are numbers. It breaks if value pairs in column A and B of Sheet1 occur multiple times, but you did not address that situation in your question in the first place.
For versions 2007 and up, you can use the more convenient function SUMIFS with basically the same approach:
=SUMIFS(Sheet1!$C:$C,Sheet1!$A:$A,$A1,Sheet1!$B:$B,$B1)
Alternatively, you can use a combination of IF and VLOOKUP functions. A formula that will work for Excel 2007 or newer is the following:
=IFERROR(IF(VLOOKUP($A1,Sheet1!$A:$C,2,FALSE)=$B1,VLOOKUP($A1,Sheet1!$A:$C,3,FALSE),0),0)
Older versions of Excel do not support IFERROR, but you can still use a similar approach as explained here.
I have uploaded an example workbook here, which includes an alternative method in column D of Sheet2 as well.

Resources