I have a sheet with text in a range in column B. The text in each cell can include none or multiple email addresses. I want to extract the first found email address in each cell in the range and copy it to column T (in the same row as the source).
I have found this Function at http://spreadsheetpage.com/index.php/tip/extracting_an_email_address_from_text/ for extracting the emails.
It works perfectly for extracting the first email as I want. But I have had no success in converting the Function into a Sub that will loop through the source range and copy it to column T.
Any help or pointers much appreciated.
I have found this Function at http://spreadsheetpage.com/index.php/tip/extracting_an_email_address_from_text/ for extracting the emails.
Code:
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._-]"
[COLOR=#0000FF]'Get location of the @[/COLOR]
AtSignLocation = InStr(s, "@")
If AtSignLocation = 0 Then
ExtractEmailAddress = "" 'not found
Else
TempStr = ""
[COLOR=#0000FF]'Get 1st half of email address[/COLOR]
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
[COLOR=#0000FF]'get 2nd half[/COLOR]
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
[COLOR=#0000FF]'Remove trailing period if it exists[/COLOR]
If Right(TempStr, 1) = "." Then TempStr = _
Left(TempStr, Len(TempStr) - 1)
ExtractEmailAddress = TempStr
End Function
It works perfectly for extracting the first email as I want. But I have had no success in converting the Function into a Sub that will loop through the source range and copy it to column T.
Any help or pointers much appreciated.