Create macro to separate first name and last name in email and plug it into columns

Jyggalag

Active Member
Joined
Mar 8, 2021
Messages
445
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Dear all,

I am currently working on a spreadsheet that looks like this, albeit the real one is much larger:

1642513991403.png


I was wondering if there would be some way in which I could create a macro or some VBA code attached to a macro where, once i press it, it takes the e-mails from C2-C12 and takes the first name and puts it in column A and the last name in column B. Almost all of the emails that I work with have emails that basically follow a rule of "Firstname.Lastname@email.com". If they do not have this first name separated from the last name by a dot ".", I would like for the macro to return the value "X" and "X" for both first name and last name in column A and B.

Is this possible? And if so, how might I carry out such a task?

Thank you so much for your time and assistance everybody! It is greatly appreciated.

Please let me know if you have questions or need further information and I will respond ASAP.

Kindest regards,
Jyggalag.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Here's one solution. I also through the proper case in just in case not all the emails are proper case already.

Cell Formulas
RangeFormula
A2:A8A2=IFERROR(PROPER(LEFT(LEFT($C2,FIND("@",$C2)-1),FIND(".",LEFT($C2,FIND("@",$C2)-1))-1)),"X")
B2:B8B2=IFERROR(PROPER(RIGHT(LEFT($C2,FIND("@",$C2)-1),LEN(LEFT($C2,FIND("@",$C2)-1))-FIND(".",LEFT($C2,FIND("@",$C2)-1)))),"X")
 
Upvote 0
Another formula option
+Fluff 1.xlsm
ABC
1FirstLastemail
2BobIgorbob.igor@xyc.com
3NataliaHenriksennatalia.henriksen@pharmatest.com
4GeorgeWashingtongeorge.washington@senator.com
5DonaldHarrisdonald.harris@privateequity.com
6XXsomething@gmail.com
7XXiaminoneword@badcorp.com
8SteveWallissteve.wallis@entpres.com
Data
Cell Formulas
RangeFormula
A2:A8A2=IF(FIND(".",C2)>FIND("@",C2),"X",PROPER(LEFT(C2,FIND(".",C2)-1)))
B2:B8B2=IF(FIND(".",C2)>FIND("@",C2),"X",PROPER(MID(REPLACE(C2,FIND("@",C2),200,""),FIND(".",C2)+1,100)))
 
Upvote 0
Or with a macro
VBA Code:
Sub Jyggalag()
   With Range("C2", Range("C" & Rows.Count).End(xlUp))
      .Offset(, -2).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),""X"",PROPER(LEFT(#,FIND(""."",#)-1)))", "#", .Address))
      .Offset(, -1).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),""X"",PROPER(MID(REPLACE(#,FIND(""@"",#),200,""""),FIND(""."",#)+1,100)))", "#", .Address))
   End With
End Sub
 
Upvote 0
Another formula option
+Fluff 1.xlsm
ABC
1FirstLastemail
2BobIgorbob.igor@xyc.com
3NataliaHenriksennatalia.henriksen@pharmatest.com
4GeorgeWashingtongeorge.washington@senator.com
5DonaldHarrisdonald.harris@privateequity.com
6XXsomething@gmail.com
7XXiaminoneword@badcorp.com
8SteveWallissteve.wallis@entpres.com
Data
Cell Formulas
RangeFormula
A2:A8A2=IF(FIND(".",C2)>FIND("@",C2),"X",PROPER(LEFT(C2,FIND(".",C2)-1)))
B2:B8B2=IF(FIND(".",C2)>FIND("@",C2),"X",PROPER(MID(REPLACE(C2,FIND("@",C2),200,""),FIND(".",C2)+1,100)))
This is amazing Fluff, thank you so much!!

Would there be a way change to the last name formula so it returns the entire value that is written before the @ sign?

Otherwise truly amazing to see that you're active here with your great solutions! Truly appreciated :)
 
Upvote 0
Or with a macro
VBA Code:
Sub Jyggalag()
   With Range("C2", Range("C" & Rows.Count).End(xlUp))
      .Offset(, -2).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),""X"",PROPER(LEFT(#,FIND(""."",#)-1)))", "#", .Address))
      .Offset(, -1).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),""X"",PROPER(MID(REPLACE(#,FIND(""@"",#),200,""""),FIND(""."",#)+1,100)))", "#", .Address))
   End With
End Sub
Also same question for the macro. It would be nice if it, instead of printing "X" for first name could print "Team" and instead of "X" for last name, could print whatever is written before the @ sign in the email. So for example value C9 would become First name: Team, Last name: Something

Apologies for the confusion and thank you so much!
 
Upvote 0
Do you want this as a macro or formula?
 
Upvote 0
Do you want this as a macro or formula?
Preferably macro, but if you have the luxury of doing both (so I can keep an alternative solution at hand), it would be incredibly appreciated from my point of view :)

Thank you so much for your time once again!
 
Upvote 0
Ok, how about
VBA Code:
Sub Jyggalag()
   With Range("C2", Range("C" & Rows.Count).End(xlUp))
      .Offset(, -2).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),""Team"",PROPER(LEFT(#,FIND(""."",#)-1)))", "#", .Address))
      .Offset(, -1).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),left(#,find(""@"",#)-1),PROPER(MID(REPLACE(#,FIND(""@"",#),200,""""),FIND(""."",#)+1,100)))", "#", .Address))
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub Jyggalag()
   With Range("C2", Range("C" & Rows.Count).End(xlUp))
      .Offset(, -2).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),""Team"",PROPER(LEFT(#,FIND(""."",#)-1)))", "#", .Address))
      .Offset(, -1).Value = Evaluate(Replace("IF(FIND(""."",#)>FIND(""@"",#),left(#,find(""@"",#)-1),PROPER(MID(REPLACE(#,FIND(""@"",#),200,""""),FIND(""."",#)+1,100)))", "#", .Address))
   End With
End Sub
Hm I tried inserting it but I get this message now:

1642597243306.png


In my module the code looks like this:

1642597285706.png


Did I perhaps enter something in an incorrect way?

Apologies for the confusion!

Kind regards,
Jyggalag
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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