Results 1 to 10 of 10

Thread: Vlookup to Return Multiple Values

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Here is a latest version. Code amended to ignore blanks in the result.

    Code:
    Public Function MLOOKUP(TableArray As Range, ByVal LookupVal, LookupRange As Range, _
                                        Optional ByVal NumAsText As Boolean = False, _
                                        Optional ByVal NthMatch As Long, _
                                        Optional IgnoreBlanks As Boolean = True)
    '---------------------------------------------------------------------------------------
    ' Procedure : MLOOKUP
    ' Author    : Krishnakumar @ ExcelFox.com
    ' Date      : 12/5/2012
    ' Purpose   : Returns multiple values
    ' Amended   : Include option to ignore blanks - 07/22/13
    '---------------------------------------------------------------------------------------
    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(, , , 1) & "," & LookupVal & ")")
        If NumAsText Then GoTo 1
        fFoundNo = Evaluate("match(" & CLng(LookupVal) & "," & LookupRange.Address(, , , 1) & ",0)")
    ElseIf IsDate(LookupVal) Then
        LV_Cnt = Evaluate("countif(" & LookupRange.Address(, , , 1) & "," & CLng(LookupVal) & ")")
        fFoundNo = Evaluate("match(" & CLng(LookupVal) & "," & LookupRange.Address(, , , 1) & ",0)")
    Else
    1:
        strLval = """" & LookupVal & """"
        LV_Cnt = Evaluate("countif(" & LookupRange.Address(, , , 1) & "," & strLval & ")")
        fFoundNo = Evaluate("match(" & strLval & "," & LookupRange.Address(, , , 1) & ",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
                    If Not IgnoreBlanks Then
                        MLOOKUP = MLOOKUP & "," & KA1(r, c)
                    ElseIf Len(KA1(r, c)) Then
                        MLOOKUP = MLOOKUP & "," & KA1(r, c)
                    End If
                End If
            End If
        Next
    Next
    MLOOKUP = Mid$(MLOOKUP, 2)
    End Function
    This handles numbers which are not true number ,but text. Just pass the 3rd parameter as TRUE. By default it is FALSE.

    http://www.excelfox.com/forum/f2/mlo...-710/#post3172
    Last edited by Admin; 07-22-2013 at 09:16 AM. Reason: code amended
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. How do I get Vlookup to return multiple matches?
    By HANOOF in forum Excel Help
    Replies: 1
    Last Post: 06-04-2013, 10:06 PM
  2. Replies: 4
    Last Post: 04-24-2013, 10:04 AM
  3. Vlookup Multiple Values By Adding Formula With Loop In VBA
    By Safal Shrestha in forum Excel Help
    Replies: 15
    Last Post: 04-22-2013, 04:49 PM
  4. Vlookup - Multiple Sheets
    By Suhail in forum Excel Help
    Replies: 3
    Last Post: 01-30-2013, 06:47 PM
  5. VLOOKUP with Multiple Results
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 2
    Last Post: 05-26-2011, 10:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •