Some Sort Of VLookup/Match Formula

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have sheet 1 as below. What I need is similar to a VLookup where on sheet 2 it will give me whats in column B on sheet 1 when the corresponding numbers in column A are found. Problem is VLookup only returns one result for each number.

Sheet 1

NumberNumber1
M14307100294003M279
M14307100294003M280
M14307100294003M281
M14307100294003M282
M14307100294003M283

Sheet 2 Using VLookup

NumberNumber1
M14307100294003M279
M14307100294003M279
M14307100294003M279
M14307100294003M279
M14307100294003M279

Obviously on sheet 2 I would like the same result that is on sheet 1. There will be 10s of 1000s of rows and any different amount of matching numbers in column A.
Thanks.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Here is a UDF developed by Krishnakumar. The syntax for this is MLOOKUP(TableArray,LookupValue,LookupRange,[NthMatch])

VBA 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
 
Upvote 0
Here is a UDF developed by Krishnakumar. The syntax for this is MLOOKUP(TableArray,LookupValue,LookupRange,[NthMatch])

VBA 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
I just get a result of VALUE, how do I use it, where to I put the formula?
 
Upvote 0
Put the formula in the cell you wish to get the results in.

MLOOKUP.xlsm
ABCDEFG
14/1/2011SN181
24/2/2011DC825
34/3/2011QO512QO512,348,777,599
44/4/2011JK354DC825,394,518,509
54/4/2011DC394
64/4/2011OF921
74/4/2011DC518
84/4/2011QO348
94/9/2011DC509
104/10/2011MI193
114/11/2011BA329
124/12/2011AI930
134/13/2011QO777
144/14/2011OH962
154/15/2011FS221
164/16/2011RU419
174/17/2011KU115
184/18/2011QO599
194/19/2011WJ657
204/20/2011AM105
214/21/2011WJ786
224/22/2011RN457
234/23/2011DF343
244/24/2011SN848
254/25/2011WP956
Sheet1
Cell Formulas
RangeFormula
G3:G4G3=MLOOKUP($C$1:$C$25,E3,$B$1:$B$25)
 
Upvote 0
Put the formula in the cell you wish to get the results in.

MLOOKUP.xlsm
ABCDEFG
14/1/2011SN181
24/2/2011DC825
34/3/2011QO512QO512,348,777,599
44/4/2011JK354DC825,394,518,509
54/4/2011DC394
64/4/2011OF921
74/4/2011DC518
84/4/2011QO348
94/9/2011DC509
104/10/2011MI193
114/11/2011BA329
124/12/2011AI930
134/13/2011QO777
144/14/2011OH962
154/15/2011FS221
164/16/2011RU419
174/17/2011KU115
184/18/2011QO599
194/19/2011WJ657
204/20/2011AM105
214/21/2011WJ786
224/22/2011RN457
234/23/2011DF343
244/24/2011SN848
254/25/2011WP956
Sheet1
Cell Formulas
RangeFormula
G3:G4G3=MLOOKUP($C$1:$C$25,E3,$B$1:$B$25)
But the results will need to be on sheet 2 and taking the info from sheet 1? This is appearing to have the info and result on same sheet which is whats confusing me.
 
Upvote 0
Also the results are in the same cell, I need them all downwards like in my example.
 
Upvote 0
Any other ideas on this please anyone?
 
Upvote 0
To get the data to display in a column tweak the formula as follows: =@MLOOKUP($C$1:$C$2200,$E$2,$B$1:$B$2200,ROWS($F$2:F12)).

change the last optional item to the section showing Rows and the range of rows.
 
Upvote 0
But is the result going to be on sheet 2 getting the data from sheet 1?
 
Upvote 0
You need to reference the sheet(s) where the data is located for this to happen. Look at how it happens in regular Vlookup and adapt.

 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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