Return multiple results with WorksheetFunction.VLookup

Exceladd1ct

Board Regular
Joined
Feb 10, 2019
Messages
76
Hi, i am trying to get multiple results with WorksheetFunction.VLookup. The way i see it, i could use a function as bellow, but i got stuck getting the address of the value found by vLookUp Function.
I know it is easier to use Range.Find Method but it has a small delay on my big data set while WorksheetFunction.VLookup gets the results instantly.

VBA Code:
Function lookup(tofind, firstrow)

    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim ws As Worksheet
    Set ws = wb.Sheets("Data")
    
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim myColumnIndex as Long
    Dim myLastRow As Long
    Dim myVLookupResult
    Dim allstr As String
    
    Dim myTableArray As Range
    
    myFirstColumn = 1
    myLastColumn = 3
    myColumnIndex = 3
    myLastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

    With ws
        Set myTableArray = .Range(.Cells(firstrow, myFirstColumn), .Cells(myLastRow, myLastColumn))
    End With
    
    On Error Resume Next
        myVLookupResult = Application.WorksheetFunction.VLookup(tofind, myTableArray, myColumnIndex, False)
    On Error GoTo 0
    
    If myVLookupResult <> "" Then

        'This is where it gets difficult, this is more like pseudocode

        'Save the result in a delimited string to work with later
        allstr = allstr & "|" & myVLookupResult

        'Define the starting point for the next lookup'
        firstrow = myVLookupResult.Address

        'Recall function
        
        Call lookup(tofind, firstrow + 1)
    End If

End Function
 

Attachments

  • 1609449762404.png
    1609449762404.png
    7.6 KB · Views: 14

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Save the result in a delimited string to work with later
Noting the above, what do you intend to do with the string later?

If you just want a delimited string then I would look at evaluating a formula like
Excel Formula:
=TEXTJOIN("|",1,FILTER(ResultColumn,CriteriaColumn=tofind,"")
but with a large data set there are probably a number of better methods that you could use.
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,215
Members
448,874
Latest member
b1step2far

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