Option Explicit
Sub test()
Application.ScreenUpdating = False
Dim LastRow As Long, _
TestRow As Long, _
NextColumn As Long, _
AnchorRow As Long, _
MaxCol As Long, _
BlankPos As Long, _
Phone As String, _
Contact As String, _
NextContact As String, _
SortRange As Range
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For AnchorRow = 1 To LastRow
Contact = Cells(AnchorRow, "a").Value
BlankPos = InStr(Contact, " ")
Cells(AnchorRow, "B").Value = Right(Contact, Len(Contact) - BlankPos)
Contact = Left(Contact, BlankPos - 1)
Cells(AnchorRow, "A").Value = Contact
NextColumn = 2
TestRow = AnchorRow + 1
NextContact = Left(Cells(TestRow, "a").Value, Len(Cells(TestRow, "a").Value) - 9)
While NextContact = Contact
NextColumn = NextColumn + 1
Cells(AnchorRow, NextColumn).Value = Right(Cells(TestRow, "A").Value, 8)
Cells(TestRow, 1).Value = ""
TestRow = TestRow + 1
If TestRow > LastRow Then
Set SortRange = Range(Cells(1, 1), Cells(LastRow, MaxCol))
SortRange.Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add _
Key:=Range("A1"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange SortRange
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
Exit Sub
End If
NextContact = Left(Cells(TestRow, "a").Value, Len(Cells(TestRow, "a").Value) - 9)
Wend
AnchorRow = TestRow - 1
MaxCol = WorksheetFunction.Max(NextColumn, MaxCol)
Next AnchorRow
Application.ScreenUpdating = True
End Sub
Or...
Sub Last_Pnone_Number()
Const cMail As String = "A" 'Conlumn letter with the email data
Const cPhone As String = "B" 'Column letter with the phone numbers
Dim i As Long, _
BlankPos As Long
Application.ScreenUpdating = False
For i = Cells.Find("*", , , , 1, 2).Row To 1 Step -1
BlankPos = InStr(Range(cMail & i).Value, " ")
If LCase(Left(Range(cMail & i).Value, BlankPos)) = LCase(Left(Range(cMail & i + 1).Value, BlankPos)) Then
Range(cPhone & i).Value = Range(cMail & i + 1).Value
Rows(i + 1).Delete
End If
Next
Application.ScreenUpdating = True
End Sub
Bookmarks