Excel search number search in columns and rows - excel

Would like to find out how many times (rows) between matching numbers. So if F8 has a 14, when did the next 14 come up in the range of F9:J1000. So columns and rows to do a search in. So if the next 14 is at G11, the number would be 2.

Using the exemple you provided wiht lookup value = 14 and the range = F9:J1000, you could use an array formula (this means you need to press Ctrl+Shift+Enter on Excel 2013 and earlier) to find the distance between the first and the last match using this method.
Step 1:
Create an array that return the row of the match (if there is one) or zero.
=IF(F9:J1000=14,ROW(F9:J1000)-ROW($F$9)+1,0)
Step 2:
Calculate the smallest non-zero value in that array (from step 1) using this formula:
SMALL([The array],SUM(IF([The array]=0,1,0))+1)
Step 3:
You can find the maximum value in the array using the MAX function
MAX([The array])
Step 4:
Take the result from Step 3 minus the result from Step 2 and you got the difference in row between the highest and lowest match.

Related

Two-way lookup to return value or next closest value in Excel

I have a three-column table in Excel, called Table1, like this:
Given two values (one for each input variable), one which must be exactly equal to any of the numbers in the first column (2, 4, 6, or 8) and which must be typed in cell F2, and another one which can be any number between the least (1) and greatest (25) numbers in the second column and which must be typed in cell F3, I want to find the corresponding value in the third column. If the value typed for the second variable is not present in the second column of the table, then the output value of the next row is chosen.
For example, suppose the lookup values are 4 (for the first column) and 10 (for the second column), then the output should be E, since both 4 and 10 are present in the first and second columns, respectively, and the row with the output E corresponds to those values for the inputs.
Another example. Suppose the lookup values are 8 (for the first column) and 17 (for the second column), then the output should be K; it is not J because the latter corresponds to a value of 15 for the second column, which is strictly less than 17; so the output is K because it corresponds to the value that is immediately after (or greater than) 17, being 20.
My attempt
To limit the available values the user can choose, I could create data-validated cells. For choosing the values in the first column, the data validation would by of type list and be equal to 2, 4, 6, 8; such cell would be F2. Like this:
For choosing the values in the second column, the data validation would be of the type whole number, with minimum value of 1 and maximum value of 25. Like this:
Now the formulas for the lookup. After googling, I found out that performing a look-up task with two input criteria is known as a two-way lookup. Using the INDEX and MATCH functions, I managed to perform the two-way lookup, unfortunately the formula only allows exact matches, so it works fine when the first and second input values are 4 and 10, but not when they're 8 and 17. The formula is the following, and it is in cell F4:
{=INDEX(Table1[Output], MATCH($F$2 & "|" & $F$3, Table1[1st input variable] & "|" & Table1[2nd input variable], 0))}
(The presence of curly braces means that we must enter the formula with Ctrl + Shift + Enter instead of just Enter.)
Here's a screenshot for the first successful example:
Here's a screenshot for the second failed example:
I tried changing the third parameter of the MATCH function from 0 to 1, but it returns J (which corresponds to 15 in the second column, but 17 < 15) instead of K (which corresponds to 20, since 17 > 20 and 20 is the closest value to 17 that is immediately after it.)
How can I achieve what I want?
if you have Excel 365 then you can use the new Filter-function:
=INDEX(FILTER(Table1[output],(Table1[1st Input variable]=first)*(Table1[2nd input variable]>=second),"no result"),1)
I named F3 "first" and F4 "second".
FILTER returns all output values where
column A = value from F3
column B >= the value from F4.
INDEX selects the first row of the FILTER-result
Not the best way, but you can round the second input to what you need. In your example, all your values are multiples of 5. Just create an exception for number 1 with an IF.
Here's what I tried:
={INDEX($C$1:$C$12;MATCH(F7&IF(ROUNDUP(G7/5;0)=1;1;ROUNDUP(G7/5;0)*5);$A$1:$A$12&$B$1:$B$12;0))}
Notice it's an array formula.

Reference cells with no gaps to fill table with gaps

