Excel - Recursive VLookup - excel

I recently came to know about Ctrl+Shift+Enter array formulas of Excel and currently still learning them. Coming to my problem,
SheetA:
Product Code
S1 19875
S2 19834
S1 13575
S1 35675
S2 47875
SheetB:
Code Indent
19875 40
19834 15
13575 22
35675 25
47875 20
I need to do Sum of all Indents of a given Product name.
Eg: I need S1's Total Indent,
Vlookup on SheetA, Get the Code 19875
Perform a vlookup on SheetB, Get the Indent of 40
Next Vlookup on Sheet A, Get the code 13575
Use 13575 to Vlookup on SheetB, Get Indent of 22
Next Vlookup on Sheet A, Get the code 35675
Use 35675 to Vlookup on SheetB, get indent of 25
Sum of 40+22+25, Return 87
I can achieve this through VBA, but I'm wondering if this is possible within excel functions using CSE/Array formulas.
Edit:
I don't have values in Sheet2 in the same order of Sheet1.. They are completely random. My SheetB will be something randomly like following:
SheetB:
Code Indent
19834 40
19875 15
47875 22
13575 25
35675 20

{=SUM(NOT(ISNA(MATCH((($A$2:$A$6="S1")*(B2:B6)),Sheet2!$A$2:$A$6,FALSE)))*(Sheet2!$B$2:$B$6))}
The first argument of the MATCH resolves to
{19875;0;13575;35675;0}
The MATCH resolves to
{1;#N/A;3;4;#N/A}
You'll have to make sure you don't have zeros in SheetB. The NOT ISNA turns those into TRUEs and FALSEs and resolves to
{TRUE;FALSE;TRUE;TRUE;FALSE}
And the final SUM looks like this
=SUM({TRUE;FALSE;TRUE;TRUE;FALSE}*{40;15;22;25;20})
Update
I can't figure out a single-array solution when the lists are in a different order. My attempts with OFFSET and TRANSPOSE either gave the wrong answer or crashed Excel. If you can stand using a helper column, you could put this formula in third column of your first sheet
=VLOOKUP(B2,Sheet2!$A$2:$B$6,2,FALSE)
and then use this array formula to sum them up
{=SUM(($A$2:$A$6=A2)*($C$2:$C$6))}

If the Code column were identical (same order) on both SheetA and SheetB, a simple SUMIF function would do. Similarly, if the INDENT data were on SheetA, you could also use a pivot table to calculate the sums quickly.
I'm guessing the design of your workbook won't allow this though. In which case, I don't have any easy solutions, but I'll give it another look.

You could use the below:
//Ola.S
Table 1
Col:A B C
Product Code Intendent
Col C: =SUMIF($E$4:$E$8;B4;$F$4:$F$8)
Table 2
Col:E F
Code Intendent
Table 3
Pivottable (Col:A and C)

On sheetC
If A1 has S1 then in Cell B1 type
=SUM((IF((Product=A1),Code1)=TRANSPOSE(Code2))*TRANSPOSE(Amt))
Array entered
Where
Code1 = Dynamic Range for Codes on SheetA starting from Row2
Code2 = Dynamic Range for Codes on SheetB starting from Row2

No TRANSPOSE calls needed.
Try the array formula
=SUM(SUMIF(SheetB!A2:A6,IF(SheetA!A2:A6="S1",SheetA!B2:B6),SheetB!B2:B6))
Assuming no cell in SheetB!A2:A6 it evaluates to FALSE. If the Code columns were all numeric (probably a questionable assumption), this could be done without an array entry using:
=SUMPRODUCT(SUMIF(SheetB!A2:A6,SheetA!B2:B6/(SheetA!A2:A6="S1"),SheetB!B2:B6))
Assuming no cell in SheetB!A2:A6 evaluates to #DIV/0!.

Related

Use a variable for columns in SUMPRODUCT and VLOOKUP formula

I'm trying to use this formula:
=SUMPRODUCT(VLOOKUP(B$4778,$D$4:$DC$4623,{4,5},0))
It works fine but I'd like to try to use a variable for the {4,5} portion of the formula (columns in the array to be summed) as the formula needs to change based on sheet inputs before this formula.
I have cells on the sheet that are to be used to set the initial and final columns to be searched (likely 10 columns, but the 10 columns would have to be selected from 90 some columns available).The columns are populations related to each age. So, if I need population of those aged 10 through 15, I'd need to sum up 5 columns. If 20-25, need to sum up 5 different columns.
I tried to use the Columns function but it didn't seem to work for me.
The columns are selected by users entering in cells the upper and lower limits of the search range and then I convert those values to the corresponding numerical column value.
So if they select 5 as lower and 10 as upper limit, I know I have to add 7 to get the correct data column on the data page (column 12) and likewise for upper (column 17).
The entire possible area to search is $D$4:$DC$4623. So, in the formula, if I wrote it out long way it would be:
=SUMPRODUCT(VLOOKUP(B$4778,$D$4:$DC$4623,{12,13,14,15,16,17},0))
I'd prefer to write it out using variables, something like this:
=SUMPRODUCT(VLOOKUP(B$4778,$D$4:$DC$4623,{L:U},0))
Where variable L would be 12 and variable U would be 17.
Can anyone suggest a way to write the formula?
use this array formula:
=SUM(VLOOKUP(B$4778,$D$4:$DC$4623,ROW(INDIRECT(D5 & ":" & E5)),FALSE))
Where D5 is the Lower and E5 would be the upper.
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then excel will put {} around the formula.
Or better yet use this non array formula:
=SUM(INDEX($D$4:$DC$4623,MATCH(B$4778,$D$4:$D$4623,0),D5):INDEX($D$4:$DC$4623,MATCH(B$4778,$D$4:$D$4623,0),E5))

Find a range of value in excel

I have two different sheets with 300,000 data in Excel.
First sheet contains:
S2_Symbol Start_Pos End Position
STE 254857 267891
PRI 748578 758962
ILA 852741 963369
VIS 789456 796325
Second:
S1_Location
789460
852898
748678
My output should be like this:
S1_Location Symbol
789460 VIS
852898 ILA
748678 PRI
I have to find that S1_location falls in which S2_location and its corresponding Symbol. I have used INDEX formula in Excel but for each cell, I have to change the reference cell manually. I couldn't do it 300,000 data.
How can I do in an in Excel or should I use a script?
This solution assumes the following:
Start and End Positions for each S2 Symbol are unique (i.e. there is no intersection between the ranges allocated to each symbol)
Data in first sheet is located at A1:D17 (adjust ranges in formulas as needed)
Data in second sheet is locate at A1:B300010 (adjust ranges in formulas as needed)
The solution requires:
To add a working column in worksheet one. Enter this formula in D2 and copy till last record.
=ROWS($A$1:$A2)
Fig. 1
Then in second worksheet enter this formula at B2 and copy till last record.
=INDEX( Sheet1!$A$1:$A$17,
SUMIFS( Sheet1!$D$1:$D$17,
Sheet1!$B$1:$B$17, "<=" & $A2, Sheet1!$C$1:$C$17, ">=" & $A2 ) )
Fig. 2
It took aprox. less than 14 seconds to copy downwards and calculate the formulas in sheet 2.
As it can be seen in figures 1 and 2 none of the tables need to be sorted.
Assuming both sheets start in A1, and First sheet ColumnB is sorted ascending, in Second sheet B2 please try:
=INDEX(First!A:A,MATCH(A2,First!B:B))
copied down to suit. It relies on inexact matching.
Assuming we have a Sheet1 like this:
note, the Sheet1is sorted by Start_Pos, End_Pos in ascending order.
and a Sheet2 like this:
Then the formula in Sheet2!B2 downwards could be:
=INDEX(Sheet1!A:A,IF(MATCH(A2,Sheet1!B:B)>IFERROR(MATCH(A2-(10^-10),Sheet1!C:C),0),MATCH(A2,Sheet1!B:B),NA()))
See MATCH: https://support.office.com/en-us/article/MATCH-function-e8dffd45-c762-47d6-bf89-533f4a37673a
The idea is: MATCH without exact matching (without parameter match_type) gets the row of the largest value which is smaller or equal the search value. So in the Start_Pos column it will get the row from which we can get the S2_Symbol. But from the End_Pos column it should get one row beforehand if the value is not outside the given ranges.
There is only one exception. If the value is exact the value in the End_Pos column, then it will return the same row as in the Start_Pos column. Considering this exception, we can search in the End_Pos column with a little bit smaller value. Thanks to Tom Sharpe for his comment.
The formula in Sheet2!D2 downwards is:
{=INDEX(Sheet1!A:A,MIN(IF($A2>=Sheet1!$B$2:$B$300000,IF($A2<=Sheet1!$C$2:$C$300000,ROW(Sheet1!$A$2:$A$300000),2^20+1))))}
this is an array formula which is exactly formulated respecting the requirements. But this is very bad in performance for using in much many cells. But using this, the Sheet1 is not required to be sorted.
Benchmark test:
Have the following Sheet1:
Formulas:
A2:A300002: ="S"&(ROW(A1)-1)*10&"-"&(ROW(A1)-1)*10+7
B2:B300002: =(ROW(A1)-1)*10
C2:C300002: =B2+7
and the following Sheet2:
Formulas:
A2:A300002: =RANDBETWEEN(0,3000007)
B2:B300002: =INDEX(Sheet1!A:A,IF(MATCH(A2,Sheet1!B:B)>IFERROR(MATCH(A2-10^-9,Sheet1!C:C),0),MATCH(A2,Sheet1!B:B),NA()))
Note the -10^-9 instead of -10^-10 in previous version. This is because we have only 16 digits precision. In previous version this was maximum 6 digits integer part and then 10 digits decimal part. Now it is maximum 7 digits integer part and then 9 digits decimal part.
Calculation after pressing F9 in Sheet2 takes ca. 2 s. (Excel 2007, Windows 7, 4 core processor).
I would have gone for something like this which gives you the first match if there is one:-
=INDEX(First!A:A,MATCH(1,(First!B:B<=A2)*(First!C:C>=A2),0))
assuming keys and start and end values are in a sheet called First and lookup values start in A2.
Array formula which must be entered with CtrlShiftEnter
In response to the question from #pnuts about how long it will take, I have set up a similar benchmark with 300,000 rows in each sheet and it has reached 1% after 90 minutes, so it should take about 150 hours to reach 100% or roughly one week. This is to be expected as the number of computations required is (rows in sheet 1) X (rows in sheet 2)
300,000 X 300,000
but in fact because the multiplication applies to complete columns, I believe it is more correctly
300,000 X 1,048,576
i.e. > 300 billion.
A practical version which gives good response for smaller ranges is as follows:-
I define three named ranges Range1, Range2 and Range3
=First!$A$1:INDEX(First!$A:$A,MATCH("ZZZ",First!$A:$A))
=First!$B$1:INDEX(First!$B:$B,MATCH(9.9E+307,First!$B:$B))
=First!$C$1:INDEX(First!$C:$C,MATCH(9.9E+307,First!$C:$C))
and the modified formula is
=INDEX(Range1,MATCH(1,(Range2<=A2)*(Range3>=A2),0))
I was thinking of deleting this answer, but would rather it stood as a counter-example.

Formula to return original value if specified character not found

I have two Excel sheets (let's say sheet1 and sheet2). For an example here, the values on sheet1 from R1C1 to R4C1 are 0(5), 2(6)*, 92and 44.
I want to copy just the numeric values from sheet1 to sheet2. Thus, I have this formula in R1C1 of my sheet2:
=LEFT(Sheet1!A1,SEARCH("(",Sheet1!A1)-1)
This formula returns the values on sheet2 like this:
R1C1 - 0
R2C1 - 2
R3C1 - #Value!
R4C1 - #Value!
The main issue is for R3C1 and R4C1.
Can we include a logical test in the formula which returns the value 92 for R3C1 and 44 for R4C1 if the formula cannot find "("?
I suggest you try =IF(ISNUMBER(Sheet1!A1),Sheet1!A1,"")
Only after editing the question do I now realise requirement seems to be for what may be served by a combination of OP's formula and mine above:
=IF(ISNUMBER(Sheet1!A1),Sheet1!A1,LEFT(Sheet1!A1,SEARCH("(",Sheet1!A1)-1))
This will returns text as before of 0 and 2 and numbers of 92 and 44.
yes, you can check for an error using ISERROR function:
=IF(ISERROR(LEFT(Sheet1!A1,SEARCH("(",Sheet1!A1)-1)),LEFT(Sheet1!A1,SEARCH("(",Sheet1!A1)-1))
so that is like an IF, THEN, ELSE block
IF LEFT(Sheet1!A1,SEARCH("(",Sheet1!A1)-1))= #VALUE
Then just take Sheet1!A1)-1
else take LEFT(Sheet1!A1,SEARCH("(",Sheet1!A1)-1)
that should get you started anyway
HTH
Philip

