CONCATENATE Cell values from multiple row into single cell based on Unique Value

earthworm

Well-known Member
Joined
May 19, 2009
Messages
773
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All,

Below is the data set . I need vba and formula approach that can create unique value from Column A and then merge all the values that are distributed into multiple rows into single cell

Example I have shown the required result in Column E and F . Please assist . I am using office 2016

Below formula works well in excel 2021 . However I need formula and vba for older version of excel

=TEXTJOIN(",",TRUE,IF($A$3:$A$21=E3,$C$3:$C$21,""))

Book1
ABCDEFG
2ABCResult
31Test 1AB11AB1,AB2,AB3,AB4AB1,AB2,AB3,AB4
41Test 2AB22AB5AB5
51Test 3AB33AB6AB6
61Test 4AB44AB7,AB8,AB9AB7,AB8,AB9
72Test 5AB55AB10AB10
83Test 6AB66AB11AB11
94Test 7AB77AB12,AB13,AB14,AB16,AB17AB12,AB13,AB14,AB15,AB16,AB17
104Test 8AB88AB18AB18
114Test 9AB99AB19AB19
125Test 10AB10
136Test 11AB11
147Test 12AB12
157Test 13AB13
167Test 14AB14
177Test 15AB15
187Test 16AB16
197Test 17AB17
208Test 18AB18
219Test 19AB19
Sheet1
Cell Formulas
RangeFormula
G3:G11G3=TEXTJOIN(",",TRUE,IF($A$3:$A$21=E3,$C$3:$C$21,""))
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
With vba, try this:
VBA Code:
Sub earthworm1()

Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, tx As String

va = Range("A1", Cells(Rows.Count, "C").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 2)

For i = 2 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    
    i = i - 1
    tx = ""
        For n = j To i
            tx = tx & "," & va(n, 3)
        Next
    k = k + 1
    vb(k, 1) = va(i, 1)
    If Len(tx) > 1 Then vb(k, 2) = Mid(tx, 2, 100000)
Next

'put the result
Range("E2").Resize(k, 2) = vb
End Sub

Result:
Book1
ABCDEF
1ABCResult
21Test 1AB11AB1,AB2,AB3,AB4
31Test 2AB22AB5
41Test 3AB33AB6
51Test 4AB44AB7,AB8,AB9
62Test 5AB55AB10
73Test 6AB66AB11
84Test 7AB77AB12,AB13,AB14,AB15,AB16,AB17
94Test 8AB88AB18
104Test 9AB99AB19
115Test 10AB10
126Test 11AB11
137Test 12AB12
147Test 13AB13
157Test 14AB14
167Test 15AB15
177Test 16AB16
187Test 17AB17
198Test 18AB18
209Test 19AB19
Sheet2
 
Upvote 0
With vba, try this:
VBA Code:
Sub earthworm1()

Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, tx As String

va = Range("A1", Cells(Rows.Count, "C").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 2)

For i = 2 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
   
    i = i - 1
    tx = ""
        For n = j To i
            tx = tx & "," & va(n, 3)
        Next
    k = k + 1
    vb(k, 1) = va(i, 1)
    If Len(tx) > 1 Then vb(k, 2) = Mid(tx, 2, 100000)
Next

'put the result
Range("E2").Resize(k, 2) = vb
End Sub

Result:
Book1
ABCDEF
1ABCResult
21Test 1AB11AB1,AB2,AB3,AB4
31Test 2AB22AB5
41Test 3AB33AB6
51Test 4AB44AB7,AB8,AB9
62Test 5AB55AB10
73Test 6AB66AB11
84Test 7AB77AB12,AB13,AB14,AB15,AB16,AB17
94Test 8AB88AB18
104Test 9AB99AB19
115Test 10AB10
126Test 11AB11
137Test 12AB12
147Test 13AB13
157Test 14AB14
167Test 15AB15
177Test 16AB16
187Test 17AB17
198Test 18AB18
209Test 19AB19
Sheet2
Perfect !! thank you a lot for your support

If you dont mind i will be obliged if you can explain the macro piece by piece for my knowledge and understanding so that in future i can make my own
specially the redim and unbound part . what does it do ?
 
Upvote 0
Anyone else please also share formula approach for old version of excel please
 
Upvote 0
Another code option

VBA Code:
Sub test()
    Dim a As Variant
    Dim i As Long
    a = Cells(1).CurrentRegion
    With CreateObject("scripting.dictionary")
        For i = 2 To UBound(a)
            If Not .exists(a(i, 1)) Then .Add a(i, 1), a(i, 3) Else: .Item(a(i, 1)) = .Item(a(i, 1)) & "," & a(i, 3)
        Next
        Cells(2, 5).Resize(.Count, 2) = Application.Transpose(Array(.keys, .items))
    End With
End Sub
 
Upvote 0
achieve this by using Power Pivot and Concatenatex function. Here is a tutorial.


Row LabelsConcat
1AB1, AB2, AB3, AB4
2AB5
3AB6
4AB7, AB8, AB9
5AB10
6AB11
7AB12, AB13, AB14, AB15, AB16, AB17
8AB18
9AB19
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top