+ Reply to Thread
Results 1 to 9 of 9

Alter Solution of a thread to " Concatenate Data" solved by MickG

Hybrid View

capson Alter Solution of a thread to... 02-06-2015, 06:21 PM
AB33 Re: Alter Solution of a... 02-06-2015, 06:30 PM
jindon Re: Alter Solution of a... 02-06-2015, 06:46 PM
capson Re: Alter Solution of a... 02-06-2015, 09:46 PM
jindon Re: Alter Solution of a... 02-06-2015, 09:57 PM
capson Re: Alter Solution of a... 02-06-2015, 10:14 PM
jindon Re: Alter Solution of a... 02-06-2015, 10:27 PM
capson Re: Alter Solution of a... 02-06-2015, 10:38 PM
jindon Re: Alter Solution of a... 02-06-2015, 10:40 PM
  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Alter Solution of a thread to " Concatenate Data" solved by MickG

    I need to Concatenate mulipe discontinuous columns, these columns are constantly changing, so in one project it might be
    Columns a, e, g, k, l

    And in another
    Columns g,k,l,o,aa

    There might be a lot of data 100,00 rows so a fast solution is desirable, I found this Here: http://www.excelforum.com/excel-prog...te-tables.html

    And a solution to the thread by @MickG and altered it to Concatenate the first two rows but how to generalize it to Concatenate mulipe discontinuous columns?

    Thanks

    Option Explicit
    
    Private Sub CommandButton21_Click()
    Dim Rng As Range, Dn As Range, n As Long, Q As Variant
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
    For Each Dn In Rng
        If Not IsEmpty(Dn.Value) Then
            If Not .Exists(Dn.Value) Then
                .Add Dn.Value, Array(Dn.Value, Dn.Offset(, 1) & "|" & Dn.Offset(, 2), "")
            Else
                Q = .Item(Dn.Value)
                    Q(2) = Dn.Offset(, 2).Value
                .Item(Dn.Value) = Q
            End If
        End If
    Next
    Range("I2").Resize(.Count, 3) = Application.Index(.items, 0, 0)
    End With
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    .Add Dn.Value, Array(Dn.Value, Dn.Offset(, 1) & "|" & Dn.Offset(, 2), "")
    Concatenates columns 2 and 3, using column A as a key.
    If you have a single key, you can expand the columns by changing the offset columns.

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

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    I see no reason to use Dictionary....
    Select Column(s) that you want to concatenate.
    It will only concatenate when selected column has value.
    Private Sub CommandButton21_Click()
        Dim rng As Range, r As Range, i As Long
        On Error Resume Next
        Set rng = Application.InputBox("Select column(s)", Type:=8)
        On Error GoTo 0
        If rng Is Nothing Then Exit Sub
        With Cells(1).CurrentRegion
            ReDim a(1 To .Rows.Count, 1 To 2)
            a(1, 1) = .Cells(1, 1).Value
            a(1, 2) = "Concat"
            For i = 2 To .Rows.Count
                a(i, 1) = .Cells(i, 1).Value
                For Each r In rng
                    If .Cells(i, r.Column) <> "" Then
                        a(i, 2) = a(i, 2) & IIf(a(i, 2) = "", "", "|") & _
                        .Cells(i, r.Column).Value
                    End If
                Next
            Next
            With .Offset(, .Columns.Count + 1).Resize(, 2)
                .CurrentRegion.ClearContents
                .Value = a
            End With
        End With
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    jindon thank you for the responce,

    I tried your code on 4 columns of 9000 rows and it exacuted but the code never completed its action even after 30 minutes

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

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    You must be selecting whole column....

    Try select one cell in a column

    like
    A1:D1
    A1,C1,F1:G1.... Holding down Ctrl key

  6. #6
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    I uploaded a file with just a few rows to show how the concatenating is working.

    The range was [B2:C12]

    ColA   ColB
    abc    xxx
    .      .
    .      .
    .      .
    .      .
    I get for the first cell of the concatenated Colunm
    abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx|abc|xxx
    Attached Files Attached Files

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

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    See my previous post.

    Select only one cell a column...

  8. #8
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    jindon pardon me I did not understand, I got it now, thank you

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

    Re: Alter Solution of a thread to " Concatenate Data" solved by MickG

    No problem and thanks for the rep.

+ 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] Mark a thread "Solved"
    By FORTRANguru in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2014, 07:41 PM
  2. Solution to populate cell with "X" by selecting "R"
    By nirola in forum Excel General
    Replies: 4
    Last Post: 11-17-2012, 10:15 AM
  3. I don't see a button/Switch to select "Thread Solved"
    By NewToExcel-2007 in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 09-05-2010, 10:46 AM
  4. Replies: 0
    Last Post: 08-07-2006, 01:35 AM
  5. Replies: 0
    Last Post: 07-17-2006, 09:45 AM

Tags for this Thread

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