+ Reply to Thread
Results 1 to 4 of 4

Lookup multiple values concatenated into one cell without duplicates

Hybrid View

  1. #1
    Registered User
    Join Date
    07-18-2013
    Location
    Kolkata
    MS-Off Ver
    Excel 2010
    Posts
    21

    Lookup multiple values concatenated into one cell without duplicates

    Hi,

    I have a excel dump of employees with more than half a million rows. I need to lookup this data and return multiple values concatenated into one cell without duplicates. I found out an udf which is working well, but the problem is with such a huge data to lookup and dragging this formula to around thousand rows makes the file very heavy and the file stops responding at times for as long as four hours, coz it takes humongous time to execute the code. The udf that I used was:

    Function MLOOKUP(lVal, Rng As Range, lVal_Col_Index As Long, Rslt_Col_Index As Long, Optional tFlag As Boolean) As String
    Dim a, x, s, i As Long
    a = Rng
    With CreateObject("scripting.dictionary")
    .comparemode = vbTextCompare
    For i = 1 To UBound(a, 1)
    If UCase(a(i, lVal_Col_Index)) = UCase(lVal) Then
    Select Case tFlag
    Case True
    s = Format(a(i, Rslt_Col_Index), "hh:mm")
    If Not .exists(s) Then .Add s, Nothing
    Case Else
    s = a(i, Rslt_Col_Index)
    If Not .exists(s) Then .Add s, Nothing
    End Select
    End If
    Next
    If .Count > 0 Then
    MLOOKUP = Join(.Keys, ";")
    End If
    End With
    End Function

    Please help with something that works fast.

  2. #2
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: Lookup multiple values concatenated into one cell without duplicates

    Try this function:
    Function CoupleIf(ByRef Value_Range As Range, ByVal Criteria As String, ByRef Couple_Range As Range, Optional Delim As String = " ", Optional NonDuplicate As Boolean = False) As String
        Dim li As Long, sStr As String, avItem, avDateArr(), avRezArr(), lUBnd As Long
        If Value_Range.Count > 1 Then
            avDateArr = Intersect(Value_Range, Value_Range.Parent.UsedRange).Value
            avRezArr = Intersect(Couple_Range, Couple_Range.Parent.UsedRange).Value
            If Value_Range.Rows.Count = 1 Then
                avDateArr = Application.Transpose(avDateArr)
                avRezArr = Application.Transpose(avRezArr)
            End If
        Else
            ReDim avDateArr(1, 1): ReDim avRezArr(1, 1)
            avDateArr(1, 1) = Value_Range.Value
            avRezArr(1, 1) = Couple_Range.Value
        End If
        lUBnd = UBound(avDateArr, 1)
    
        Dim objRegExp As Object, objMatches As Object
        Set objRegExp = CreateObject("VBScript.RegExp")
        objRegExp.Global = False: objRegExp.Pattern = "=|<>|=>|>=|<=|=<|>|<"
        Set objMatches = objRegExp.Execute(Criteria) 
        If objMatches.Count > 0 Then
            Dim sStrMatch As String
            sStrMatch = objMatches.Item(0)
            Criteria = Replace(Replace(Criteria, sStrMatch, "", 1, 1), Chr(34), "", 1, 2)
            Select Case sStrMatch
            Case "="
                For li = 1 To lUBnd
                    If avDateArr(li, 1) = Criteria Then
                        If Trim(avRezArr(li, 1)) <> "" Then _
                           sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                    End If
                Next li
            Case "<>"
                For li = 1 To lUBnd
                    If avDateArr(li, 1) <> Criteria Then
                        If Trim(avRezArr(li, 1)) <> "" Then _
                           sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                    End If
                Next li
            Case ">=", "=>"
                For li = 1 To lUBnd
                    If avDateArr(li, 1) >= Criteria Then
                        If Trim(avRezArr(li, 1)) <> "" Then _
                           sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                    End If
                Next li
            Case "<=", "=<"
                For li = 1 To lUBnd
                    If avDateArr(li, 1) <= Criteria Then
                        If Trim(avRezArr(li, 1)) <> "" Then _
                           sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                    End If
                Next li
            Case ">"
                For li = 1 To lUBnd
                    If avDateArr(li, 1) > Criteria Then
                        If Trim(avRezArr(li, 1)) <> "" Then _
                           sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                    End If
                Next li
            Case "<"
                For li = 1 To lUBnd
                    If avDateArr(li, 1) < Criteria Then
                        If Trim(avRezArr(li, 1)) <> "" Then _
                           sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                    End If
                Next li
            End Select
        Else    'Если нет вхождения
            For li = 1 To lUBnd
                If avDateArr(li, 1) Like Criteria Then
                    If Trim(avRezArr(li, 1)) <> "" Then _
                       sStr = sStr & IIf(sStr <> "", Delim, "") & avRezArr(li, 1)
                End If
            Next li
        End If
        If NonDuplicate Then
            Dim oDict As Object, sTmpStr
            Set oDict = CreateObject("Scripting.Dictionary")
            sTmpStr = Split(sStr, Delim)
            On Error Resume Next
            For li = LBound(sTmpStr) To UBound(sTmpStr)
                oDict.Add sTmpStr(li), sTmpStr(li)
            Next li
            sStr = ""
            sTmpStr = oDict.keys
            For li = LBound(sTmpStr) To UBound(sTmpStr)
                sStr = sStr & IIf(sStr <> "", Delim, "") & sTmpStr(li)
            Next li
        End If
        CoupleIf = sStr
    End Function
    Analog standart function Countif, but working with text.
    Source
    I'm sorry my english...

  3. #3
    Registered User
    Join Date
    07-18-2013
    Location
    Kolkata
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Lookup multiple values concatenated into one cell without duplicates

    Hi.. Thank you for your quick response. I tried the code but could not follow it properly. It would be great if you can attach a sample file. I have also attached a data file which will better explain what I require.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: Lookup multiple values concatenated into one cell without duplicates

    In attached file value "US-0000146" is not exists in first sheet. And other values also.
    My function must be writen:
    Formula: copy to clipboard
    =CoupleIf(Data!$B$2:$B$19999;Result!A2;Data!$A$2:$A$19999;";";1)


    Sample_CoupleIf.xlsm

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Match multiple substrings in a cell against a lookup table and return concatenated values
    By stevewc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2013, 10:50 AM
  2. Replies: 10
    Last Post: 12-19-2012, 03:07 PM
  3. Lookup and then return multiple values concatenated
    By cjcass in forum Excel General
    Replies: 1
    Last Post: 11-01-2012, 03:19 AM
  4. Sum values from multiple cells - multiple lookup values in single cell
    By taxdept in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-21-2012, 04:12 PM
  5. vba to Match concatenated values in concatenated columns
    By bjurick in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2012, 03:45 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1