我必須在名稱和電子郵件地址組合的列中提取客戶名稱。會顯示在此列什麼的例子可能看起來像:從excel中提取名稱
John Smith [email protected]
Joe Bloggs [email protected]
Justin Credible [email protected]
我發現這個很酷的VBA中提取電子郵件地址。
Function ExtractEmailAddress(s As String) As String
Dim AtSignLocation As Long
Dim i As Long
Dim TempStr As String
Const CharList As String = "[A-Za-z0-9._-]"
'Get location of the @
AtSignLocation = InStr(s, "@")
If AtSignLocation = 0 Then
ExtractEmailAddress = "" 'not found
Else
TempStr = ""
'Get 1st half of email address
For i = AtSignLocation - 1 To 1 Step -1
If Mid(s, i, 1) Like CharList Then
TempStr = Mid(s, i, 1) & TempStr
Else
Exit For
End If
Next i
If TempStr = "" Then Exit Function
'get 2nd half
TempStr = TempStr & "@"
For i = AtSignLocation + 1 To Len(s)
If Mid(s, i, 1) Like CharList Then
TempStr = TempStr & Mid(s, i, 1)
Else
Exit For
End If
Next i
End If
'Remove trailing period if it exists
If Right(TempStr, 1) = "." Then TempStr = _
Left(TempStr, Len(TempStr) - 1)
ExtractEmailAddress = TempStr
End Function
但我需要類似的東西來提取名稱。
任何人都可以協助嗎?