+ Reply to Thread
Results 1 to 11 of 11

Concatenate based on other cell data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-08-2012
    Location
    BD
    MS-Off Ver
    Microsoft Office 2016
    Posts
    633

    Concatenate based on other cell data

    Dear all,
    I am having some data in the CELL range A&B which I use as input data and after processing by macro I want to get the result as output in the range D&E.

    CELL A  CELL B
    ID	      Value
    Apple	1K
    Apple	2K
    Apple	3K
    Mango	8K
    Mango	5K
    Banana	1k
    Lemon	3K
    Lemon	4K
    Cherry	10k
    Cherry	11k
    Cherry	12k
    Cherry	13k
    Here CELL A & CELL B is my input data from this I want to get the results (output) as CELL D and CELL E
    Here CELL E will be concatenate value of CELL B

    CELL D    CELL E
    ID	        Value
    Apple	1k,2k,3K
    Mango	8K,5K
    Banana	1k
    Lemon	3k,4k
    Cherry	10k,11k,12k,13k
    I would use an UDF to perform my activity but it makes my excel halt , I need help to solve my issue by macro or excel formula except UDF. I have attached my excel sheet for better understanding.
    Attached Files Attached Files

  2. #2
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Concatenate based on other cell data

    You can use a UDF Concat Function and Unique items formula:

    Function Concat_Unique(Lookup_Value As String, Lookup_Column As Range, Concat_column As Range)
     
    Dim i As Single
    Dim Unique As New Collection
    Dim Value As Variant
    Dim result As String
     
    For i = 1 To Lookup_Column.Cells.Rows.Count
        If Lookup_Value = Lookup_Column.Cells(i).Value Then
            If Len(Concat_column.Cells(i)) > 0 Then
                On Error Resume Next
                Unique.Add Concat_column.Cells(i), CStr(Concat_column.Cells(i))
                On Error GoTo 0
            End If
        End If
    Next i
     
    For Each Value In Unique
        result = result & Value & ", "
    Next Value
     
    If Len(result) = 0 Then
        Concat_Unique = ""
    Else
        Concat_Unique = Left(result, Len(result) - 2)
    End If
     
    End Function
    =IFERROR(INDEX($A$2:$A$13,MATCH(0,INDEX(COUNTIF($D$1:D1,$A$2:$A$13),0,0),0)),"")
    UDF Concat funtion form:

    =Concat_Unique(D2,$A$2:$A$13,$B$2:$B$13)
    EDIT: Forgot to read everything, The UDF part
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,065

    Re: Concatenate based on other cell data

    I think there's a function that will do this with the MOREFUNC plugin or there's TEXTJOIN if you have Office 365 otherwise it's got to be a macro/UDF.
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  4. #4
    Forum Contributor
    Join Date
    06-08-2012
    Location
    BD
    MS-Off Ver
    Microsoft Office 2016
    Posts
    633

    Re: Concatenate based on other cell data

    Hi Special-K , thanks for your nice idea.

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Concatenate based on other cell data

    Try :-
    Sub MG03Apr03
    Dim Rng As Range, Dn As Range, Temp As Range, c As Long, nStr As String
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    For Each Dn In Rng
        If Not Temp Is Nothing Then
            If Not Dn = Temp Then
                c = c + 1
                Cells(c, "D") = Temp.Value: Cells(c, "E") = nStr
                nStr = ""
            End If
        End If
            nStr = nStr & IIf(nStr = "", Dn.Offset(, 1).Value, ", " & Dn.Offset(, 1).Value)
            Set Temp = Dn
    Next Dn
    c = c + 1
    Cells(c, "D") = Temp.Value: Cells(c, "E") = nStr
    End Sub
    Regards Mick

  6. #6
    Forum Contributor
    Join Date
    06-08-2012
    Location
    BD
    MS-Off Ver
    Microsoft Office 2016
    Posts
    633

    Re: Concatenate based on other cell data

    Hi PaulM100, your code is working great! It filled my requirements, I appreciated it, it will save my time , thanks for helping me.
    Last edited by nur2544; 04-03-2018 at 11:56 AM.

  7. #7
    Forum Contributor
    Join Date
    06-08-2012
    Location
    BD
    MS-Off Ver
    Microsoft Office 2016
    Posts
    633

    Re: Concatenate based on other cell data

    Hi Mick , your code is working great! It filled my requirements, I appreciated it, it will save my time , thanks for helping me.

  8. #8
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Concatenate based on other cell data

    Is not my thread, but Mick's code does not work entirely. If you add more data, even if is the same ID, it will create a new line instead of concatenating the values.

    Capture.JPG

  9. #9
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Concatenate based on other cell data

    nur2544:- You're welcome

    PaulM100:- The code below will work on your modified data format:-
    Sub MG03Apr09
    Dim Rng As Range, Dn As Range, n As Long
    Set Rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
    For Each Dn In Rng
        If Not .Exists(Dn.Value) Then
            .Add Dn.Value, Dn.Offset(, 1).Value
        Else
            .Item(Dn.Value) = .Item(Dn.Value) & ", " & Dn.Offset(, 1).Value
        End If
    Next
    Range("D1").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
    End With
    End Sub
    Regards Mick

  10. #10
    Forum Contributor
    Join Date
    06-08-2012
    Location
    BD
    MS-Off Ver
    Microsoft Office 2016
    Posts
    633

    Re: Concatenate based on other cell data

    Hi PaulM100 , you have written code for helping me , I really appreciated your nice job But earlier I used UDF (I already mention that in my post above) that makes my excel slowness, even some time my whole excel program would gone to halted. What is why I was not interested in UDF. I was disappointed to see your UDF once again although it was working nice. But I was given reputation both of you at the same time.
    However, I agree that it was your thread but it was my mistake that not to replay you first, I am sorry for that. I am also agree with you that if I add same id then Mick’s code giving additional id which suppose not to come . this mismatch I didn’t notice that time. Thanks for understanding me.
    @ PaulM100 & Mick , I need both of your help to solve my issue.

  11. #11
    Forum Contributor
    Join Date
    06-08-2012
    Location
    BD
    MS-Off Ver
    Microsoft Office 2016
    Posts
    633

    Re: Concatenate based on other cell data

    Hi MickG , Now it works perfectly , thanks once again.

+ 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. VBA Automatically concatenate cell data based on data, and prefix with specific word.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-30-2017, 02:16 PM
  2. Replies: 11
    Last Post: 07-28-2015, 05:08 PM
  3. [SOLVED] Identify values in a cell based from another sheet and concatenate the results in one cell
    By danallamas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-15-2015, 12:00 AM
  4. [SOLVED] Formula concatenate data in multiple rows based on match data in a different row.
    By kidkool in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-17-2014, 11:01 AM
  5. Replies: 6
    Last Post: 08-01-2013, 08:09 PM
  6. [SOLVED] Concatenate based on cell data
    By Healthport - Jamie in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-13-2012, 03:04 PM
  7. Concatenate based on cell value
    By Russell719 in forum Excel General
    Replies: 5
    Last Post: 04-29-2009, 01:35 PM

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