Solved

Custom Excel lookup function

Posted on 2014-01-15
6
341 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

825 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