+ Reply to Thread
Results 1 to 12 of 12

Concatenate data from cells by values from two columns

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Concatenate data from cells by values from two columns

    Hello everyone
    It seems that the title of the thread is somewhat strange ..
    I have sheets("Data") .. in Column B there are codes and for each code there are sub codes (numbers) and in column D there are strings that I need to concatenate ..
    In Columns F:G there are other related data to the codes as each code has a name ..
    This was the structure of Data sheet
    *********
    In Result sheet I would input Code in column F and the sub code in Column G (but the sub code could be sometimes concatenated like that 4|7 ..which mean 4 ,5, 6 and 7)
    In column H I need the results as explained in the attachment ..

    For example: in row 2 >> Code is 1 and sub code is 2|4 so I need to have 2 / 3 / 4 sub codes from Data sheet ...and put these between brackets ( )
    and after 5 spaces put the name of the code and sub code between brackets < >
    So the result would be like that

    ( Text1 (2) Text1 (3) Text1 (4) )     < NameA 2|4 >
    I need the solution to use arrays as original data is over 60,000 rows
    Thanks advanced for help
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Contributor
    Join Date
    06-02-2015
    Location
    delhi
    MS-Off Ver
    2010
    Posts
    104

    Re: Concatenate data from cells by values from two columns

    Check this
    Sub INTERST()
    
        Dim D As Object
        Dim i As Long
                
                Set D = CreateObject("scripting.dictionary")
    
    'Filling Dictionary object with unique values and their Data
    With Sheets("Data")
        For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            If Not D.exists(CStr(Cells(i, 2).Value)) Then
                D.Add CStr(Cells(i, 2).Value), CStr(Cells(i, 3).Value) & "-" & Cells(i, 4).Value
        Else
            D.Item(CStr(Cells(i, 2).Value)) = D.Item(CStr(Cells(i, 2).Value)) & "," & Cells(i, 3).Value & "-" & Cells(i, 4).Value
           End If
        Next
    End With
        
    'Now Retrieval
    With Sheets("Result")
            For j = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            
                    Tmp = Split(D.Item(CStr(.Cells(j, 1).Value)), ",")  'Get Each dictionary value Data and split it with ","
                        If InStr(.Cells(j, 2), "|") > 0 Then
                            Tmp2 = Split(.Cells(j, 2), "|")                        'Split second column Text for looping array Ex:2|4
                                LT = Tmp2(0)                                        'Lower bound of array
                                    Ht = Tmp2(1)                                    'Higer bound of array
                                        For k = LT - 1 To Ht - 1
                                                Txt = Txt & Split(Tmp(k), "-")(1)
                                        Next
                                                  .Cells(j, 4) = "(" & Txt & ") (" & Application.WorksheetFunction.VLookup(.Cells(j, 1).Value, Sheets("data").Range("F:G"), 2, 0) & " " & .Cells(j, 2).Value & ")"
                                            Else
                                                    .Cells(j, 4) = "(" & Split(Tmp(0), "-")(1) & ") (" & Application.WorksheetFunction.VLookup(.Cells(j, 1).Value, Sheets("data").Range("F:G"), 2, 0) & " " & .Cells(j, 2).Value & ")"
                                        
                    End If
                    Txt = ""
            Next
        
        End With
    
    MsgBox "Hey I have done my work"
    
    End Sub
    'Made by :- Mandeep baluja
    'https://www.facebook.com/groups/825221420889809/
    'https://www.linkedin.com/in/mandeep-baluja-b777bb88
    Add Reputation if it helped
    Attached Files Attached Files

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Concatenate data from cells by values from two columns

    Maybe :

    Sub Test()
      Dim coll As New Collection, arr, i As Long, j As Long, k1 As Long, k2 As Long, v
    
      arr = Sheets("Data").Range("A1").CurrentRegion.Value
      For i = 2 To UBound(arr, 1)
          coll.Add key:=arr(i, 2) & Chr$(2) & arr(i, 3), Item:=arr(i, 4)
      Next i
    
      arr = Sheets("Data").Range("F1").CurrentRegion.Value
      For i = 2 To UBound(arr, 1)
          coll.Add key:=CStr(arr(i, 1)), Item:=arr(i, 2)
      Next i
    
      With Sheets("Result").Range("F1").CurrentRegion
        arr = .Value
        For i = 2 To UBound(arr, 1)
            arr(i, 3) = ""
            v = Split(arr(i, 2), "|"): k1 = CLng(v(0)): If UBound(v) = 0 Then k2 = k1 Else k2 = v(1)
            On Error Resume Next
               For j = k1 To k2
                   arr(i, 3) = arr(i, 3) & Space$(1) & coll(arr(i, 1) & Chr$(2) & j)
               Next j
               arr(i, 3) = "(" & arr(i, 3) & " )     < " & coll(CStr(arr(i, 1))) & Space$(1) & arr(i, 2) & " >"
            On Error GoTo 0
        Next i
        .Value = arr
      End With
    End Sub

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Concatenate data from cells by values from two columns

    Thanks a lot for these great and wonderful solutions
    Really great and awesome
    Thank you very much for helping me all the time

    I will apply it to the original file and if there are any notes I would tell you soon
    Best Regards

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Concatenate data from cells by values from two columns

    You are welcome, it's my pleasure.


    Regards

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Concatenate data from cells by values from two columns

    @Mr. Karedog
    I'm very sorry to disturb you again and again ..
    As for this issue It is perfect but I need another addition ..
    As for the results .. the numbers between < and > needed to be Arial font regardless the original font of the other characters
    In fact the cell font is not Arial but I just need the parts between < > to be of Arial font ..
    Thanks a lot for advanced help

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Concatenate data from cells by values from two columns

    Can it be done to have the results in the same cell with two different font type ???
    I have posted the request on this link too
    http://chandoo.org/forum/threads/for...nt-type.28885/
    Last edited by YasserKhalil; 04-14-2016 at 07:12 AM.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,532

    Re: Concatenate data from cells by values from two columns

    Don't have time for more but to give you an idea how it works.
    On sheet Result select f.i. range H2 and run below code.
    Sub tst()
        f = InStr(1, ActiveCell, "<", vbTextCompare)
        ff = InStrRev(ActiveCell, ">", , vbTextCompare)
        fff = (ff - 1) - (f + 1)
        ActiveCell.Characters(f, fff).Font.Name = "Arial"
    End Sub
    Select other cell and run code again.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Concatenate data from cells by values from two columns

    Thanks a lot for this working method ..it is great
    As for the large data .. will I deal with the 10,000 rows using loops ..? I think it will be disaster ..
    Could font type be stored in an array or this is just a dream of mine?

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,532

    Re: Concatenate data from cells by values from two columns

    Tried it with over 10,000 rows and it took about 1 sec. so I don't think that 's such a big disaster no ?
    Sub tst2()
        t = Timer
        Application.ScreenUpdating = False
        For i = 2 To Range("H" & Rows.Count).End(xlUp).Row
            f = InStr(1, Cells(i, 8), "<", vbTextCompare)
            Cells(i, 8).Characters(f).Font.Name = "Arial"
        Next
        Application.ScreenUpdating = True
        MsgBox Timer - t
    End Sub

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Concatenate data from cells by values from two columns

    Thank you so much for this great help
    Now it is solved
    Best Regards

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,532

    Re: Concatenate data from cells by values from two columns

    You're welcome 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. How to concatenate adjacent cells based on values present in previous columns?
    By Palraj in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-30-2014, 01:01 AM
  2. Copy Values from two columns and Paste them in one (Concatenate).
    By pipoliveira in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-08-2014, 11:12 AM
  3. Concatenate 6 columns but exclude blank cells
    By shaz0503 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-05-2014, 11:28 PM
  4. [SOLVED] Excel function or script for concatenate all values from B:B for same columns in A:A
    By Odeen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-27-2012, 10:29 AM
  5. Concatenate values from columns and deleting duplicates
    By RandomP in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-15-2011, 06:20 PM
  6. Concatenate cells with delimiter, new row for values after blank cells
    By testerss in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-28-2011, 04:26 PM
  7. concatenate values two columns
    By matrixknow in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2007, 08:52 AM

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