2017-03-08 186 views
0

我必須在名稱和電子郵件地址組合的列中提取客戶名稱。會顯示在此列什麼的例子可能看起來像:從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 

但我需要類似的東西來提取名稱。

任何人都可以協助嗎?

回答

1

只需製作另一個小函數即可通過刪除電子郵件地址來獲取名稱。見下面...

在工作表中使用函數。 enter image description here

Function GetName(refCell As String) 
Dim tempName As String 
    tempName = Trim(Left(refCell, Len(refCell) - Len(ExtractEmailAddress(refCell)))) 
    GetName = tempName 
End Function 

'---------------------------------------------------------- 

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 

您還可以使用內置函數代替GetNameExtractEmailAddress

=TRIM(LEFT(A1,LEN(A1)-LEN(ExtractEmailAddress(A1)))) 

enter image description here

1

公式只方法依賴於這Super User答案方面尋找細胞中的最後一個空間。然後,您只需使用LEFT(position_of_last_space-1)即可獲取電子郵件地址左側的所有內容。

=LEFT(A1,FIND("`",SUBSTITUTE(A1," ","`",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1) 

使用back-tick是替代空格。假設是名稱或電子郵件地址中沒有備份。

例子:

enter image description here

0

你嘗試這種非VBA的解決方案稱爲Flash Fill

如果你有相同的模式數據(例如電子郵件地址,你要提取第一名稱)然後:

  • 在下一列寫你想要提取的例子(例如約翰史密斯在你的例子中):
  • 選擇該單元格並按Ctrl + E
  • 您將獲得可進一步檢查的列表。

enter image description here