Results 1 to 10 of 49

Thread: LookUp Value and Concatenate All Found Results

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    The idea for this UDF (user defined function) came from this article which I saw in another forum...

    Excel udf: Lookup and return multiple values concatenated into one cell | Get Digital Help - Microsoft Excel resource

    This is actually an old article that someone just commented on which, in turn, brought it to the top of that forum's "Recent Comments" list and, hence, to my attention. When I looked at the article, I decided the UDF presented by that programmer could be expanded to make it more flexible, hence this current article by me (you should follow the above link in order to see the type of data being processed and the expected resulting output from it). Here is the code I came up with...

    Code:
    Function LookUpConcat(ByVal SearchString As String, SearchRange As Range, ReturnRange As Range, _
                          Optional Delimiter As String = " ", Optional MatchWhole As Boolean = True, _
                          Optional UniqueOnly As Boolean = False, Optional MatchCase As Boolean = False)
                       
      Dim X As Long, CellVal As String, ReturnVal As String, Result As String
      
      If (SearchRange.Rows.Count > 1 And SearchRange.Columns.Count > 1) Or _
         (ReturnRange.Rows.Count > 1 And ReturnRange.Columns.Count > 1) Then
        LookUpConcat = CVErr(xlErrRef)
      Else
        If Not MatchCase Then SearchString = UCase(SearchString)
        For X = 1 To SearchRange.Count
          If MatchCase Then
            CellVal = SearchRange(X).Value
          Else
            CellVal = UCase(SearchRange(X).Value)
          End If
          ReturnVal = ReturnRange(X).Value
          If MatchWhole And CellVal = SearchString Then
            If UniqueOnly And InStr(Result & Delimiter, Delimiter & ReturnVal & Delimiter) > 0 Then GoTo Continue
            Result = Result & Delimiter & ReturnVal
          ElseIf Not MatchWhole And CellVal Like "*" & SearchString & "*" Then
            If UniqueOnly And InStr(Result & Delimiter, Delimiter & ReturnVal & Delimiter) > 0 Then GoTo Continue
            Result = Result & Delimiter & ReturnVal
          End If
    Continue:
        Next
        
        LookUpConcat = Mid(Result, Len(Delimiter) + 1)
      End If
      
    End Function
    hello sir please is there anyway so we can put a comma in between the concatenated results
    Thank you

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by Alaeddine View Post
    hello sir please is there anyway so we can put a comma in between the concatenated results
    There is an optional Delimiter argument (it is the fourth argument) that allows you do specify whatever character or characters you want to be the delimiter.

Similar Threads

  1. Replies: 5
    Last Post: 06-04-2013, 01:04 PM
  2. Replies: 6
    Last Post: 12-12-2012, 08:03 PM
  3. Lookup lookup lookup just can't make it work
    By work2live in forum Excel Help
    Replies: 1
    Last Post: 12-08-2012, 11:48 PM
  4. Excel found unreadable content
    By zyousafi in forum Excel Help
    Replies: 2
    Last Post: 08-08-2012, 10:41 AM
  5. Concatenate Multiple Lookup Values in Single Cell
    By Admin in forum Download Center
    Replies: 4
    Last Post: 04-06-2012, 09:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •