+ Reply to Thread
Results 1 to 5 of 5

Convert Comma Separated Values to matching Comma Separated Strings

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2012
    Location
    Modesto, CA
    MS-Off Ver
    Excel 2007
    Posts
    45

    Convert Comma Separated Values to matching Comma Separated Strings

    Thanks in advance for your review of this query!

    Looking to convert a group of values in a single cell that are separated by commas into a matching list of strings separated by commas.

    For instance, I'm using the following formula when only two values have been entered :

    =TRIM(CONCATENATE(INDEX(M:M,MATCH(VALUE(LEFT(C4,FIND(",",C4)-1)),L:L,0))," ",INDEX(N:N,MATCH(VALUE(LEFT(C4,FIND(",",C4)-1)),L:L,0))," & ",INDEX(N:N,MATCH(VALUE(RIGHT(C4,LEN(C4)-FIND(",",C4)-1)),L:L,0))))

    33205, 90149 > flavors : banana & licorice

    Is there a "kinder" solution for this? What if more than two values are entered?

    33205, 78596, 90149 > flavors : banana, pumpkin, licorice

    See attached table for values and strings.

    artikyulashun
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Convert Comma Separated Values to matching Comma Separated Strings

    Would M:M ever contain anything other than flavors : ? Also, if there are only 2 codes in col C, it appears you want the 2 corresponding values from N:N separated by &, but it there are 3 or more you want a comma-separated list.

    This is an example of something Excel does very poorly, expecially in a version as old as Excel 2007. From my perspective, you'd be better off using VBA.

  3. #3
    Registered User
    Join Date
    08-21-2012
    Location
    Modesto, CA
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Convert Comma Separated Values to matching Comma Separated Strings

    Fair observation on the "&". I think a comma for each flavor would suffice.

    No. M:M would contain other naming conventions.

    Honestly, I'm not versed in VBA. Do you have something in mind?

  4. #4
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Convert Comma Separated Values to matching Comma Separated Strings

    The VBA looks like this.

    Sorry, correction below.

    '# Arguments:
    '# vs = value sought, a delimited list of codes
    '# ra = lookup table, matching codes in vs against 1st column of ra, categories in 2nd column, values in last column
    '# id = 'input' delimiter for codes in vs
    '# od = 'output' delimited for values in last column of ra
    '#
    '# Propagates error values in any of the arguments. Returns Empty if no items in vs match 1st column of ra.
    Function multilookup( _
     vs As Variant, _
     ra As Variant, _
     Optional id As String = ", ", _
     Optional od As String = ", " _
    ) As Variant
    '-------------------------------
      Const DD As String = " ; "
      Dim i As Long, j As Long, k As Long
      Dim rc As Variant, rv As Variant
    
      '# propagate errors
      If IsError(id) Then
        multilookup = id
        Exit Function
      End If
    
      If IsError(od) Then
        multilookup = od
        Exit Function
      End If
    
      If IsError(vs) Then
        multilookup = vs
        Exit Function
      Else
        '# use only 1st item if vs is range or array
        If IsArray(vs) Then
          For Each rv In vs
            vs = rv
            Exit For
          Next rv
        End If
    
        vs = Split(vs, id)
      End If
    
      '# propagate errors
      If IsError(ra) Then
        multilookup = ra
        Exit Function
      Else
        '# note: assumed to be n-row by 2- or 3-column
        If TypeOf ra Is Range Then ra = ra.Areas(1).Value
      End If
    
      ReDim rc(1 To 2, 1 To 4)  '# initial guess
      k = 0  '# for 2nd dim of rc
    
      For i = LBound(vs) To UBound(vs)
        With Application
          rv = Empty
    
          '# match items in vs against 1st column of ra
          '--------------------------------------------
          '# Split() above only produces arrays of strings.
          '# If the fields are numeric, try numeric matching first.
          If IsNumeric(vs(i)) Then
            rv = .Match(CDbl(vs(i)), .WorksheetFunction.Index(ra, 0, 1), 0)
          End If
    
          '# If no match yet, try text matching.
          If IsError(rv) Or IsEmpty(rv) Then
            rv = .Match(vs(i), .WorksheetFunction.Index(ra, 0, 1), 0)
          End If
    
          '# If still no match, skip.
          If IsError(rv) Then GoTo Continue
    
          j = CLng(rv)
    
          '# match 2nd column item in ra against 1st row of rc
          '---------------------------------------------------
          rv = .Match(ra(j, 2), .WorksheetFunction.Index(rc, 1, 0), 0)
    
          '# If no match, then new category to enter into rc.
          If IsError(rv) Then
            k = k + 1
            If k > UBound(rc, 2) Then
              ReDim Preserve rc(1 To 2, 1 To 2 * UBound(rc, 2))
            End If
            rc(1, k) = ra(j, 2)
            rc(2, k) = ra(j, UBound(ra, 2))
    
          '# Otherwise, existing category, supplement 2nd row of rc.
          Else
            rc(2, rv) = rc(2, rv) & od & ra(j, UBound(ra, 2))
    
          End If
        End With
    
    Continue:
      Next i
    
      Erase vs
    
      '# If no matches at all, return Empty.
      If k = 0 Then Exit Function
    
      rv = ""
      For i = 1 To k
        rv = rv & DD & RTrim$(rc(1, i)) & " " & rc(2, i)
      Next i
    
      multilookup = Mid$(rv, Len(DD) + 1)
    
      Erase rc
    
    End Function
    I'm also attaching a workbook to show how it works.
    Attached Files Attached Files
    Last edited by hrlngrv; 12-01-2020 at 02:51 PM. Reason: correction

  5. #5
    Registered User
    Join Date
    08-21-2012
    Location
    Modesto, CA
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Convert Comma Separated Values to matching Comma Separated Strings

    Appreciate the coding that went into this hrlngrv. I'll roll up my sleeves and see if I can follow along ...

+ 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. Replies: 3
    Last Post: 12-26-2019, 09:59 AM
  2. Converting Comma and Dash-Separated Number Strings to Binary
    By ExcelWashington in forum Excel General
    Replies: 6
    Last Post: 03-01-2019, 07:40 AM
  3. [SOLVED] Combine comma separated strings
    By JimmyA in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-09-2016, 01:21 PM
  4. Replies: 3
    Last Post: 09-25-2014, 02:14 PM
  5. Programatically Convert comma separated 'Text to Column'
    By super_duper_guy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-01-2012, 08:04 PM
  6. Convert range of cells to comma separated list
    By maw230 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-23-2012, 06:03 PM
  7. Return Y/N after matching from multiple lookup values separated by comma
    By indoglans in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2011, 03:13 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