Wrox Programmer Forums
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 11th, 2007, 06:05 AM
Authorized User
Join Date: May 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default Email IDs


I have a list of email IDs which need to be seperated by First Name, Middle Name and Last Name.

jamie O lennox @something.com
olena z briggs@something.com

as you can see these IDs have first name middle name and a last name.

The name are seperated by dot, space, middle initial or underscore. Is there a way to write a code which can segregate the first, middle and last name in three different columns. I have tried this code but its not working. I do not need the @something.com.

Sub Names()
Rng = Selection.Rows.Count
ActiveCell.Offset(0, 0).Select
Application.ScreenUpdating = False
For i = 1 To Rng
If ActiveCell.Value = "*." Then
With ActiveCell.Characters(Start:=1, Length:="*.").Font
        .Name = "Arial"
        .FontStyle = "Regular"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = 1
    End With
    ActiveCell.Offset(0, -1).Select
    ActiveCell.Offset(1, 1).Select
ActiveCell.Offset(1, 0).Select
End If
Next i
Application.ScreenUpdating = True
End Sub

Any help on this will be greatly appreciated.

Old July 16th, 2007, 03:56 PM
Friend of Wrox
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

Not sure how you'd differentiate via code which character is the middle initial without some complex code that won't always work anyway. If it will always be Last, MI, First order for name you can extract the names relatively easily. Below is a way to separate the name into three different strings; Hopefully you can extrapolate from there:
Private Sub CommandButton1_Click()

Dim sAddress As String, sFirst As String, sMI As String, sLast As String, iChar As Long
sAddress = Selection.Value
iChar = InStr(1, sAddress, "@")
If iChar > 0 Then sAddress = Left(sAddress, iChar - 1) 'Chop off Everything @ and after
sAddress = Replace(sAddress, ".", ",") 'Replace . with ,
sAddress = Replace(sAddress, " ", ",") 'Replace spaces with ,
sAddress = Replace(sAddress, "_", ",") 'replace _ with ,
'Removes any instance where replace could cause two or more camas
Do While InStr(1, sAddress, ",,") > 0
  sAddress = Replace(sAddress, ",,", ",")
If Right(sAddress, 1) = "," Then sAddress = Left(sAddress, Len(sAddress) - 1)
iChar = InStr(1, sAddress, ",")
If iChar > 0 Then
  sFirst = Left(sAddress, iChar - 1)
  sLast = Right(sAddress, Len(sAddress) - iChar)
  iChar = InStr(1, sLast, ",")
' Only Separates middle inital when there is another ,
  If iChar > 0 Then
    sMI = Left(sLast, iChar - 1)
    sLast = Right(sLast, Len(sLast) - iChar)
  End If
  sFirst = sAddress
End If
MsgBox "First: " & sFirst & vbCrLf & "MI: " & sMI & vbCrLf & "Last: " & sLast

End Sub

Hope this helps.

Old July 16th, 2007, 03:57 PM
Friend of Wrox
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

P.S. If ALL your middle initial occurances will always be the only capital letter, you could use that for your separation criteria.

Similar Threads
Thread Thread Starter Forum Replies Last Post
ids dhoward VB.NET 2002/2003 Basics 4 December 17th, 2007 05:46 PM
Custom Generate IDs wackoyacky SQL Language 4 June 4th, 2007 08:53 AM
Dialog Help Ids sumeetpm Visual C++ 0 October 31st, 2006 03:19 PM
Ints or Unique IDs? pettrer ASP.NET 2.0 Basics 3 June 5th, 2006 08:28 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.