vlookup with two or more matches

Trevor Osborne

New Member
Joined
Sep 12, 2016
Messages
29
=IFERROR(IFERROR( VLOOKUP(A:A,Sheet1!A:C,3,FALSE),VLOOKUP(A:A,Sheet1!B:C,2,FALSE)),"") this the vlookup that I am using.
How can I have this show if there is more then one match on sheet1?


<colgroup><col width="746" style="width: 560pt; mso-width-source: userset; mso-width-alt: 26042;"> <tbody> </tbody>
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Here is some code to create a UDF.

Code:
Function MLOOKUP(ByRef TableArray As Range, ByVal LookupVal, ByRef LookupRange As Range, _
                                                        Optional ByVal NthMatch As Long)


' Author        : Krishnakumar @ ExcelFox.com




If Not TypeOf TableArray Is Range Then
    MLOOKUP = CVErr(2042)
    Exit Function
End If
If Not TypeOf LookupRange Is Range Then
    MLOOKUP = CVErr(2042)
    Exit Function
End If
If TableArray.Rows.Count <> LookupRange.Rows.Count Then
    MLOOKUP = CVErr(2042)
    Exit Function
End If
If TableArray.Columns.Count <> LookupRange.Columns.Count Then
    MLOOKUP = CVErr(2042)
    Exit Function
End If


Dim LV_Cnt      As Long 'Count Loookup Value
Dim KA1, KA2
Dim r As Long, c As Long
Dim fFoundNo    As Long
Dim n           As Long
Dim strLval     As String


If IsNumeric(LookupVal) Then
    LV_Cnt = Evaluate("countif(" & LookupRange.Address & "," & LookupVal & ")")
    fFoundNo = Evaluate("match(" & CLng(LookupVal) & "," & LookupRange.Address & ",0)")
ElseIf IsDate(LookupVal) Then
    LV_Cnt = Evaluate("countif(" & LookupRange.Address & "," & CLng(LookupVal) & ")")
    fFoundNo = Evaluate("match(" & CLng(LookupVal) & "," & LookupRange.Address & ",0)")
Else
    strLval = """" & LookupVal & """"
    LV_Cnt = Evaluate("countif(" & LookupRange.Address & "," & strLval & ")")
    fFoundNo = Evaluate("match(" & strLval & "," & LookupRange.Address & ",0)")
End If


If NthMatch > 0 Then
    If LV_Cnt = 0 Or NthMatch > LV_Cnt Then
        MLOOKUP = CVErr(2042)
        Exit Function
    End If
End If




KA1 = TableArray: KA2 = LookupRange


For r = fFoundNo To UBound(KA1, 1)
    For c = 1 To UBound(KA1, 2)
        If LCase$(KA2(r, c)) = LCase$(LookupVal) Then
            If NthMatch Then
                n = n + 1
                If n = NthMatch Then
                    MLOOKUP = KA1(r, c)
                    Exit Function
                End If
            Else
                MLOOKUP = MLOOKUP & "," & KA1(r, c)
            End If
        End If
    Next
Next
MLOOKUP = Mid$(MLOOKUP, 2)
End Function

Here is an explanation of how it works.

Excel Workbook
ABCDEFGHIJKLMNO
14/1/2011SN181************
24/2/2011WP825*vm509509,592,717,359,172,287,556,211********
34/3/2011QO512*QO592512,471,906,981********
44/4/2011JK354*DC717518,259,826,820********
54/4/2011SV394**359*********
64/4/2011OF921**172*Syntax*******
74/4/2011DC518**287*MLOOKUP(TableArray,LookupValue,LookupRange,[NthMatch])*******
84/4/2011GD348**556*********
94/9/2011VM509**211*If the optional NthMatch is missing, All the values will be concatenated in a single cell*******
104/10/2011MI193**#N/A*********
114/11/2011BA329**#N/A*********
124/12/2011AI930**#N/A*Click on Alt +F11 for the VBA code to make this UDF. *Copy it and paste it*******
134/13/2011XJ777****into your worksheet*******
144/14/2011OH962************
154/15/2011FS221************
164/16/2011RU419*4/4/2011394*********
174/17/2011KU115************
184/18/2011LG599************
194/19/2011WJ657************
204/20/2011AM105************
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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