Extracting data with VLOOKUP. How to handle duplicates?

jakemelon

Board Regular
Joined
Mar 14, 2003
Messages
76
VLOOKUP only allows you to look up the first occurence of data. What if the first one is not the one I want to extract?

For example:


Name | Age | IQ |
--------------------------------
Bob 15 100
Mary 20 96
Bob 7 110


=VLOOKUP("Bob",A2:C4,3)

I am using VLOOKUP to find BOB and extract his IQ. But VLOOKUP will find the first instance of BOB and return 100 as the result.

I need it to show me the BOB #2 IQ because he is less than 10 years old. I'd like the result to be "110"

Sorry about this lame illustration, does it make sense as to what I am looking for? how would I accomplish this task?

Jake
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
What if creating unique data is not an option? All the lookup data will be imported and cannot be changed.
 
Upvote 0
Then you can use the array formula ...

{=INDEX($C$1:$C$3,MATCH(E1&F1,$A$1:$A$3&$B$1:$B$3,0))}

see the following simulation ...
Book2
ABCDEFG
1Bob15130bob7115
2Mary12119
3Bob7115
Sheet5
 
Upvote 0
Or, with the same data set in the earlier posts, you can use the following SUMPRODUCT formula ...

=SUMPRODUCT((A1:A3=E1)*(B1:B3=F1)*(C1:C3))
 
Upvote 0
Hi,

What if we could tell the LookUp Function what Instance of the LookUp Value to search For in the First Column.Wouldn't that be nice?
Certainly,this would give you much more control.

Well,to achieve this, I have created this UDF Called CustomVLookUpwhich takes an Extra Argument : 'Inst' which determins the Instance or Occurrence of the Value to look for in the First Column of the Table.

The 3 first arguments are the same as the Built in Excel VLookUp Function.

An additional advantage is that if a Match is not found then the Function returns the message : 'No Match'which is more Descriptive than Excel Error messages.

Below is the Code:

Code:
Public Function CustomVLookUp(vl, Table As Range, Col, Inst)
    On Error GoTo x
    Set SearchCol = Range _
    (Table.Cells(1, 1), Cells(Table.Rows.Count, 1))
    If Abs(Int(Inst))<> Inst Or Inst > _
    Application.CountIf(SearchCol, vl) Or Inst = 0 Then GoTo x
    Set mtch = SearchCol.Find(vl, LookIn:=xlValues)
    For i = 1 To Inst - 1
        Set mtch = Range(mtch, Cells(Table.Rows.Count, 1)) _
        .Find(vl, LookIn:=xlValues)
    Next
    CustomVLookUp = mtch.Offset(0, Col - 1)
    Exit Function
x:  CustomVLookUp = "No Match"
End Function

Below is a Worksheet Demo with Different Values for the Instance Argument including negative numbers and 0:
turedat.xls
ABCDEFGH
1NameAgeIQName:BOB
2BOB3010
3MARY2010InstanceFunctionResult
4BOB508-2NoMatch
5BOB1040NoMatch
6BOB1120110
7JOHN10228
8BOB681234
9ALI1001420
10MORAD61008NoMatch
11MARY3560512
12BOB44740.25NoMatch
13BOB1585785
14JOHN8455100NoMatch
15AMIR4085674
16ALI7768110
17
18
Sheet1


Hope this helps.
 
Upvote 0
Convert BOB to Bob1, Bob2, Bob3, etc

Column A Column B Column C Column D col. E

BOB =COUNTIF($A$1:A1,A1) =A1&B1 36 115
BOB
BOB
BOB
etc...


_____ =VLOOKUP(A8,A1:E4,5,)



Column A has 4 ocurrences of BOB

Column B will count that ocurrences as 1, 2, 3, 4...

Column C will concatenate A with B (BOB1, BOB2, BOB3, BOB4)
This are unique values that VLOOKUP will understand.

In cell A8 write BOB1, BOB2, BOB3 or BOB4 whichever you are looking for.

VLOOKUP sees A8, Search in A1:E4, When it finds A8 looks for column 5 in this case column E, A comma after number 5 will calculate EXACT match of A8 but if A8 doesn't exist in A1:E4 will return #N/A. Try it and tell me.

_________
From PUERTO RICO with pride
 
Upvote 0
jakemelon said:
What if creating unique data is not an option? All the lookup data will be imported and cannot be changed.

Creating an additional column (using a concatenation formula) does not change the imported data at all. You can even create the additional column in a different worksheet and use it for multi-key/conditional retrieval. I might as well add that this method (see the maxflia post) is in performance terms the cheapest.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top