Find something in column A then show the value of B for that row in Excel 2010

Basically my problem is that I have a string in one cell in excel, I then need to see if that string exists in another row (not one cell but the whole row) and if so then print the contents of another cell in the same row but in another column.
I will give a basic example:
Title Answer
Police 15
Ambulance 20
Fire 89
Now I need to scan the title column for, say, "Police" and then populate the cell with the value under Answer (in this case 15).
I cant just say IF(A2="Police";B2;"" as I need the scan the whole of the Title column.
I have tried using IF(COUNTIF(A$2:A$100;"Police"); which scans the contents of A2 to A100 for the string Police, and know how to make it print a constant (just put something after the ;) but cant work out how to make that "constant" a variable that changes depending on the found row. So if the COUNTIF found Police in cell A44 then the answer to my formula would be B44, the same as if it found Police in A62 then my formula should show B62
I hope this makes sense and that someone can help me :)
Note that I am using excel 2010 and need a normal formula as I can not use scripting for this document.
EDIT:
Here is what I have so far, note that the spreadsheet I am using is far more complex than the "simple" example I have in the question...
=IF(ISNUMBER(FIND("RuhrP";F9));LOOKUP(A9;Ruhrpumpen!A$5:A$100;Ruhrpumpen!I$5:I$100);"")
This is showing "RuhrP" in every answer where "RuhrP" is found in F9 and not the answer I want which should be that found in RuhrPumpen!I$5:I$100 where the cell index is the same as that for the A coloum where A9 was found. Again, sorry for the complexity I cant think of any better way to word it.
I note you suggested this formula
=IF(ISNUMBER(FIND("RuhrP";F9));LOOKUP(A9;Ruhrpumpen!A$5:A$100;Ruhrpumpen!I$5:I$100);"")
.....but LOOKUP isn't appropriate here because I assume you want an exact match (LOOKUP won't guarantee that and also data in lookup range has to be sorted), so VLOOKUP or INDEX/MATCH would be better....and you can also use IFERROR to avoid the IF function, i.e
=IFERROR(VLOOKUP(A9;Ruhrpumpen!A$5:Z$100;9;0);"")
Note: VLOOKUP always looks up the lookup value (A9) in the first column of the "table array" and returns a value from the nth column of the "table array" where n is defined by col_index_num, in this case 9
INDEX/MATCH is sometimes more flexible because you can explicitly define the lookup column and the return column (and return column can be to the left of the lookup column which can't be the case in VLOOKUP), so that would look like this:
=IFERROR(INDEX(Ruhrpumpen!I$5:I$100;MATCH(A9;Ruhrpumpen!A$5:A$100;0));"")
INDEX/MATCH also allows you to more easily return multiple values from different columns, e.g. by using $ signs in front of A9 and the lookup range Ruhrpumpen!A$5:A$100, i.e.
=IFERROR(INDEX(Ruhrpumpen!I$5:I$100;MATCH($A9;Ruhrpumpen!$A$5:$A$100;0));"")
this version can be dragged across to get successive values from column I, column J, column K etc.....
Assuming
source data range is A1:B100.
query cell is D1 (here you will input Police or Fire).
result cell is E1
Formula in E1 = VLOOKUP(D1, A1:B100, 2, FALSE)
I figured out such data design:
Main sheet:
Column A: Pump codes (numbers)
Column B: formula showing a corresponding row in sheet 'Ruhrpumpen'
=ROW(Pump_codes)+MATCH(A2;Ruhrpumpen!$I$5:$I$100;0)
Formulae have ";" instead of ",", it should be also German notation. If not, pleace replace.
Column C: formula showing data in 'Ruhrpumpen' column A from a row found by formula in col B
=INDIRECT("Ruhrpumpen!A"&$B2)
Column D: formula showing data in 'Ruhrpumpen' column B from a row found by formula in col B:
=INDIRECT("Ruhrpumpen!B"&$B2)
Sheet 'Ruhrpumpen':
Column A: some data about a certain pump
Column B: some more data
Column I: pump codes. Beginning of the list includes defined name 'Pump_codes' used by the formula in column B of the main sheet.
Spreadsheet example: http://www.bumpclub.ee/~jyri_r/Excel/Data_from_other_sheet_by_code_row.xls
Guys Its very interesting to know that many of us face the problem of replication of lookup value while using the Vlookup/Index with Match or Hlookup.... If we have duplicate value in a cell we all know, Vlookup will pick up against the first item would be matching in loopkup array....So here is solution for you all...
e.g.
in Column A we have field called company....
Column A Column B Column C
Company_Name Value
Monster 25000
Naukri 30000
WNS 80000
American Express 40000
Bank of America 50000
Alcatel Lucent 35000
Google 75000
Microsoft 60000
Monster 35000
Bank of America 15000
Now if you lookup the above dataset, you would see the duplicity is in Company Name at Row No# 10 & 11. So if you put the vlookup, the data will be picking up which comes first..But if you use the below formula, you can make your lookup value Unique and can pick any data easily without having any dispute or facing any problem
Put the formula in C2.........A2&"_"&COUNTIF(A2:$A$2,A2)..........Result will be Monster_1 for first line item and for row no 10 & 11.....Monster_2, Bank of America_2 respectively....Here you go now you have the unique value so now you can pick any data easily now..
Cheers!!!
Anil Dhawan

Excel: check all columns have a different value

I would like to know how to compare for every row that their (numeric) columns have different values or all of them are equal, and get the result (either a boolean or a 0/1) in another column. Something like:
colA colB colC Result
row1 12 12 45 false
row2 67 67 67 true
How could I write this formula concisely in Microsoft Excel? Note that the number of columns may vary and at least I have about 30 columns, so I don't want to name the columns in the formula, but write something like a for
Thank you.
If you want to test only numbers, what about?
=MIN(B1:B30)=MAX(B1:B30)
(if your range goes from 1 to 30)
for doesn't exist in Excel formula, the best way to deal with a for loop would be to use array formulas but this is not appropriated here.
what you may look for is a dynamic range but i can't see a way to implement it easily in your case
[EDIT] new formula thanks to Excelll's comment
=AND(MIN(B1:B30)=MAX(B1:B30),COUNTBLANK(B1:B30)=0)
If blank cells within a row are not an issue, #JMax offers a clever solution. However, if you want a blank cell to return "FALSE", you can use the following array formula:
=AND(EXACT(A1,B1:C1))
Enter this as an array formula by pressing Ctrl+Shift+Enter.

Resources