Solved

Custom Excel lookup function (part 2)

Posted on 2014-01-15
6
283 Views
Last Modified: 2014-01-16
follow-up to http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28339566.html#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?
0
Comment
Question by:xenium
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 24

Expert Comment

by:Steve
ID: 39784628
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
0
 

Author Comment

by:xenium
ID: 39785654
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
0
 
LVL 24

Expert Comment

by:Steve
ID: 39785808
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:xenium
ID: 39785834
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.
0
 
LVL 24

Accepted Solution

by:
Steve earned 280 total points
ID: 39786043
OK the attached has a change to allow for selecting the columns as a set of numbers...

so for columns 1 and 3 you would enter 13.
for columns 1 and 2 and 3 use 123.

Have a look and see if it is better.

Have hard coded the spaces and line feeds as this may be better in the code.

ATB
Steve.
Example.xlsm
0
 

Author Closing Comment

by:xenium
ID: 39787024
Works great thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question