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
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
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