+ Reply to Thread
Results 1 to 10 of 10

Excel concatenate and carriage return by columns

Hybrid View

highwayman Excel concatenate and... 01-25-2013, 08:35 AM
mike7952 Re: Excel concatenate and... 01-25-2013, 08:42 AM
OllieB Re: Excel concatenate and... 01-25-2013, 08:45 AM
OllieB Re: Excel concatenate and... 01-25-2013, 08:56 AM
highwayman Re: Excel concatenate and... 01-25-2013, 09:41 AM
mike7952 Re: Excel concatenate and... 01-25-2013, 10:17 AM
highwayman Re: Excel concatenate and... 01-25-2013, 12:27 PM
highwayman Re: Excel concatenate and... 01-28-2013, 11:37 AM
mike7952 Re: Excel concatenate and... 01-28-2013, 02:09 PM
highwayman Re: Excel concatenate and... 01-28-2013, 05:26 PM
  1. #1
    Registered User
    Join Date
    01-25-2013
    Location
    Connecticut, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Excel concatenate and carriage return by columns

    I would like to combine records into a single cell by 'Order' and 'Row' as shown in my example. There are thousands of records in the spreadsheet. I can do this manually, but it is extremely tedious. The number of rows for any given 'Order' vary. I can't find a way to do this with 'standard' Excel, but I think there must be a way with VBA. If someone could help me by providing the code (the more detailed and specific, the better), I would really appreciate it.

    Thank you!

    excel row combine.jpg

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel concatenate and carriage return by columns

    Can you upload an example workbook. With before and after results wanted?
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Excel concatenate and carriage return by columns

    misread the question - post removed.
    If you like my contribution click the star icon!

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Excel concatenate and carriage return by columns

    Should do the trick

         Public Sub CombineEntries()
         '# declare
              Dim lngRowNumber As Long
              Dim lngInnerLoop As Long
              Dim strKeyValue As String
              Dim lngKeyStart As Long
              Dim strCombineValue
         '# initialise
              lngKeyStart = 0
         '# loop for all rows
              With ActiveSheet
                   For lngRowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
                        If .Cells(lngRowNumber, "A").Value <> strKeyValue Then
                             If lngKeyStart > 0 Then
                                  For lngInnerLoop = lngKeyStart To lngRowNumber - 1
                                       .Cells(lngInnerLoop, "D").Value = strCombineValue
                                  Next lngInnerLoop
                             End If
                             lngKeyStart = lngRowNumber
                             strKeyValue = .Cells(lngRowNumber, "A").Value
                             strCombineValue = .Cells(lngRowNumber, "C").Value
                        Else
                             strCombineValue = strCombineValue & vbLf & .Cells(lngRowNumber, "C").Value
                        End If
                   Next lngRowNumber
                   If lngKeyStart > 0 Then
                        For lngInnerLoop = lngKeyStart To lngRowNumber - 1
                             .Cells(lngInnerLoop, "D").Value = strCombineValue
                        Next lngInnerLoop
                   End If
              End With
         End Sub

  5. #5
    Registered User
    Join Date
    01-25-2013
    Location
    Connecticut, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Excel concatenate and carriage return by columns

    Hi Mike -

    Columns A, B, and C are my starting data. Column D, 'Combined Notes', is my desired output. Attached is an example workbook. As you can see, I can do this manually using CONCATENATE, but with 10,000+ rows in my actual data set, this would take forever based on the variability of the data. Thanks for taking a look at this!!
    Attached Files Attached Files

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel concatenate and carriage return by columns

    This should do it

    Option Explicit
    Sub abc()
     Dim a, i As Long
     
     a = Range("a2", Cells(Rows.Count, "a").End(xlUp).Offset(, 2)).Value
    
     Application.ScreenUpdating = False
     With CreateObject("scripting.dictionary")
        .comparemode = 1
        For i = 1 To UBound(a)
            If Not .exists(a(i, 1)) Then
                .Item(a(i, 1)) = a(i, 3)
            Else
                .Item(a(i, 1)) = .Item(a(i, 1)) & vbCrLf & a(i, 3)
            End If
        Next
        For i = 1 To UBound(a)
            Cells(i, "d").Offset(1) = .Item(a(i, 1))
        Next
     End With
     Application.ScreenUpdating = True
    End Sub

  7. #7
    Registered User
    Join Date
    01-25-2013
    Location
    Connecticut, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Excel concatenate and carriage return by columns

    Mike - this worked perfectly!! Thanks a ton for your help, you're a life saver!!!

  8. #8
    Registered User
    Join Date
    01-25-2013
    Location
    Connecticut, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Excel concatenate and carriage return by columns

    Hi Mike ... was hoping you might be able to help me again. I have a slightly more complicated scenario in the attached 'example2' than what was in the original. I'm not sure how to modify the macro to add this extra 'layer'. If you'd give me a hand I'd sure appreciate it!!
    Attached Files Attached Files

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel concatenate and carriage return by columns

    This should do it

    Option Explicit
    
    Sub abc()
     Dim a, i As Long
     
     a = Range("a2", Cells(Rows.Count, "a").End(xlUp).Offset(, 3)).Value
    
     Application.ScreenUpdating = False
     With CreateObject("scripting.dictionary")
        .comparemode = 1
        For i = 1 To UBound(a)
            If Not .exists(a(i, 1) & a(i, 2)) Then
                .Item(a(i, 1) & a(i, 2)) = a(i, 4)
            Else
                .Item(a(i, 1) & a(i, 2)) = .Item(a(i, 1) & a(i, 2)) & vbCrLf & a(i, 4)
            End If
        Next
        For i = 1 To UBound(a)
            Cells(i, "e").Offset(1) = .Item(a(i, 1) & a(i, 2))
        Next
     End With
     Application.ScreenUpdating = True
    End Sub

  10. #10
    Registered User
    Join Date
    01-25-2013
    Location
    Connecticut, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Excel concatenate and carriage return by columns

    Thanks Mike! This was spot on.

+ 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