Macro to combine all First Names from duplicate Last Name

freerskys

New Member
Joined
Jul 24, 2014
Messages
29
Office Version
  1. 2010
Platform
  1. Windows
Hello Friends, I need help with a project I’m working on, please.
There are 4000 Rows, consisting of, Cell A = First Name , Cell B = Last Name.
Last Name will have Doubles, if so, all the first names that are associated with the double last names, would all go next to the first last name , all in one cell.

Example:

Tom King
Lucy King
Jane King

Result: Tom, Lucy, Jane King.

Put this on the same row next to the King (Cell).
Do this, till the end of the 4000 rows.
Using Excel 2010.
Thank you.
 
Like this? No more sheet2?
VBA Code:
Sub GroupName()

Dim n As Long
Dim key As Variant
Dim cell As Range, rngData As Range
Dim dName As Object
Dim ws1 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set dName = CreateObject("Scripting.Dictionary")

Set rngData = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If Not dName.Exists(cell.Offset(0, 1).Value) Then
        dName.Add cell.Offset(0, 1).Value, cell.Value
    Else
        dName(cell.Offset(0, 1).Value) = dName(cell.Offset(0, 1).Value) & ", " & cell.Value
    End If
Next

n = 1
For Each key In dName
    n = n + 1
    ws1.Range("D" & n) = dName(key) & " " & key
Next

End Sub
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Like this? No more sheet2?
VBA Code:
Sub GroupName()

Dim n As Long
Dim key As Variant
Dim cell As Range, rngData As Range
Dim dName As Object
Dim ws1 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set dName = CreateObject("Scripting.Dictionary")

Set rngData = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If Not dName.Exists(cell.Offset(0, 1).Value) Then
        dName.Add cell.Offset(0, 1).Value, cell.Value
    Else
        dName(cell.Offset(0, 1).Value) = dName(cell.Offset(0, 1).Value) & ", " & cell.Value
    End If
Next

n = 1
For Each key In dName
    n = n + 1
    ws1.Range("D" & n) = dName(key) & " " & key
Next

End Sub
Sheet 2 is not needed,
It's not spacing correctly in column D, it's putting it all together.
 
Upvote 0
Like this?
VBA Code:
Option Explicit
Sub GroupName()
Dim arr(1 To 1000000, 1 To 1)
Dim spl, Lr&, k&, cell As Range, key, key1, st As String
Dim dic As Object, dic1 As Object
Set dic = CreateObject("Scripting.dictionary")
Set dic1 = CreateObject("Scripting.dictionary")
Lr = Cells(Rows.Count, "A").End(xlUp).Row
    ' create unique full name list
    For Each cell In Range("A2:A" & Lr)
        If Not dic.exists(cell.Value) Then
            dic.Add cell.Value, ""
        End If
    Next
    'creat unique last name list
    For Each key1 In dic.keys
        spl = Split(key1, " ")
        If Not dic1.exists(spl(UBound(spl))) Then
            dic1.Add spl(UBound(spl)), ""
        End If
    Next
    'loop through last name then combine first name
    For Each key1 In dic1.keys
        For Each key In dic.keys
            spl = Split(key, " ")
            If spl(UBound(spl)) Like key1 Then
                st = st & "," & Left(key, Len(key) - Len(spl(UBound(spl))))
            End If
        Next
        k = k + 1
        arr(k, 1) = Right(st, Len(st) - 1) & key1
        st = ""
    Next
    k = 0
    'write down to column D
    For Each cell In Range("A2:A" & Lr)
        spl = Split(cell, " ")
        If Not cell.Offset(-1, 0) Like "*" & spl(UBound(spl)) Then
        k = k + 1
        cell.Offset(0, 3).Value = arr(k, 1)
        End If
    Next
End Sub

Capture.JPG
 
Upvote 0
If you meant something like @bebo021999 shown, then the modified code would be
VBA Code:
Sub GroupName1()

Dim LName As String
Dim cell As Range, rngData As Range
Dim dName As Object
Dim ws As Worksheet

Application.ScreenUpdating = False

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set dName = CreateObject("Scripting.Dictionary")

Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If Not dName.Exists(cell.Offset(0, 1).Value) Then
        dName.Add cell.Offset(0, 1).Value, cell.Value
    Else
        dName(cell.Offset(0, 1).Value) = dName(cell.Offset(0, 1).Value) & ", " & cell.Value
    End If
    cell = cell & " " & cell.Offset(0, 1)
    cell.Offset(0, 1) = ""
Next

For Each cell In rngData
    LName = Split(cell)(1)
    If dName.Exists(LName) Then
        ws.Range("D" & cell.Row) = dName(LName) & " " & LName
        dName.Remove LName
    End If
Next

End Sub
 
Upvote 0
Solution
This is exactly it, both Zot and bebo021999 got it right.
Thank you, very much.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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