This works even for spaces with names:
Function ExtractName(str As String) As String Dim i As Long Dim splitStr() As String Dim nameParts() As String splitStr = Split(str, " ") ReDim nameParts(LBound(splitStr) To UBound(splitStr) - 4) For i = LBound(nameParts) To UBound(nameParts) nameParts(i) = splitStr(i + 2) Next i ExtractName = Join(nameParts, " ") End Function
What this does effectively is to remove four substrings: date, time, add bit and email address. Everything else in the middle is considered part of the name.
Usage example:
Debug.Print ExtractName("2014/08/19 12:59 John Doe add sample@hotmail.com ") Debug.Print ExtractName("2014/08/19 12:59 Johan Sebastian Bach add sample@hotmail.com ") Debug.Print ExtractName("2014/08/19 12:59 Fuh Wei Guo Tang add sample@hotmail.com ") Debug.Print ExtractName("2014/08/19 12:59 Jens von dem Hagen add sample@hotmail.com ") Debug.Print ExtractName("2014/08/19 12:59 José Manuel de Santiago Itthuralde add sample@hotmail.com ")
EDIT Now you say that your input line is split into two lines ... This works for me with the input you specify:
Function ExtractName(str As String) As String Dim i As Long Dim splitStr() As String Dim nameParts() As String splitStr = Split(Split(str, vbLf)(0), " ") ReDim nameParts(LBound(splitStr) To UBound(splitStr) - 2) For i = LBound(nameParts) To UBound(nameParts) nameParts(i) = splitStr(i + 2) Next i ExtractName = Join(nameParts, " ") End Function
source share