+ Reply to Thread
Results 1 to 10 of 10

Creating a comma separated list for unique entires

Hybrid View

  1. #1
    Registered User
    Join Date
    07-19-2012
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Creating a comma separated list for unique entires

    Please let me know how I can achieve the following:

    I have this:

    NAME STATE
    BOB PA
    BOB NJ
    BOB DE
    TOM PA
    TOM DE

    I need to have this:

    NAME STATE
    BOB PA, NJ, DE
    TOM PA, DE

    I have to do this on a very large scale and doing it manually is just not an option... any ideas would be greatly appreciated.

    Thanks!
    Last edited by JBeaucaire; 07-22-2012 at 09:35 PM. Reason: Corrected thread title to topic only, as per forum rules

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Please help! Trouble creating a comma separated list for unique entires

    Hi,

    Assuming the original table is in A1:Bnn, and is sorted on the name then use a couple of helper columns.

    C2:
    =IF(A2=A1,C1&","&B2,B2)

    D2:
    =COUNTIF($A2:$A$6,A2)
    change the row 6 to suit

    Copy C2:D2 down, filter the list on column D for the value 1. Then copy the filtered rows and copy them wherever.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru
    Join Date
    05-24-2011
    Location
    India
    MS-Off Ver
    365
    Posts
    2,243

    Re: Please help! Trouble creating a comma separated list for unique entires

    Time for VBA

    Insert this VBA oode,

    Function CONCATIF(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
        ' code base by Mike Rickson, MrExcel MVP
        ' used as exactly like SUMIF() with two additional parameters
        ' of delimiter and "no duplicates" as TRUE/FALSE if concatenated values
        ' might include duplicates  ex. =ConcatIf($A$1:$A$10,C1,$B$1:$B$10,", ",True)
    
    Dim i As Long, j As Long
    
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                        stringsRange.Column - compareRange.Column)
        
        For i = 1 To compareRange.Rows.Count
            For j = 1 To compareRange.Columns.Count
                If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                    If InStr(CONCATIF, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                        CONCATIF = CONCATIF & Delimiter & CStr(stringsRange.Cells(i, j))
                    End If
                End If
            Next j
        Next i
        CONCATIF = Mid(CONCATIF, Len(Delimiter) + 1)
    End Function
    Then extract unique Name by Advanced Filter & enter them D2 to down. In E2,

    =CONCATIF(A:A,D2,B:B,", ",1)

    A:A = Name, B:B = State, 1 for exclude duplicate
    Regards,
    Haseeb Avarakkan

    __________________________________
    "Feedback is the breakfast of champions"

  4. #4
    Registered User
    Join Date
    07-19-2012
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Please help! Trouble creating a comma separated list for unique entires

    This is so helpful! Thank you.

    Another minor issue.. in some cases, all 50 states will be listed..I dont want to have to spread the cell out to view everything.. this will need to be printed.. is there a way to set it up so that I can put in a hard return after every third value?

    So instead of

    PA, NJ, DE, IL, NY, AZ

    I could get

    PA, NJ, DE, (Alt+Enter)
    IL, NY, AZ

  5. #5
    Forum Guru
    Join Date
    05-24-2011
    Location
    India
    MS-Off Ver
    365
    Posts
    2,243

    Re: Please help! Trouble creating a comma separated list for unique entires

    Adjust the column width & use 'Wrap Text'

  6. #6
    Registered User
    Join Date
    07-19-2012
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Please help! Trouble creating a comma separated list for unique entires

    Using wrap text causes some of the state names to get split up like this:

    PA,NJ,D
    E,IL,NY
    ,AZ

  7. #7
    Registered User
    Join Date
    07-19-2012
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Please help! Trouble creating a comma separated list for unique entires

    can Alt-Enter after every third value be automated?

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Please help! Trouble creating a comma separated list for unique entires

    Here you go:
    Option Explicit
    
    Function CONCATIF(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
        ' code base by Mike Rickson, MrExcel MVP
        ' used as exactly like SUMIF() with two additional parameters
        ' of delimiter and "no duplicates" as TRUE/FALSE if concatenated values
        ' might include duplicates  ex. =ConcatIf($A$1:$A$10,C1,$B$1:$B$10,", ",True)
    
    Dim i As Long, j As Long, Cnt As Long
    
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                        stringsRange.Column - compareRange.Column)
        
        For i = 1 To compareRange.Rows.Count
            For j = 1 To compareRange.Columns.Count
                If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                    If InStr(CONCATIF, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                        If Cnt = 3 Then
                            Cnt = 1
                            CONCATIF = CONCATIF & Delimiter & Chr(10) & CStr(stringsRange.Cells(i, j))
                        Else
                            Cnt = Cnt + 1
                            CONCATIF = CONCATIF & Delimiter & CStr(stringsRange.Cells(i, j))
                        End If
                    End If
                End If
            Next j
        Next i
        CONCATIF = Mid(CONCATIF, Len(Delimiter) + 1)
    End Function

    You will need to still turn on Wrap Text in those cells.
    Last edited by JBeaucaire; 07-23-2012 at 01:57 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Creating a comma separated list for unique entires

    Maybe try this sub:

    Option Explicit
    
    Sub MergeList()
    
    Dim Dic As Object, arrOld, arrNew, r&, x&, LR&
    
    Set Dic = CreateObject("Scripting.Dictionary")
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
      ' Sheet to array
        arrOld = Range("A2:B" & LR)
    
      ' Scripting Dictionary to create Unique list
        For r = 1 To LR - 1
            Dic.Item(arrOld(r, 1)) = Dic.Item(arrOld(r, 1)) & ", " & arrOld(r, 2)
        Next
        
      ' Merge Keys and Items to form 2D array
        arrNew = Application.Transpose(Array(Dic.Keys, Dic.Items))
    
      ' Remove leading commas
        For x = 1 To UBound(arrNew)
            arrNew(x, 2) = Mid(arrNew(x, 2), 3)
        Next
        
      ' Array to Sheet
        Range("D2:E" & Dic.Count + 1) = arrNew
      
      ' Header names
        Range("D1:E1") = [{"NAME","STATE"}]
        
      ' Wrap text, set column width and autofit row height
        With Column("E:E")
            .WrapText = True
            .ColumnWidth = 10
        End With
        Rows("2:" & Range("E" & Rows.Count).End(xlUp).Row).EntireRow.AutoFit
    End Sub
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Creating a comma separated list for unique entires

    try
    Sub test()
        Dim a, i As Long, x, n As Long
        With Range("a1").CurrentRegion
            a = .Value
            With CreateObject("Scripting.Dictionary")
                .CompareMode = 1
                For i = 1 To UBound(a, 1)
                    If Not .exists(a(i, 1)) Then
                        .Item(a(i, 1)) = a(i, 2)
                    Else
                        .Item(a(i, 1)) = _
                        Join$(Array(.Item(a(i, 1)), a(i, 2)), ", ")
                    End If
                Next
                x = Application.Transpose(Array(.keys, .items))
                n = .Count
            End With
            With .Offset(, .Columns.Count + 1).Resize(n, 2)
                .CurrentRegion.Clear
                .Value = x
                .Columns.AutoFit
                .CurrentRegion.Borders.Weight = 2
            End With
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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