Extracting First and Last Name from Email address

Hobolord

Board Regular
Joined
Sep 9, 2015
Messages
64
I currently have the below code that gives me the first name of someone from their email (assuming their email is in the format Firstname.Lastname@Email.com).

Code:
Sub Email ()

Dim EmailAddress    As String
Dim Name               As String

EmailAddress = Sheets("Detail").Range("A3").Value

        Position = 0
        Char = "Z"
        Do Until Char = "."
            Position = Position + 1
            Char = Mid(EmailAddress, Position, 1)
        Loop
        NameL = Position - 1
        Name = Left(EmailAddress, NameL)
        
Msgbox (Name)

End Sub


I need to also have a way to get the last name, and I'm terrible with the mid function. Would anyone be able to assist with the VBA code that would be able to provide the last name from an email address in the format Firstname.Lastname@email.com?
Thank you in advance!

Edit: Formatting
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Loops are not necessary. Just use the InStr function to look to find where the period and @ symbol occur.

Try this:
Code:
Sub Email()

Dim EmailAddress As String
Dim FirstName As String
Dim LastName As String
Dim Period As Long
Dim At As Long

EmailAddress = Sheets("Detail").Range("A3").Value

'Find location of Period and At symbol in email address
Period = InStr(EmailAddress, ".")
At = InStr(EmailAddress, "@")

FirstName = Left(EmailAddress, Period - 1)
LastName = Mid(EmailAddress, Period + 1, At - Period - 1)

MsgBox "First Name: " & FirstName & vbCrLf & _
        "Last Name: " & LastName

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top