Solved

Excel formula looking for value between 2

Posted on 2013-11-06
7
575 Views
Last Modified: 2013-11-07
Hi,
I have a document and I want to use an IF function and don't know how.
Here's how it goes
I have on a sheet "table" those column
A    B    C    
0    5    17  
6   10   43  
11 15   33
16 20   2

Then on the other sheet "info" I have
A
3
7
11
19
I want a formula that says on sheet "info" look at cell A1 and return the "table" C number where A1 on "info" is comprise between number of A1 A2 on the "table" sheet.
So in my example
A1 = 3, 3 is between 0 and 5 so return 17
A2 = 7 and that's between 6-10 so return 43.
A3 = 11 and that's between 11 and 15 so return 33
A4 - 19 and that's between 16-20 so return 2.

I hope that this is clear enough ;-)
tx!
0
Comment
Question by:philjans
  • 4
  • 3
7 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 500 total points
ID: 39628674
Consider a VLOOKUP formula like:
=VLOOKUP(A1,table!A$1:C$4,3)

Note that the data on worksheet table will need to be sorted in ascending order by column A.
VLOOKUP-Q28287281.xlsx
0
 
LVL 81

Expert Comment

by:byundt
ID: 39628981
If you insist on using an IF function in the formula, the following array-entered formula will work. I much prefer the VLOOKUP, however.
=INDEX(table!$C$1:$C$4,MAX(IF(A1>=table!$A$1:$A$4,ROW(table!$A$1:$A$4),"")))

To array-enter a formula, click in the formula bar, hold the Control and Shift keys down, then hit Enter. Release all three keys. Excel should respond by adding curly braces { } surrounding your formula.
0
 

Author Comment

by:philjans
ID: 39630238
If vlookup is better, i'll go for it: what's that formula?
tx!
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 81

Expert Comment

by:byundt
ID: 39630254
I gave you the VLOOKUP formula and a sample workbook in my first post.
0
 

Author Comment

by:philjans
ID: 39630370
Hi byndt, it works good.
But there is something I don't understand and it's ... why?
I am used to a vlookup that says "if you find this value in this table please report the value contained in column x of the same table"
How does the formula now figures out I am looking for a number not textually identify but that is located between colunm a and b which has numbers textually identified in them
"here's a value, locate where it is located between a range of numbers in colunm A and B and then report..."
It's the "between 2 numbers" I don't see how excel finds it. And why does it assume it's between numbers in column A and B and not say B and D or E or F (in my original tables the value to return is located in column G and).
tx for the clarification
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39630696
There are two forms of VLOOKUP.

The one you are used to is called the "four parameter" or "exact" form of VLOOKUP. It searches the first column of a lookup table for an exact match to the first parameter, then returns a value from the same row. It performs this search row by row, and does not require the lookup table to be sorted by the first column.
=VLOOKUP(A2, lookupTable, 3, FALSE)        find A2 in first column of lookupTable, then return value from third column

The other one is called the "three parameter" or "approximate" form of VLOOKUP. It also searches the first column of a lookup table, but looks for the largest value equal or less than the first parameter. It then returns a value from the same row. This form of VLOOKUP requires that the lookup table be sorted by its first column, and it performs a binary search. The three parameter form of VLOOKUP has two principal uses: bracket pricing and really fast lookups when you know that the first parameter will always be in the lookup table.

Your problem is like bracket pricing, and so is a good candidate for the three parameter form of VLOOKUP. In bracket pricing, the first column of the lookup table should contain the bottom (lowest number) in each bracket. The top (highest number) in each bracket is assumed to be just a smidge less than the value in the next row of the first column. So the VLOOKUP is not looking directly for a value between a range of numbers in columns A and B of the lookup table, but it accomplishes the same end. Both of the following formulas apply to a bracket pricing type of problem
=VLOOKUP(A1,table!A$1:C$4,3)              If fourth parameter is omitted, it is assumed to be TRUE
=VLOOKUP(A1,table!A$1:C$4,3,TRUE)


Just to complete the picture, binary search means to repeat a process of testing the middle of a range to see if the value is higher or lower than the one being sought. Suppose you have a million rows in your lookup table. Rather than searching each one, you first check the value in row 500,000. If it is larger than the value you want, your answer must be in the first 500,000 rows. If it is smaller than the value you want, your answer must be in the last 500,000 rows. You then repeat the process on the smaller range found as a result of the first test. With 21 tests, you will find the desired match--an impressive improvement over searching a million rows one at a time.

The problem with binary search when you need an exact match is if you are looking for a value that falls between two values in column A. Instead of giving you an error that you can trap with ISNA or IFERROR, it gives you the result from the row at the bottom of that bracket.
0
 

Author Closing Comment

by:philjans
ID: 39630866
Perfect explanation, you're a good teacher, thanks for taking the time!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question