Advertisement
OrlandoDC

Handy MS Excel Function ExtractEmailAddress(s As String)

Dec 9th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. Function ExtractEmailAddress(s As String) As String
  2. Dim AtSignLocation As Long
  3. Dim i As Long
  4. Dim TempStr As String
  5. Const CharList As String = "[A-Za-z0-9._-]"
  6.  
  7. 'Get location of the @
  8. AtSignLocation = InStr(s, "@")
  9. If AtSignLocation = 0 Then
  10. ExtractEmailAddress = "" 'not found
  11. Else
  12. TempStr = ""
  13. 'Get 1st half of email address
  14. For i = AtSignLocation - 1 To 1 Step -1
  15. If Mid(s, i, 1) Like CharList Then
  16. TempStr = Mid(s, i, 1) & TempStr
  17. Else
  18. Exit For
  19. End If
  20. Next i
  21. If TempStr = "" Then Exit Function
  22. 'get 2nd half
  23. TempStr = TempStr & "@"
  24. For i = AtSignLocation + 1 To Len(s)
  25. If Mid(s, i, 1) Like CharList Then
  26. TempStr = TempStr & Mid(s, i, 1)
  27. Else
  28. Exit For
  29. End If
  30. Next i
  31. End If
  32. 'Remove trailing period if it exists
  33. If Right(TempStr, 1) = "." Then TempStr = _
  34. Left(TempStr, Len(TempStr) - 1)
  35. ExtractEmailAddress = TempStr
  36. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement