Extract text from field

M

Mark

Hi,
I'm trying to get the email domain name from an email address. So the text
between the @ sign and the first '.' after the @ sign. I'm aware this is not
100% correct as some mail addresses have more '.' behind the @ sign, any clue
how to make it better is welcome as well?
 
A

Allen Browne

Use Instr() to locate the @ and the dot in the string, and Mid() to parse
it.

This kind of thing:

Public Function GetDomain(varEmail As Variant) As Variant
'Purpose: Parse the domain from an email address.
'Assumes: Domain is between @ and first dot after that.
Dim lngPosAt As Long
Dim lngPosDot As Long
Dim strEmail As String

GetDomain = Null

If varEmail <> vbNullString Then
lngPosAt = InStr(varEmail, "@")
If lngPosAt > 0 Then
lngPosDot = InStr(varEmail, ".")
End If
If lngPosDot > 0 Then
GetDomain = Mid(varEmail, lngPosAt + 1, lngPosDot - lngPosAt -
1)
End If
End If
End Function
 
A

Allen Browne

Sorry, Mark. Can't believe I left out the starting point when searching for
the dot, after we already got it in the variable:

lngPosDot = InStr(lngPosAt, varEmail, ".")
 
Top