Solved

Custom Excel lookup function

Posted on 2014-01-15
6
322 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

760 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

18 Experts available now in Live!

Get 1:1 Help Now