+ Reply to Thread
Results 1 to 10 of 10

Help Concatinating cells from a column based on duplicate rows

Hybrid View

  1. #1
    Registered User
    Join Date
    10-27-2011
    Location
    Alameda, CA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Help Concatinating cells from a column based on duplicate rows

    I have a spreadsheet with multiple columns. The first column has many duplicates. For each group of the duplicate values in first column, I want to concatenate unique values in other columns. I started on the attached file by identifying the first of each unique cells. I need help on how to fill in the concatenated values.

    Blue colums are the source data, Green columns are the start of the desired output and Yellow is where I need help. I manually typed the desired output in red text for the first row as an example.

    Thanks
    Attached Files Attached Files
    Last edited by Rabid Squirrel; 11-02-2011 at 04:42 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help Concatinating cells from a column based on duplicate rows

    Hi Rabid Squirrel
    Welcome to the forum

    Can you modify the macro?
    Option Explicit
    Sub test19()
        Dim a, b(), i As Long, ii As Long, n As Long
        With Worksheets("Sheet1").Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 5)
            a = .Value
        End With
        ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For i = 1 To UBound(a, 1)
                If Not IsEmpty(Trim(a(i, 1))) Then
                    If Not .exists(Trim(a(i, 1))) Then
                        n = n + 1
                        For ii = 1 To 5
                            b(n, ii) = Trim(a(i, ii))
                        Next
                        .Add a(i, 1), n
                    Else
                        For ii = 2 To 5
                            b(.Item(Trim(a(i, 1))), ii) = b(.Item(Trim(a(i, 1))), ii) & IIf(b(.Item(Trim(a(i, 1))), ii) <> "", ", ", "") & " " & a(i, ii)
                        Next
                    End If
                End If
            Next
        End With
        With Worksheets("Sheet2")
            .Range("a1").Resize(n, 5).Value = b
        End With
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    10-27-2011
    Location
    Alameda, CA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Help Concatinating cells from a column based on duplicate rows

    Thank you. I copied into my spreadsheet and it worked almost perfectly creating a new worksheet. Can you help with one more part of the problem? I need to have only unique cells from Columns B-E copied over. So instead of P,P,P, D it would read P, D.

    It would be fine to do this in two steps if that makes it easier.

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help Concatinating cells from a column based on duplicate rows

    Hi Rabid Squirrel
    No problem, almost perfectly is not close enough
    Option Explicit
    Sub test19()
        Dim a, b(), i As Long, ii As Long, n As Long, temp As String, e
        With Worksheets("Sheet1").Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 5)
            a = .Value
        End With
        ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For i = 1 To UBound(a, 1)
                If Not IsEmpty(Trim(a(i, 1))) Then
                    If Not .exists(Trim(a(i, 1))) Then
                        n = n + 1
                        For ii = 1 To 5
                            b(n, ii) = Trim(a(i, ii))
                        Next
                        .Add Trim(a(i, 1)), n
                    Else
                        For ii = 2 To 5
                            b(.Item(Trim(a(i, 1))), ii) = b(.Item(Trim(a(i, 1))), ii) & IIf(b(.Item(Trim(a(i, 1))), ii) <> "", ",", "") & " " & a(i, ii)
                        Next
                    End If
                End If
            Next
            .RemoveAll
            For i = 2 To n
                For ii = 2 To 5
                    For Each e In Split(b(i, ii), ",")
                        If Not .exists(Trim(e)) Then
                            temp = temp & "," & e
                            .Add Trim(e), Nothing
                        End If
                    Next
                    b(i, ii) = Mid$(temp, 2)
                    temp = ""
                    .RemoveAll
                Next
            Next
        End With
        With Worksheets("Sheet3")
            .Range("a1").Resize(n, 5).Value = b
        End With

  5. #5
    Registered User
    Join Date
    10-27-2011
    Location
    Alameda, CA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Help Concatinating cells from a column based on duplicate rows

    It works perfectly when I paste the code into the Challenge worksheet but when I paste into my "real" spreadsheet, I get a "Run Time Error 1004" with the following row of code highlighted in the debugger:

    With Worksheets("Step 3 Paste from 2").Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 5)

    All I changed was the worksheet name which I did with your first solution and had no problems.

  6. #6
    Registered User
    Join Date
    10-27-2011
    Location
    Alameda, CA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Help Concatinating cells from a column based on duplicate rows

    Pike,

    I don't know why I had that error but I rebuilt the workbook from scratch and now don't have any errors.

    THANK YOU VERY MUCH!!!!!

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help Concatinating cells from a column based on duplicate rows

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  8. #8
    Registered User
    Join Date
    10-27-2011
    Location
    Alameda, CA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Help Concatinating cells from a column based on duplicate rows

    The error above is back and I can't get rid of it.

    I've attached the whole workbook and I would really appreciate it if you could take a look. I'm trying to run your code to transform the data from "Step 3" into "Step 4".

    I renamed the macro to BuildRCS and I don't know why it worked one time but now it gets the error and I can't figure out why.

    Thank you for your help again.
    Last edited by Rabid Squirrel; 11-02-2011 at 04:43 PM.

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help Concatinating cells from a column based on duplicate rows

    Rabid Squirrel,
    Possibly you will need to have the sheet with the data visible

  10. #10
    Registered User
    Join Date
    10-27-2011
    Location
    Alameda, CA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Help Concatinating cells from a column based on duplicate rows

    That was it!

    I just moved the Macro button to the source data and it worked!

    You Rock!

+ 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