[QUOTE=Sal Ammoniac]
Yep, the function you want is called VLOOKUP, and it works like this:
In cell A1, you put 20, as you say, and then in cell A2 put the following formula: VLOOKUP(A1,‘CompanyLookup’!$A$1:$B$100,2,FALSE).
[/QUOTE]
To piggyback on Sal’s excellent suggestion here, you could also replace the cell references ($A$1:$B$100) above with column references, like so: $A:$B. This results in Excel looking at the entire column instead of just the 100-row range it was limited to before, and means that later on down the road you don’t have to go back and figure out why it suddenly stopped working (when you get past the end of the original range, it won’t find anything any more).
Also, by playing with the other lookup functions in Excel, and the order your CompanyLookup sheet is sorted in, you can achieve a portion of the range lookup you were looking for earlier. You’d do this in another cell, of course – A3, I think you said – but the formula(s) are similar.
For example, IF you sort your CompanyLookup sheet so that all numbers are in numeric order, and IF each company has a continuous range assigned to it, then the following formula in cell A3 would give you a range result like you describe:
=INDEX(CompanyLookup!$A:$A,MATCH($A$2,CompanyLookup!$B:$B,0)) & IF(COUNTIF(CompanyLookup!$B:$B, $A$2)>1, " - " & INDEX(CompanyLookup!$A:$A, MATCH($A$2, CompanyLookup!$B:$B,0)+COUNTIF(CompanyLookup!$B:$B, $A$2)-1), “”)
On my test sheet I’ve set up ‘ABC Co’ to have numbers from 20 - 22, and the result I get in cell A3 using this formula is “20 - 22”.
What the function I’ve described is doing is straightforward, it just appears complex. It relies on you having the CompanyLookup sheet sorted correctly, so if that can’t be done then feel free to ignore this. There ARE other ways to do what you want without resorting to VB code, but it gets much more complex and may be impractical for your situation.
First, it’s using the INDEX() function coupled with the MATCH() function to perform something very similar to a VLOOKUP. VLOOKUP only works in one direction, left to right, so if you want to find something in a column to the right and return a value to the left, you have to use this method instead. So this is all it does to return the first match found for the company name.
Next, it counts how many times the company name appears in the CompanyLookup tab. If this number is more than 1, then it knows that it needs to display a range rather than a single value. Using the same INDEX() and MATCH() method as before, it (1) finds the position of the first entry again, (2) adds the number of entries for that company to that position, (3) removes 1 from that number so it won’t go past the end of the range, and finally (4) returns the value from that position in column A.
Whew!
Again, if you can’t sort your company data like I’ve described, this method won’t work properly. There ARE other ways to achieve what you’ve described, but it can get pretty messy, formula-wise. If you need to do it, though, just speak up. I’d be happy to help.