2010-10-26 107 views
4

我需要提取分號之間的特定單詞,條件是這些單詞包含意味着電子郵件的「@」。在Excel中從分隔字符串中提取單詞?

下面是一個例子:

A1 >> james john;Paris street;p.o. box:12345;tel.987654321;[email protected];usa 
B1 >> david eric;34th street;tel.543212345;[email protected];canada;ottawa 

...等等

注意,沒有用於電子郵件沒有具體的地方,以便它可以在任何地方。除了「@」以外,沒有常用的單詞或字符,所以必須有一個公式可以在分號+包含的「@」之間進行選擇以提取電子郵件並將其放入A2和B2等等

+0

更清楚地請! – pinichi 2010-10-26 07:42:03

回答

2

將數據複製到列A
選擇數據。
Data - >Text to Columns...
DelimitedNext >
Semicolon
Finish

現在,你必須在列A-F數據。

G1輸入:

=INDEX(A1:F1,1,MATCH("~",IF(ISNUMBER(FIND("@",A1:F1)),A1:F1),-1)) 

,然後按Ctrl + T恤+ Enter鍵。將公式向下拖動。

+0

無需程序即可完成+1 ... – st0le 2010-10-26 08:49:40

1

這是一個使用正則表達式的VBA函數:

Function EmailFrom(source As String) 

Dim Matches As Object 

    With CreateObject("VBScript.RegExp") 

     .Pattern = "(^|;)([^;@][email protected][^;@]+);?" 

     If .Test(source) Then 
      Set Matches = .Execute(source) 
      EmailFrom = Matches(0).SubMatches(1) 
     Else 
      EmailFrom = CVErr(xlErrNA) 
     End If 

    End With 

End Function 

[更新]甚至(濃縮)

Function EmailFrom(source As String) 

    With CreateObject("VBScript.RegExp") 
     .Pattern = "(^|;)([^;@][email protected][^;@]+);?" 
     With .Execute(source) 
      If .Count > 0 Then 
       EmailFrom = .Item(0).SubMatches(1) 
      Else 
       EmailFrom = CVErr(xlErrNA) 
      End If 
     End With 
    End With 

End Function 
+0

爲什麼要運行正則表達式兩次('Test'和'Execute')?當然只有執行並檢查'Matches.Count> 0'就足夠了嗎? – GSerg 2010-10-26 08:25:43

+0

@GSerg - 有道理,謝謝。實際上,顯式的Matches對象也可以用'With ... End With'模塊代替。更新。 – 2010-10-26 08:42:00

+0

這看起來是正確的做法 – MikeAinOz 2010-10-26 09:21:03

2
B1  =FIND("@",A1) 
C1  =IF(ISERR(FIND(";",A1,B1)),LEN(A1)+1,FIND(";",A1,B1)) 
D1  =MAX(IF(ISERR(FIND(";",LEFT(A1,C1-1),ROW(INDIRECT("A1:A"&B1)))),0,FIND(";",LEFT(A1,C1-1),ROW(INDIRECT("A1:A"&B1))))) 
E1  =MID(A1,D1+1,C1-D1-1) 

,如果你願意的話,可以將那些爲一個超級公式。

B1 = the location of the at sign 
C1 = the first semicolon after the @ 
D1 = the semicolon before the at sign (array entered) 
0

有一個簡單的分隔表達式可以幫助您在特定的中斷點處分割字符串。在這種情況下,分號就是你想要打斷字符串的地方。 您只需點擊頂部菜單中的DATA,然後選擇包含數據的列,然後在頂部導航欄中選擇TEXT TO COLUMN。它會將數據按照您指定的數據分割,而您的情況則是您想分割數據的分號。

我試圖發佈屏幕截圖幫助,但本網站的垃圾郵件檢測器不允許我。但你可以隨時訪問我的hubpage http://nikhilchandra.hubpages.com/。我希望它可以幫助:-)

相關問題