Solved

Custom Excel lookup function

Posted on 2014-01-15
6
332 Views
Last Modified: 2014-01-15
hi there, it's been a while since I used VBA and could do with help here. I'd like to create a function to lookup all instances of a particular value and return results from a few columns, each result on a new line but within a single string, using char(10) to separate results.

e.g.

function LookupExpense(LookupValue)

' get all the row numbers by looping through this until Row(n:n) returns no more values:
' RowNum =SMALL(IF('expenses'!$H:$H=LookupValue,ROW('expenses'!$H:$H)),ROW(1:1))

' Result =INDEX('expenses'!$A$1:$N$9999,RowNum, 5)

' for each rownum
LookupExpense = LookupExpense  & char(10) & Result


end function

Open in new window


Any help much appreciated thanks! :-)
0
Comment
Question by:xenium
  • 4
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
ID: 39783452
Will the attached do it:

Function lookupjoin(SearchValue As Variant, SearchRange As Range, JoinRange As Range)

Dim x As Long
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")

For x = 1 To SearchRange.Count
    If SearchRange(x, 1) = SearchValue Then
       If Not d.exists(JoinRange(x, 1)) Then
            d.Add JoinRange(x, 1), ""
       End If
    End If
Next x

lookupjoin = Join(d.keys, " | ")

End Function

Open in new window


It really is important to pass the Lookup Range and the Join Range in the function or it will not calculate on sheet changes. And so could be unstable.
I have used " | " to join the values. You could pass the join character into the function variables if you like, up to you.
Example.xlsm
0
 

Author Comment

by:xenium
ID: 39783667
hi Steve, that's excellent thankyou! 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
 
LVL 24

Expert Comment

by:Steve
ID: 39783942
It would not be too hard to allow for a width of cells to join...
how would you look to separate them?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:xenium
ID: 39784131
A space would be fine, or supplied as an input..
0
 

Author Comment

by:xenium
ID: 39784143
Posted as follow-up since the original question has now been answered

See follow up here: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28339835.html

Thanks Steve for you quick and expert response
0
 

Author Closing Comment

by:xenium
ID: 39784146
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 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

19 Experts available now in Live!

Get 1:1 Help Now