Link to home
Start Free TrialLog in
Avatar of xenium
xenium

asked on

Custom Excel lookup function (part 2)

follow-up to https://www.experts-exchange.com/questions/28339566/Custom-Excel-lookup-function.html?anchorAnswerId=39784131#a39784131

If i wanted to return more than one column (the join range), eg to concatenate a few columns, would this be possible with a simple variation?
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

OK, the attached file has the following formula:

Function lookupjoin(SearchValue As Variant, SearchRange As Range, JoinRange As Range, wordSeperator As String, Optional LineSeperator As String)

Dim x As Long, y As Long, z As Long
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
Dim colArr
Dim JoinString As String

z = JoinRange.Columns.Count
ReDim colArr(1 To z)

For x = 1 To SearchRange.Count
    If SearchRange(x, 1) = SearchValue Then
       For y = 1 To z
            colArr(y) = JoinRange(x, y)
       Next y
       JoinString = Join(colArr, wordSeperator)
       If Not d.exists(JoinString) Then
            d.Add JoinString, ""
       End If
    
    End If
Next x

If LineSeperator = Empty Then LineSeperator = vbCrLf
lookupjoin = Join(d.keys, LineSeperator)

End Function

Open in new window


This will allow for a Range to be concatenated.
It does have the optional second line joiner (set to default to crlf).
It should be quite apparent how it works from the examples in the workbook.
Any questions let me know.
Example.xlsm
Avatar of xenium
xenium

ASKER

Works great thanks! Simple question (i hope) how can I enter a discontiguous JoinRange, eg if i want to return columns B and D for example.

Cheers
That is not as simple as you would hope, you could add a variable for columns to include/exclude.
But passing a group of ranges is not so easy as they get separated by commas which ruins the formula.
How many columns need to be joined? Do they change?
Avatar of xenium

ASKER

Maybe 3 or 4 columns. They probably won't change. Solution needs to be fairly user-friendly as I won't be doing the updates to rows etc, and user will just copy-paste formulae down etc.
ASKER CERTIFIED SOLUTION
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xenium

ASKER

Works great thanks again!