Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Custom Excel lookup function

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
xenium
Asked:
xenium
  • 4
  • 2
1 Solution
 
SteveCommented:
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
 
xeniumAuthor Commented:
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
 
SteveCommented:
It would not be too hard to allow for a width of cells to join...
how would you look to separate them?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
xeniumAuthor Commented:
A space would be fine, or supplied as an input..
0
 
xeniumAuthor Commented:
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
 
xeniumAuthor Commented:
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now