concatenate text from multiple cells based on different cell values

glad_ir

Board Regular
Joined
Nov 22, 2020
Messages
146
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I wonder if somebody could help with this one please.

I have a sheet with peoples names in column A and their assigned Team in column B (e.g. Team A, Team B). Is there a way to concatenate all the people assigned a common team in a single cell - e.g. the names of all the people assigned Team A in cell D1 separated by commas or every better on different "rows" within the cell (not sure rows is the correct term here).

Any help would be very much appreciated.

thank you,
Iain
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You could try this macro with a copy of your workbook.

VBA Code:
Sub ListTeamMembers()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Set d = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(a)
    If d.exists(a(i, 2)) Then
      d(a(i, 2)) = d(a(i, 2)) & vbLf & a(i, 1)
    Else
      d(a(i, 2)) = a(i, 1)
    End If
  Next i
  Range("D1").Resize(2, d.Count).Value = Application.Index(Array(d.keys, d.Items), 0, 0)
End Sub

My sample data is in columns A:B below and the other columns were produced by the above code.

glad_ir.xlsm
ABCDEF
1NameTeamTeam ATeam BTeam C
2Name 1Team AName 1 Name 4Name 2 Name 3 Name 6Name 5
3Name 2Team B
4Name 3Team B
5Name 4Team A
6Name 5Team C
7Name 6Team B
8
Sheet1
 
Upvote 0
Hi Peter,

Thank you for your reply. This is amazing and works a treat!

Can I make a further request please. Can the code be modified to concatenate the names of the members for Team A in cell D1 with "Team A" in cell E1, the names of the members of Team B in cell D2 with "Team B" in E2, members of Team C in cell D3 with "Team C" in cell E3 etc? I have tried to modify but don't know what I'm doing!

Thank you again for your help.
Iain
 
Upvote 0
Can the code be modified to concatenate the names of the members for Team A in cell D1 with "Team A" in cell E1, the names of the members of Team B in cell D2 with "Team B" in E2, members of Team C in cell D3 with "Team C" in cell E3 etc?
Like this?

VBA Code:
Sub ListTeamMembers_v2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Set d = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(a)
    If d.exists(a(i, 2)) Then
      d(a(i, 2)) = d(a(i, 2)) & vbLf & a(i, 1)
    Else
      d(a(i, 2)) = a(i, 1)
    End If
  Next i
  Range("D1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Items, d.Keys))
End Sub

glad_ir.xlsm
ABCDE
1NameTeamName 1 Name 4Team A
2Name 1Team AName 2 Name 3 Name 6Team B
3Name 2Team BName 5Team C
4Name 3Team B
5Name 4Team A
6Name 5Team C
7Name 6Team B
8
Sheet1
 
Upvote 0
Like this?

VBA Code:
Sub ListTeamMembers_v2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Set d = CreateObject("Scripting.Dictionary")
 
  For i = 1 To UBound(a)
    If d.exists(a(i, 2)) Then
      d(a(i, 2)) = d(a(i, 2)) & vbLf & a(i, 1)
    Else
      d(a(i, 2)) = a(i, 1)
    End If
  Next i
  Range("D1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Items, d.Keys))
End Sub

glad_ir.xlsm
ABCDE
1NameTeamName 1 Name 4Team A
2Name 1Team AName 2 Name 3 Name 6Team B
3Name 2Team BName 5Team C
4Name 3Team B
5Name 4Team A
6Name 5Team C
7Name 6Team B
8
Sheet1
Exactly like that! Thank you so much for your help. I am in awe of your coding skills.

thank you,
Iain
 
Upvote 0
Like this?

VBA Code:
Sub ListTeamMembers_v2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Set d = CreateObject("Scripting.Dictionary")
 
  For i = 1 To UBound(a)
    If d.exists(a(i, 2)) Then
      d(a(i, 2)) = d(a(i, 2)) & vbLf & a(i, 1)
    Else
      d(a(i, 2)) = a(i, 1)
    End If
  Next i
  Range("D1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Items, d.Keys))
End Sub

glad_ir.xlsm
ABCDE
1NameTeamName 1 Name 4Team A
2Name 1Team AName 2 Name 3 Name 6Team B
3Name 2Team BName 5Team C
4Name 3Team B
5Name 4Team A
6Name 5Team C
7Name 6Team B
8
Sheet1
Hi Peter,

Apologies - one last request. Is it possible to limit the macro to work within a specified number of rows - i.e. only concatenate names in rows A2 to A30 and ignore anything from A31 down?

Thank you,
Iain
 
Upvote 0
Try
VBA Code:
Sub ListTeamMembers_v3()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  a = Range("A2:B30").Value
  Set d = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then
      If d.exists(a(i, 2)) Then
        d(a(i, 2)) = d(a(i, 2)) & vbLf & a(i, 1)
      Else
        d(a(i, 2)) = a(i, 1)
      End If
    End If
  Next i
  Range("D1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Items, d.Keys))
End Sub
 
Upvote 0
Solution
Try
VBA Code:
Sub ListTeamMembers_v3()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  a = Range("A2:B30").Value
  Set d = CreateObject("Scripting.Dictionary")
 
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then
      If d.exists(a(i, 2)) Then
        d(a(i, 2)) = d(a(i, 2)) & vbLf & a(i, 1)
      Else
        d(a(i, 2)) = a(i, 1)
      End If
    End If
  Next i
  Range("D1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Items, d.Keys))
End Sub
This is perfect!

Thank you so much for your help.

Best regards,
Iain
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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