Combining Cells based on Condition

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello all, i want to combine data in B column based on data in A column. If value of A column cells are same then adjacent B column cells should be combined as one cell. please refer the example for input and expected output.
Thank you.

Book2
ABCDEF
1InputExpected Output
2Column AColumn BColumn AColumn B
3A1JamesA1James Vic Nate Mike Pete Augi Don
4A1VicA1
5A1NateA1
6A1MikeA1
7A1PeteA1
8A1AugiA1
9A1DonA1
10A2JamesA2James Vic Nate
11A2VicA2
12A2NateA2
13A3NateA3 Nate Mike Pete Augi Don
14A3MikeA3
15A3PeteA3
16A3AugiA3
17A3DonA3
Sheet14
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
How about this?

XLTemplate1
ABCDEF
1InputExpected Output
2Column AColumn BColumn AColumn B
3A1JamesA1James Vic Nate Mike Pete Augi Don
4A1VicA1 
5A1NateA1 
6A1MikeA1 
7A1PeteA1 
8A1AugiA1 
9A1DonA1 
10A2JamesA2James Vic Nate
11A2VicA2 
12A2NateA2 
13A3NateA3Nate Mike Pete Augi Don
14A3MikeA3 
15A3PeteA3 
16A3AugiA3 
17A3DonA3 
Sheet1
Cell Formulas
RangeFormula
F3:F17F3=IF(COUNTIF($A$3:A3,A3)=1,tj($A$3:$B$17,E3),"")


VBA Code:
Function TJ(r As Range, k As String)
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim AR() As Variant:    AR = r.Value2

For i = 1 To UBound(AR)
    If AR(i, 1) = k Then
        If Not SD.exists(AR(i, 2)) Then SD.Add AR(i, 2), AR(i, 2)
    End If
Next i

TJ = Join(SD.keys, " ")

End Function
 
Upvote 0
How about this?

XLTemplate1
ABCDEF
1InputExpected Output
2Column AColumn BColumn AColumn B
3A1JamesA1James Vic Nate Mike Pete Augi Don
4A1VicA1 
5A1NateA1 
6A1MikeA1 
7A1PeteA1 
8A1AugiA1 
9A1DonA1 
10A2JamesA2James Vic Nate
11A2VicA2 
12A2NateA2 
13A3NateA3Nate Mike Pete Augi Don
14A3MikeA3 
15A3PeteA3 
16A3AugiA3 
17A3DonA3 
Sheet1
Cell Formulas
RangeFormula
F3:F17F3=IF(COUNTIF($A$3:A3,A3)=1,tj($A$3:$B$17,E3),"")


VBA Code:
Function TJ(r As Range, k As String)
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim AR() As Variant:    AR = r.Value2

For i = 1 To UBound(AR)
    If AR(i, 1) = k Then
        If Not SD.exists(AR(i, 2)) Then SD.Add AR(i, 2), AR(i, 2)
    End If
Next i

TJ = Join(SD.keys, " ")

End Function
Hey Irobbo, It is working as per the sample data. Appreciate your efforts. Is it possible to have complete VBA solution instead of formula and UDF combination. Also my range is dynamic in A and B columns. Thank you.
 
Upvote 0
Is it possible to have complete VBA solution
You could try this with a copy of your workbook. I have assumed that your data actually has headings in row 1 and the data starts in row 2.

VBA Code:
Sub Combine_Values()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim s As String
  
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a), 1 To 1)
  b(1, 1) = a(1, 2)
  For i = UBound(a) To 2 Step -1
    s = a(i, 2) & " " & s
    If a(i, 1) <> a(i - 1, 1) Then
      b(i, 1) = RTrim(s)
      s = vbNullString
    End If
  Next i
  Range("B1").Resize(UBound(b)).Value = b
End Sub
 
Upvote 0
Solution
You could try this with a copy of your workbook. I have assumed that your data actually has headings in row 1 and the data starts in row 2.

VBA Code:
Sub Combine_Values()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim s As String
 
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a), 1 To 1)
  b(1, 1) = a(1, 2)
  For i = UBound(a) To 2 Step -1
    s = a(i, 2) & " " & s
    If a(i, 1) <> a(i - 1, 1) Then
      b(i, 1) = RTrim(s)
      s = vbNullString
    End If
  Next i
  Range("B1").Resize(UBound(b)).Value = b
End Sub
Hello Peter, it is working as expected. Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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