Created
June 4, 2017 12:36
-
-
Save ndthanh/d930e1f6d64e0ca8c2c8d619009a8f75 to your computer and use it in GitHub Desktop.
ExtractEmailAddress.bas https://blog.hocexcel.online
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment