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, ",,", ",")
Loop
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
Else
sFirst = sAddress
End If
MsgBox "First: " & sFirst & vbCrLf & "MI: " & sMI & vbCrLf & "Last: " & sLast
End Sub
-------------------------------------------------------------------
Hope this helps.
|