Excel INDEX / MATCH / MATCH across cell range - excel

I am trying to get the formula right to lookup a value and return the cell underneath that cell, but I can't seem to get the code right and a MATCH/MATCH does not seem to do the trick (at least to the best of my knowledge).
Let's suppose my data set looks like this:
Col1 Col2 Col3
---------------------
Row 1 | P(A1) P(A2) P(A3)
Row 2 | 10.5% 11.5% 12.5%
Row 3 |
Row 4 | P(B1) P(B2) P(B3)
Row 5 | 50.5% 60.6% 70.5%
Row 6 |
Row 7 | P(C1) P(C2) P(C3)
Row 8 | 25.2% 34.6% 88.5%
Now I have a reference cell, say A1, which can have the value P(C2) and I want to return the corresponding value underneath P(C2), i.e. 34.6% in the cell A2.
I hope I am not being too cryptic and thanks very much in advance for any assistance.
Best
Max

Try the following...
F2, confirmed with CONTROL+SHIFT+ENTER...
=INDEX(A2:C9,SMALL(IF(A2:C9=E2,ROW(A2:C9)-ROW(A2)+1),1)+1,MATCH(E2,INDEX(A2:C9,SMALL(IF(A2:C9=E2,ROW(A2:C9)-ROW(A2)+1),1),0),0))

Related

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

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

Index only cells in a column from another sheet if another cell in same row has a value greater than 0

So I have an example below of what I'm wanting to do.
Basically I need to Index Column B from Sheet 1 into Sheet 2 BUT ONLY if the values in Column W in Sheet 1 are greater than 0. If it's not then I don't want it to be included in. The only column to Index is B starting from row 5 to say 100. Same for Column W.
I was trying to do it myself as I found This which is very similar as what I'm wanting to do but I couldn't figure it out.
Sheet 1
Row# Column B | Column(s)… | Column W
=================================
5) Thing 1 | | 0
6) Thing 2 | | 3
7) Thing 3 | | 0
8) Thing 4 | | 1
Sheet 2
Row# Column B | Column C | Column D
=================================
5) Thing 2 | 3 |
6) Thing 4 | 1 |
7) | |
8) | |
EDIT #3
You can use either SMALL, LARGE function to return the values from Column B on your Sheet1.
Presume you have given the following names:
Sheet1ColB: Sheet1!B5:B100
Sheet1ColW: Sheet1!W5:W100
Here is the formula to be put in Cell B5 on your Sheet2. Please note it is an array formula so you need to press Ctrl+Shift+Enter to confirm.
{=IFERROR(INDEX(Sheet1ColB,SMALL(IF((Sheet1ColW>0)*(LEN(Sheet1ColW)>0),ROW(Sheet1ColW)),ROW()-4)-4),"")}
or
{=IFERROR(INDEX(Sheet1ColB,LARGE(IF((Sheet1ColW>0)*(LEN(Sheet1ColW)>0),ROW(Sheet1ColW)),ROW()-4)-4),"")}
You can then use INDEX+MATCH to return the value from Column W on your Sheet1 in Column C on your Sheet2:
=IFERROR(INDEX(Sheet1ColW,MATCH(B5,Sheet1ColB,0)),"")
In the above screen-shot Solution 2 is using AGGREGATE which follows the same logic as SMALL/LARGE.
As you can see the sample data has taken into account duplicated values in Sheet 1 Col W, blank cells in both Column W and Column B on Sheet1, and blank cells, negative value or 0 value in Column B only on Sheet1.
Cheers :)
Use AGGREGATE() formula to filter based on condition.
=IFERROR(INDEX($A$5:$A$8,AGGREGATE(15,6,(ROW($A$5:$A$8)-ROW($A$4))/($B$5:$B$8>0),ROW(1:1))),"")

Excel VBA sum(sumifs) within sumproduct only works with F9

Situation:
Row 1: Variable | Margin | Sales
Row 2: banana | 2 | 10
Row 3: apple | 5 | 20
Row 4: apple | 10 | 20
"Variable" sits in column A, row 1.
Sales = name range for variable column i.e. A2:A4
Fruit = name range for "apple" and "banana" i.e. {apple;banana}
I have the above table and what to find the weighted average Margin. The weighting is based on sales.
Formula used: =SUMPRODUCT(B2:B4,C2:C4/(SUM(SUMIFS(C2:C4,sales,fruit))))
Problem:
When I use this formula I get #DIV/0!
If i step into the formula and press F9 I get 6.40 which is the correct answer.
Could someone please tell me what I am doing wrong? How can I get 6.40 without stepping into the formula with F9.
Note:
I would like to avoid using this formula: =SUMPRODUCT(B2:B4,C2:C4/(SUM(SUMIFS(C2:C4,sales,{"apple","banana"}))))
The range of fruit included in the calculation is dynamic. Sometimes I'd like to include only banana, sometimes only apple and I could make it dynamic by changing the cells within the fruit named range.

COUNTIF range does not equal range

The data I'm looking to get is something like this:
+----------+-------+
| Location | Count |
+----------+-------+
| Jungle | 2 |
| Ocean | 4 |
| Other | 2 |
+----------+-------+
The formula for count should look in the data range and if a cell's data does not equal any of the cell data in the rest of the location column (Jungle, Ocean) it should add that to the count. It should exclude blanks.
This is the formula I tried:
=COUNTIF(A1:H6,"<>"&E11:E12)
Here is my example sheet: https://docs.google.com/spreadsheets/d/1YbqEwa3olEXMcmU-UORfe7jwyK_-AXDomp1t5iTVUaU/edit?usp=sharing
Where am I going wrong? There are 7 other instances of colours not Green or Blue therefore I would expect this result to be output. I haven't put anything in about ignoring spaces so I would expect the result to count blanks too, but I get 0.
Don't use & - this would join E11 and E12 together so you would be looking for greenblue. You would need a COUNTIFS to get the three conditions
=COUNTIFS(A1:H6,"<>"&E11,A1:H6,"<>"&E12,A1:H6,"<>")
Here is a more general way of doing it
=COUNTA(A1:H6)-SUMPRODUCT(COUNTIF(A1:H6,E11:E12))
See this useful reference
The COUNTIFS formula is your friend here. This formula should work for that cell:
=COUNTIFS(A1:H6,"<>"&E11,A1:H6,"<>"&E12,A1:H6,"<>")
The problem with a 'does not equal OR does not equal' is that when it is not equal to one, it could be equal to the other and vice-versa. Additionally, you range to count from is 2 dimensional so you cannot use rows of criteria. Use SUMPRODUCT and individual criteria multiplied against each other.
=SUMPRODUCT((A1:H6<>"")*(A1:H6<>E11)*(A1:H6<>E12))
Any of those three conditions that is not true is considered zero. Anything multiplied by zero is zero. 1 x 1 x 1 is 1.
For large numbers of exclusions, you will want to use an array formula (with CSE) to count them and then subtract that total from a COUNTA total of the entire range.
=COUNTA(A1:H6)-SUM(COUNTIF(A1:H6,E11:E15))

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