Solved

Custom Excel lookup function

Posted on 2014-01-15
6
359 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
[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
  • 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
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: 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

752 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