Return multiple results with WorksheetFunction.VLookup

Exceladd1ct

Board Regular
Joined
Feb 10, 2019
Messages
64
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: 8

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
12,471
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,638
Messages
5,626,038
Members
416,155
Latest member
bokboxes

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
Top