Double names to single

Lowggy

New Member
Joined
Mar 3, 2018
Messages
39
Does anyone know of a way to take a list of names of couples with the same last name that are on two adjacent rows in an Excel file and turn it into a single line.

Ie John Smith
Sharon Smith

to Smiths, John and Sharon

Thanks.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
try this....it places the result 2 columns to the right of A.

Code:
Sub NameJoin()
Dim bEnd As Boolean
Dim vPrevLast, vLast, vFirst, vBoth, vName, vPrevName
Dim i As Integer


Range("A2").Select
While ActiveCell.Value <> ""
   
   vName = ActiveCell.Value
   i = InStr(vName, " ")
   vFirst = Left(vName, i - 1)
   vLast = Mid(vName, i + 1)
   
   If vLast <> vPrevLast Then
       If bEnd Then
         vBoth = vPrevName
         ActiveCell.Offset(0, 2).Value = vBoth
         bEnd = False
       End If
       
         vBoth = vLast & ", " & vFirst & " and "
         bEnd = True
   Else
     If bEnd Then
        vBoth = vBoth & vFirst
        ActiveCell.Offset(0, 2).Value = vBoth
        vBoth = ""
     
        bEnd = False
     End If
     
   End If
   vPrevName = vLast & ", " & vFirst
   vPrevLast = vLast
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub
 
Upvote 0
try this....it places the result 2 columns to the right of A.

Code:
Sub NameJoin()
Dim bEnd As Boolean
Dim vPrevLast, vLast, vFirst, vBoth, vName, vPrevName
Dim i As Integer


Range("A2").Select
While ActiveCell.Value <> ""
   
   vName = ActiveCell.Value
   i = InStr(vName, " ")
   vFirst = Left(vName, i - 1)
   vLast = Mid(vName, i + 1)
   
   If vLast <> vPrevLast Then
       If bEnd Then
         vBoth = vPrevName
         ActiveCell.Offset(0, 2).Value = vBoth
         bEnd = False
       End If
       
         vBoth = vLast & ", " & vFirst & " and "
         bEnd = True
   Else
     If bEnd Then
        vBoth = vBoth & vFirst
        ActiveCell.Offset(0, 2).Value = vBoth
        vBoth = ""
     
        bEnd = False
     End If
     
   End If
   vPrevName = vLast & ", " & vFirst
   vPrevLast = vLast
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub

I must be doing something wrong because this did not work. I copied your code. I opened my Excel sheet, went to developer mode, clicked on Visual Basic, then sheet 1 and did an insert module and then pasted the code. When I run the macro it just puts the names, in the exact same format I had them in, into the third column over. What am I doing wrong?
 
Upvote 0
I must be doing something wrong because this did not work. I copied your code. I opened my Excel sheet, went to developer mode, clicked on Visual Basic, then sheet 1 and did an insert module and then pasted the code. When I run the macro it just puts the names, in the exact same format I had them in, into the third column over. What am I doing wrong?
try this....it places the result 2 columns to the right of A.

Code:
Sub NameJoin()
Dim bEnd As Boolean
Dim vPrevLast, vLast, vFirst, vBoth, vName, vPrevName
Dim i As Integer


Range("A2").Select
While ActiveCell.Value <> ""
   
   vName = ActiveCell.Value
   i = InStr(vName, " ")
   vFirst = Left(vName, i - 1)
   vLast = Mid(vName, i + 1)
   
   If vLast <> vPrevLast Then
       If bEnd Then
         vBoth = vPrevName
         ActiveCell.Offset(0, 2).Value = vBoth
         bEnd = False
       End If
       
         vBoth = vLast & ", " & vFirst & " and "
         bEnd = True
   Else
     If bEnd Then
        vBoth = vBoth & vFirst
        ActiveCell.Offset(0, 2).Value = vBoth
        vBoth = ""
     
        bEnd = False
     End If
     
   End If
   vPrevName = vLast & ", " & vFirst
   vPrevLast = vLast
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,780
Messages
6,132,669
Members
449,746
Latest member
signuptoignore

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