Solved

Custom Excel lookup function (part 2)

Posted on 2014-01-15
6
244 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now