I have included 2 tables below to illustrate my problem.
Table 1
Table 2
I am trying to find a formula that fills rows 140, 143 & 146 (Table 2) from rows 15,16 & 17 (Table 1). There is over 100 so it is quite time consuming to input =B15 etc over and over again.
The offset method e.g. =OFFSET($B$15,(ROW()-1)*3,0) only works when I'm referencing gaps, not trying to fill them.
Essentially, where B140's formula is =B15, B143's will be =B140 + 1 row i.e. B16
Thanks for your help!
If you are trying find value for appropriate month you can use INDEX/MATCH entered as array formula:
=IFERROR(INDEX($B$1:$B$4,MATCH(TRUE,MONTH(A10)=MONTH($A$1:$A$4),0)),"")
Array formula after editing is confirmed by pressing ctrl + shift + enter
Edit
To find by month & year use:
=IFERROR(INDEX($B$1:$B$4,MATCH(1,(MONTH(A10)=MONTH($A$1:$A$4))*(YEAR(A10)=YEAR($A$1:$A$4)),0)),"")
it's also array formula
You can use modulo for this. With the Modulo function, you check if the remainder of the row you're on is divisible by a number (e.g. 3 if you want to copy a value every third row). IF(MOD(ROW(E1);3 = 0)
If that's the case, you can divide by 3 and use for example the Index function to copy the nth value of another location (or another worksheet). If that's not the case, you print "" to get an empty row.
=IF(MOD(ROW(E1);3)=0;INDEX($B$1:$B$4;ROW(E1)/3);"")
If you're working with offsets because the row numbers are not on numbers divisible by three, you could manually offset the rows (and do the same for the division that yields the index row). For example, if you want to have rows 2, 5, 8 etc:
=IF(MOD(ROW(E1)+1;3)=0;INDEX($B$1:$B$4;ROW(E2)+1/3);"")

Return the score of a row based on if a column value is in a table

I am looking for a way to automate this process with VBA. Currently I am performing this "scoring" with helper cells (not VBA).
I am trying to score a row based on the contents of the values in it. If the value in that row is equal to a value in anyone of the defined tables, then it will follow a simple scoring rubric:
Scent = 7
Pigment = 6
AO = 5
MC = 4
PPA = 3
Antistat = 2
If the value is not located in a table, simply return a "1"
Scoring row_2, is detailed in row 3. The formula in cell A3 is:
=IF(COUNTIF(Scent,A2)>0,7,IF(COUNTIF(Pigment,A2)>0,6,IF(COUNTIF(AO,A2)>0,5,IF(COUNTIF(MT,A2)>0,4,IF(COUNTIF(PPA,A2)>0,3,IF(COUNTIF(Antistat,A2)>0,2,1))))))
Which returns the value of "7" because M14301855 is a value that resides in the "scent" table.
Cell G3 is what actually details the "score" that the row receives. Which is "largest value"."2nd largest value":
=VALUE(LARGE(A3:F3,1)&"."&LARGE(A3:F3,2))
Which returns the value of "7.5" because the row has a "scent" match and an "AO" match.
I can perform this task successfully with helper cells, I am looking for a way to accomplish this with some VBA script. I imagine that this can be accomplished in one way or another with an array, but I am still learning VBA and am unsure how to proceed.
THANK YOU IN ADVANCE FOR ANY HELP!
An array formula should do it for you (confirm with Ctrl+Shift+Enter):
=SUM(LARGE(CHOOSE({1,2,3,4,5,6},OR(A2:F2=Scent),OR(A2:F2=Pigment),OR(A2:F2=AO),OR(A2:F2=MC),OR(A2:F2=PPA),OR(A2:F2=Antistat))*{6,5,4,3,2,1}+1,{1,2})*{1,0.1})
Here's a breakdown:
Use OR to check if any of your values are in each table.
CHOOSE with {1,2,3,4,5,6} for the index_num argument the combines all the comparisons from step 1 into a single array.
Using {1,2} for the k argument in LARGE will return an array of the 2 largest values in the array from step 2.
Multiply the array from step 3 by {1,0.1} to put the second score in the tenths place.
Sum the array from step 4.
Note: I used MC for the MC table instead of MT like you had in your formula.
UPDATE: If your codes always follow the "M00000000" pattern and your tables are always all the same length, this formula is a little simpler (still confirm with Ctrl+Shift+Enter):
=SUM(LARGE(IFERROR((FIND(A2:F2,Antistat&PPA&MC&AO&Pigment&Scent)+17)/9,1),{1,2})*{1,0.1})
If the tables aren't necessarily the same length, you could pad the bottom of them with e.g., M00000000.

Microsoft Excel getting element in previous row in the adjacent column

