How to install your new code
- Copy the Excel VBA code
- Select the workbook in which you want to store the Excel VBA code
- Press Alt+F11 to open the Visual Basic Editor
- Choose Insert > Module
- Edit > Paste the macro into the module that appeared
- Close the VBEditor
- Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)
Function Clookup(LValue As Range, LRng As Range, _
ResRng As Range, Optional sopSep As String = ", ") As String
Dim i As Long, sTemp As String
If WorksheetFunction.CountIf(LRng, LValue.Value) Then
For i = 1 To LRng.Cells.Count
If LRng.Cells(i).Value = LValue.Value Then
sTemp = sTemp & ResRng.Cells(i).Value & sopSep
End If
Next i
End If
If Len(sTemp) Then Clookup = Left(sTemp, Len(sTemp) - Len(sopSep))
End Function
|
A |
B |
1 |
# |
Name |
2 |
1 |
D1 |
3 |
2 |
D2 |
4 |
3 |
D1 |
5 |
4 |
D5 |
6 |
5 |
D7 |
7 |
6 |
D3 |
8 |
7 |
D8 |
9 |
8 |
D3 |
10 |
9 |
D4 |
11 |
10 |
D1 |
|
C |
D |
1 |
Search Value |
Formula Result |
2 |
D1 |
1, 3, 10 |
|
D |
2 |
=Clookup(C2,B2:B12,A2:A12,", ") |
Bookmarks