Separate Initials from Name

  • Thread starter Thread starter Steve Warren
  • Start date Start date
S

Steve Warren

Looking for the code to pull Initials out of Names
example
(John S. Smith) J S S

Thank you
Steve
 
Steve Warren said:
Looking for the code to pull Initials out of Names
example
(John S. Smith) J S S

Function GetInitials(strName As String)

Dim i As Byte

GetInitials = Left(Trim(strName), 1)

For i = 1 To Len(strName)
If Mid(strName, i, 1) = " " _
And Mid(strName, i + 1, 1) <> " " Then
GetInitials = GetInitials & " " & Mid(strName, i + 1, 1)
End If
Next i

End Function
 
try

Dim varName As Variant
Dim i As Integer, strInitials As String

varName = Split(ControlOrVariableName, " ")

For i = 0 To UBound(varName)
strInitials = Trim(strInitials & " " & Left(varName(i), 1))
Next i

substitute the name of a control or variable for ControlOrVariableName, of
course. you could also wrap this code in a function, with a required string
argument to supply the name, which then returns the value of strInitials as
a String data type.

note that the above code will return the first letter of each part of the
name that's separated by a space. so if the person's name happens to be

Le Var Michael De La Cruz

you'll get back

L V M D L C

you could limit the code to looping through only the first three "parts" of
the name, which would work fine for

Levar Michael De La Cruz

but would would return

L V M

for the longer-spaced name.

hth
 
Back
Top