I have a data set in Excel similar to the following:
A B
1: 07:42:07 2
2: 07:42:08 3
3: 07:42:09 4
4: 07:42:10 5
5: 07:42:11 6
6: 07:42:12 7
7: 07:42:13 8
Given a particular time, I would like to extract the value which is diagonal to the specific time. For example, given the value 07:42:10 (at cell A4), I would like to get the value 4 (at cell B3), which is in the previous row and in the next column. I need to be able to pass the time value to the function, so that the respective value in column B will be shown as explained in the example.
Is there a function which will allow me to do this please?
Thank you
Just offset the range being searched and the result range.
eg:
=INDEX(B1:B6,MATCH(G1,A2:A7,0))
Try this:
=IFERROR(INDEX($B$1:$B$7,MATCH([your time value],$A$1:$A$7,0)-1),"Out of Range")
Replace [your time value] with the value you're looking to match.
I added the IFERROR function as the value 07:42:07 would return an error otherwise.
This function will take [your time value], look for an exact match in $A$1:$A$7, and return the row number where it is found. We then subtract 1 from the row to get the previous row up (let's call it row x). Now having found our row, we ask the INDEX function to return the value from Column B, Row x.

Search a date in multiple date ranges and return the corresponding value

In sheet 1, I have a list of dates in Column A in chronological order. There are values corresponding to this list in Column B. There is another list of dates in sheet 2 and I want to add values from sheet 1 to these dates.
Sheet 1.
**Column A Column B
DATE Amount**
1. 10/01/2015 25,60,000
2. 10/02/2015 26,80,000
3. 01/03/2015 21,55,000
4. 30/03/2015 24,60,500
5. 30/04/2015 28,20,000
6. 30/06/2015 19,00,000
Sheet 2.
Column A Column B
1. 21/02/2015 21,55,000
2. 15/01/2015
3. 20/05/2015
4. 25/04/2015
For example: I need to look up 21/02/2015 in sheet 1 and column A and return the value corresponding to the next available date. So for 21/02/2015 I need the value corresponding to the next date available which is 01/03/2015 and the value is 21,55,000. If its 15/01/2015 I need the value of 10/02/2015 i.e. 26,80,000
What formula could I use for this?
You could use VLOOKUP, but it has some issues. So it is better to use INDEX and MATCH combination. In your case try this
=INDEX('Sheet 1'!$B:$B,MATCH(A1,'Sheet 1'!$A:$A,-1))
Sorry, my previous answer works only for descending order. Try this instead
=INDEX('Sheet 1'!$B:$B,MATCH(TRUE,('Sheet 1'!$A:$A-A1)=MIN(IF('Sheet 1'!$A:$A-A1>=0,'Sheet 1'!$A:$A-A1)),0))
Explanation: I hope that INDEX and MATCH are well explained in Office Support.
About the condition:
('Sheet 1'!$A:$A-A1)=MIN(IF('Sheet 1'!$A:$A-A1>=0,'Sheet 1'!$A:$A-A1))
What it means?
'Sheet 1'!$A:$A-A1
results in a difference between the value in the cell A1 and the cell in A column in Sheet 1.
MIN(IF('Sheet 1'!$A:$A-A1>=0,'Sheet 1'!$A:$A-A1))
says that if the difference is non-negative ('Sheet 1'!$A:$A-A1>=0), find the minimum of such numbers (MIN function).
And if these numbers are equal (MATCH function), then pick the corresponding number in column B (INDEX('Sheet 1'!$B:$B,...)).
Apology: In my previous answers I swapped the columns of your example. I hope it is now correct.
You can use vlookup with True rather than the widely used form with False
As ExcelEfendisi said you can use vlookup with range lookup enabled. A simple way to get the value at the next date rather than the prior one would be to push all the amount values down one row, but to avoid that it might be better to repeat the index values - like this
1 10/01/15 25,60,000 1
2 10/02/15 26,80,000 2
3 01/03/15 21,55,000 3
4 30/03/15 24,60,500 4
5 30/04/15 28,20,000 5
6 30/06/15 19,00,000 6
Then you can use two vlookups, the first one to get the index of the row with the date prior to the date you are interested in and a second one to extract the balance value for the subsequent date - not very elegant but it would work
Try this formula (enter in Sheet2 cell B2 then copy till the last record)
=INDEX(Sheet1!$B:$B,1+MATCH($A2,Sheet1!$A:$A,1),1)
As data is sorted in ascending order use MATCH with match type 1 (less than) to obtain the row above the high next item, then add 1 and the result is the high next row, use this row to get the corresponding record from the column B with formula INDEX

Resources