如果您認為上述公式對您來說很麻煩,則以下VBA代碼可以幫助您一次提取電子郵件地址。
1。 按住 ALT + F11 鍵,它會打開一個 Microsoft Visual Basic for Applications 窗口。
2。 點擊 插入 > 模塊,然後將以下宏粘貼到 模塊窗口.
VBA:從文本字符串中提取電子郵件地址
Sub ExtractEmail()
'Update 20130829
Dim WorkRng As Range
Dim arr As Variant
Dim CharList As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
arr = WorkRng.Value
CheckStr = "[A-Za-z0-9._-]"
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
extractStr = arr(i, j)
outStr = ""
Index = 1
Do While True
Index1 = VBA.InStr(Index, extractStr, "@")
getStr = ""
If Index1 > 0 Then
For p = Index1 - 1 To 1 Step -1
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = Mid(extractStr, p, 1) & getStr
Exit For
End If
getStr = getStr & "@"
For p = Index1 + 1 To Len(extractStr)
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = getStr & Mid(extractStr, p, 1)
Exit For
End If
Index = Index1 + 1
If outStr = "" Then
outStr = getStr
outStr = outStr & Chr(10) & getStr
End If
Exit Do
End If
arr(i, j) = outStr
WorkRng.Value = arr
End Sub
3。 然後按 F5 鍵來運行此代碼,您應該在彈出的對話框中選擇要使用VBA的範圍,請參見屏幕截圖:
4。 然後點擊 OK,並且已從所選文本字符串中提取電子郵件地址。 查看屏幕截圖:
1.如果單元格沒有電子郵件地址,它將顯示空白單元格。
2.如果一個單元中有多個電子郵件地址,則將提取所有電子郵件。
3.提取的電子郵件將覆蓋原始數據,因此,如果需要,最好先備份數據。
上面的方法對我們來說看起來有些複雜 Excel 初學者,在這裡,我可以推薦你一個快速簡單的工具— Kutools for Excel,其 提取電子郵件地址 實用程序,您可以輕鬆地從文本字符串中提取電子郵件地址。
Kutools for Excel : 擁有超過300個方便的 Excel 插件,30 天內免費試用,無限制.
如果你已經安裝 Kutools for Excel,請執行以下操作:
1。 選擇包含文本字符串的單元格。
2。 點擊 Kutools > 文本 > 提取電子郵件地址,請參見屏幕截圖:
3。 和 提取電子郵件地址 對話框將彈出,選擇要放置結果的單元格,請參見屏幕截圖:
4。 然後點擊 OK 按鈕,已從文本字符串中提取所有電子郵件地址,請參見屏幕截圖:
點擊下載並免費試用 Kutools for Excel 現在!
演示:從文本字符串中提取電子郵件地址 Kutools for Excel
Kutools for Excel: 擁有300多個方便 Excel 加載項,30 天內免費試用,無限制。 立即下載並免費試用!
相關文章:
如何從多個電子郵件地址提取網域 Excel?
最佳辦公生產力工具
增強您的電子表格: 體驗前所未有的效率 Kutools for Excel
Kutools for Excel 擁有超過 300 個功能, 確保只需點擊一下即可獲得您所需要的...
支援辦公室/Excel 2007-2021 及更新版本,含 365 | 提供 44 種語言版本 | 享受全功能 30 天免費試用。
Office Tab 為 Office 帶來選項卡式界面,讓您的工作更輕鬆
啟用選項卡式編輯和讀入 Word, Excel, 微軟幻燈片軟件,發布者,Access,Visio和Project。
在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
I'm not sure if my comment went through, so I'm sending it again.
Can I use the above VB script to extract domain names only? I don't need the email addresses.
Thanks
[quote]I'm not sure if my comment went through, so I'm sending it again.
Can I use the above VB script to extract domain names only? I don't need the email addresses.
ThanksBy Helen[/quote]
This works to extract the domain name only for the first email address in a cell (here arbitrarily cell A1)
=MID(A1,FIND("@",A1)+1,FIND(" ",RIGHT(A1,LEN(A1)-FIND("@",A1)),1))
This formula is fabulous; however, I need to extract the domain names only, not the entire email address. I'm not a VB expert and couldn't find out a way to modify to extract out only the domain name. Can someone assist with this?
Thanks
If I want to extract only one email address from A1, this formula does so and reports only a blank, not an error, if A1 contains no email address. I find this an easier solution than trying to master all these scripts, and it costs nothing.
=IFERROR(TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND(" ",A1&" ",FIND("@",A1))-1)," ",REPT(" ",LEN(A1))),LEN(A1))),"")
since the formula has been a great help to me, I thought I'd share my experience. I run it against a list of html webscrapes which are sometimes so long that the formula errors out.
According to wikipedia the maximum length of an email address is 254 characters so replacing the len(A1) portions with 256 improves the stability of the function:
=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND (" ",A1&" ",FIND("@",A1))-1)," ", REPT(" ",256)),256))
Brother, you are really genius.
Below formula worked for me and saved manual intervention which used to take hours
=TRIM(RIGHT(SUBSTITUTE(LEFT(H2,FIND (" ",H2&" ",FIND("@",H2))-1)," ", REPT(" ",LEN(H2))),LEN(H2)))
God Bless you
Thanks to the author of the original script; I went ahead and added a "; " separator in between multiple e-mail addresses.
Function ExtractEmailFun(extractStr As String) As String
'Update 20150723
Dim CharList As String
On Error Resume Next
CheckStr = "[A-Za-z0-9._-]"
ExtractEmailFun = ""
Index = 1
Do While True
Index1 = VBA.InStr(Index, extractStr, "@")
getStr = ""
If Index1 > 0 Then
For p = Index1 - 1 To 1 Step -1
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = Mid(extractStr, p, 1) & getStr
Exit For
End If
getStr = getStr & "@"
For p = Index1 + 1 To Len(extractStr)
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = getStr & Mid(extractStr, p, 1)
getStr = getStr
Exit For
End If
Index = Index1 + 1
getStr = getStr & "; "
If ExtractEmailFun = "" Then
ExtractEmailFun = getStr
ExtractEmailFun = ExtractEmailFun & Chr(10) & getStr
End If
Exit Do
End If
End Function
Hello all, I also was looking for a way to separate out the e-mail addresses, so I could put it into Outlook. I've added a "; " separator between the e-mail addresses so they don't run on together. Let me know what you think. Thanks to the author of the original for getting this together!
Function ExtractEmailFun(extractStr As String) As String
'Update 20150723
Dim CharList As String
On Error Resume Next
CheckStr = "[A-Za-z0-9._-]"
ExtractEmailFun = ""
Index = 1
Do While True
Index1 = VBA.InStr(Index, extractStr, "@")
getStr = ""
If Index1 > 0 Then
For p = Index1 - 1 To 1 Step -1
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = Mid(extractStr, p, 1) & getStr
Exit For
End If
getStr = getStr & "@"
For p = Index1 + 1 To Len(extractStr)
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = getStr & Mid(extractStr, p, 1)
getStr = getStr
Exit For
End If
Index = Index1 + 1
getStr = getStr & "; "
If ExtractEmailFun = "" Then
ExtractEmailFun = getStr
ExtractEmailFun = ExtractEmailFun & Chr(10) & getStr
End If
Exit Do
End If
End Function
no VBA just formula to be pasted into cell
Just change the references ( the example below looks at Cell A1)
=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("@",A1)-1)," ",REPT(" ",99)),99))&MID(A1,FIND("@",A1),FIND(" ",A1&" ",FIND("@",A1))-FIND("@",A1))
[quote]no VBA just formula to be pasted into cell
Just change the references ( the example below looks at Cell A1)
=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("@",A1)-1)," ",REPT(" ",99)),99))&MID(A1,FIND("@",A1),FIND(" ",A1&" ",FIND("@",A1))-FIND("@",A1))By ME[/quote]
Thank you. It worked for me.
[quote]...
=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("@",A1)-1)," ",REPT(" ",99)),99))&MID(A1,FIND("@",A1),FIND(" ",A1&" ",FIND("@",A1))-FIND("@",A1))By ME[/quote]
Thanks "ME", works verbatim in Google spreadsheet!