String concatenation in multiple row

trduy1908

New Member
Joined
Nov 29, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Sorry Everyone,

I have data 2 column A, B with 6 rows below:
AB
text 1Atext 1B
text 2B
text 3B
text 2Atext 4B
text 5B
text 3Atext 6B
I want to run VBA to result:
AB
text 1Atext 1B, text 2B, text 3B
text 2Atext 4B, text 5B
text 3Atext 6B
Please give me keyword to research or solution ?
Thanks so much!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Here is one way.

Book1
ABCDE
1text 1Atext 1Btext 1Atext 1B,text 2B,text 3B
2text 2Btext 2Atext 4B,text 5B
3text 3Btext 3Atext 6B
4text 2Atext 4B
5text 5B
6text 3Atext 6B
Sheet1


VBA Code:
Sub COMBO()
Dim r As Range:         Set r = Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")

For i = 1 To UBound(AR)
    If i > 1 Then If AR(i, 1) = "" Then AR(i, 1) = AR(i - 1, 1)
    SD(AR(i, 1)) = Join(Array(SD(AR(i, 1)), AR(i, 2)), IIf(IsEmpty(SD(AR(i, 1))), vbNullString, ","))
Next i

Range("D1").Resize(SD.Count) = Application.Transpose(SD.keys())
Range("E1").Resize(SD.Count) = Application.Transpose(SD.items())
End Sub
 
Upvote 0
Could also use Power Query.

Book1
ABCDE
1Column1Column2Column1Combine
2text 1Atext 1Btext 1Atext 1B,text 2B,text 3B
3text 2Btext 2Atext 4B,text 5B
4text 3Btext 3Atext 6B
5text 2Atext 4B
6text 5B
7text 3Atext 6B
Sheet2


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FillDown = Table.FillDown(Source,{"Column1"}),
    Group = Table.Group(FillDown, {"Column1"}, {{"Combine", each Text.Combine(_[Column2],",")}})
in
    Group
 
Upvote 0
Or with a formula.

Excel
ABCDE
1text 1Atext 1Btext 1Atext 1B, text 2B, text 3B
2text 2Btext 2Atext 4B, text 5B
3text 3Btext 3Atext 6B
4text 2Atext 4B
5text 5B
6text 3Atext 6B
Data
Cell Formulas
RangeFormula
D1:E3D1=LET(fd,SCAN("",A1:A6,LAMBDA(s,c,IF(c="",s,c))),u,UNIQUE(fd),HSTACK(u,BYROW(u,LAMBDA(br,TEXTJOIN(", ",,IF(br=fd,B1:B6,""))))))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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