Vlookup needed to return multiple unique values into one cell

chiguy28

New Member
Joined
Jun 10, 2015
Messages
2
Hi everyone, I'm hoping someone can help me out. I am looking for a macro that will return vlookup multiple values in a single cell in excel. I have used the below one from Lewiy that was listed in another thread, but it is returning the value every time it is found in the source spreadsheet. Is there anyway to modify the below formula to add a comma between values, and to only return unique values once?

Function MYVLOOKUP(lookupval, lookuprange As Range, indexcol As Long)
Dim r As Range
Dim result As String
result = ""
For Each r In lookuprange
If r = lookupval Then
result = result & " " & r.Offset(0, indexcol - 1)
End If
Next r
MYVLOOKUP = result
End Function
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

You mean like this:
Code:
Function MYVLOOKUP(lookupval, lookuprange As Range, indexcol As Long)
    
    'Requires: Tools-->References--> Microsoft Scripting Runtime
    Dim dic As Dictionary
    Dim r As Range
    Set dic = New Dictionary

    For Each r In lookuprange
        If r = lookupval Then dic(r.Offset(0, indexcol - 1).Value) = vbNullString
    Next r
    
    MYVLOOKUP = Join(dic.Keys, ",")
    
End Function

Note: The way the macro is written it needs you to specify a reference to the Microsoft Scripting Runtime library. To do that, when in the VB Editor, select Tools-->References-->Microsoft Scripting Runtime.

It uses a Dictionary object. Dictionaries will ensure that only unique keys are added to it. SO every located value is added to the dictionary but the duplicate values are discarded.

dic.Keys works like a array so all the elements can be joined together with the Join command.
 
Upvote 0

Forum statistics

Threads
1,214,518
Messages
6,119,996
Members
448,935
Latest member
ijat

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