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.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This will create list in column D

VBA Code:
Sub GroupName()

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
Next

Set rngData = rngData.Offset(0, 1)
For Each cell In rngData
    If dName.Exists(cell.Value) Then
        ws.Range("D" & cell.Row) = dName(cell.Value) & " " & cell.Value
        dName.Remove cell.Value
    End If
Next

End Sub
 
Upvote 0
This will create list in column D

VBA Code:
Sub GroupName()

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
Next

Set rngData = rngData.Offset(0, 1)
For Each cell In rngData
    If dName.Exists(cell.Value) Then
        ws.Range("D" & cell.Row) = dName(cell.Value) & " " & cell.Value
        dName.Remove cell.Value
    End If
Next

End Sub
Wow, you are incredible, fast.
Thank you.
 
Upvote 0
Hey Zot, could you please add a way to delete all the doubles on the left side, keeping the one, with all the first names.
Thanks.
 
Upvote 0
Hey Zot, could you please add a way to delete all the doubles on the left side, keeping the one, with all the first names.
Thanks.
I could not understand what you meant.
 
Upvote 0
Do you want to just keep the result on the column D and delete all data in column A and B?
 
Upvote 0
I think the best way is to create a list in another sheet, leaving the original data as they are for reference just in case. Can delete the sheet after that

This is create list in Sheet2 starting from range A1
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, ws2 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")
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 = 0
For Each key In dName
    n = n + 1
    ws2.Range("A" & n) = dName(key) & " " & key
Next
    
End Sub
 
Upvote 0
This is also good, I could use that, I still need to keep the First double, Last Name in sheet 1.
Example:

Tom King Tom King
Lucy King Would look like--->
Jane King

Result: Tom, Lucy, Jane King. Result: Tom, Lucy, Jane King.

Keep the first , delete the other two.
 
Upvote 0
This is also good, I could use that, I still need to keep the First double, Last Name in sheet 1.
Example:

Tom King Tom King
Lucy King Would look like--->
Jane King

Result: Tom, Lucy, Jane King. Result: Tom, Lucy, Jane King.

Keep the first , delete the other two.
Not sure I clearly understood your request. This will create:
1) List first occurrence of the shared Last Name in column D
2) List of all combined name in 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, ws2 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")
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
        cell.Offset(0, 3) = cell & " " & cell.Offset(0, 1)
    Else
        dName(cell.Offset(0, 1).Value) = dName(cell.Offset(0, 1).Value) & ", " & cell.Value
    End If
Next

n = 0
For Each key In dName
    n = n + 1
    ws2.Range("A" & n) = dName(key) & " " & key
Next

End Sub
 
Upvote 0
Not sure I clearly understood your request. This will create:
1) List first occurrence of the shared Last Name in column D
2) List of all combined name in 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, ws2 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")
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
        cell.Offset(0, 3) = cell & " " & cell.Offset(0, 1)
    Else
        dName(cell.Offset(0, 1).Value) = dName(cell.Offset(0, 1).Value) & ", " & cell.Value
    End If
Next

n = 0
For Each key In dName
    n = n + 1
    ws2.Range("A" & n) = dName(key) & " " & key
Next

End Sub
I'm so sorry for not explaining myself better.
Would you be kind enough(third time the charm) to make column D look like, what's on Sheet 2.
The important part, I need the exact spacing that column D has now.
Example.jpg
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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