Option Explicit
Private strEnvironUserName As String
Private Msg As String, Ans As Integer, Config As Integer, Title As String
Public Sub Test_EnvironUserNameToFirstLastNames()
'John Citizen is the dummy name for testing the code.
'This will be replaced with Environ("UserName") once the code is working
Dim strName1 As String
Dim strName2 As String
Dim strName3 As String
Dim strName4 As String
Dim strName5 As String
Dim strName6 As String
Dim strName7 As String
'use delimiters
strName1 = UserNameToFirstLast("John.Citizen")
strName2 = UserNameToFirstLast("John_Citizen")
strName3 = UserNameToFirstLast("John Citizen")
'no delimiters
'use Capitals as delimiters
strName4 = UserNameToFirstLast("JohnCitizen")
'single name - should return as first name only
strName5 = UserNameToFirstLast("John")
'not user names - somehow pick these up and reject them
strName6 = UserNameToFirstLast("Administrator")
strName7 = UserNameToFirstLast("Admin")
Msg = "1 " & strName1
Msg = Msg & vbNewLine & "2 " & strName2
Msg = Msg & vbNewLine & "3 " & strName3
Msg = Msg & vbNewLine & "4 " & strName4
Msg = Msg & vbNewLine & "5 " & strName5
Msg = Msg & vbNewLine & "6 " & strName6
Msg = Msg & vbNewLine & "7 " & strName7
Msg = UserNameToFirstLast(Environ("UserName"))
Ans = MsgBox(Msg, Config, Title)
End Sub
Private Function UserNameToFirstLast(strUserName As String) As String
' give up if user name is all caps
If UCase(strUserName) = strUserName Then GoTo CouldNotSolve
' should have code here to give up if user name contains numerics
Select Case strUserName
' give up if user name = common non-user name log-ins
Case "Admin"
Case "Administrator"
Case "Sales"
Case "Accounts"
Case Else
' try to pass user name into first name & last name
UserNameToFirstLast = "Your user name is: First = " & FIRSTNAME(strUserName) & " Last = " & LASTNAME(strUserName)
Exit Function
End Select
CouldNotSolve:
UserNameToFirstLast = "Could not parse into first & surname"
End Function
Public Function FIRSTNAME(strName As String) As String
Dim intCharacter As Integer
FIRSTNAME = Left(strName, 1)
For intCharacter = 2 To Len(strName)
If UCase(Mid(strName, intCharacter, 1)) = Mid(strName, intCharacter, 1) Then
Exit For
Else
FIRSTNAME = FIRSTNAME & Mid(strName, intCharacter, 1)
End If
Next intCharacter
End Function
Public Function LASTNAME(strName As String) As String
Dim intCharacter As Integer
For intCharacter = 2 To Len(strName)
If UCase(Mid(strName, intCharacter, 1)) = Mid(strName, intCharacter, 1) Then
If Mid(strName, intCharacter, 1) Like "[A-Z]" = True Then
LASTNAME = Right(strName, Len(strName) - intCharacter + 1)
Else 'remove delimiter
LASTNAME = Right(strName, Len(strName) - intCharacter)
End If
Exit For
End If
Next intCharacter
End Function
Private Function ParseOutNamesFromUserName(strEnvironUserName As String) As Variant
''based on ParseOutNames http://www.cpearson.com/excel/firstlast.htm
Dim FIRSTNAME As String
Dim LASTNAME As String
Dim MidInitial As String
Dim Suffix As String
Dim Pos As Integer
Dim Pos2 As Integer
Dim Pos3 As Integer
Pos = InStr(1, strEnvironUserName, ".", vbTextCompare)
If Pos = 0 Then Pos = Len(strEnvironUserName) + 1
LASTNAME = Trim(Left(strEnvironUserName, Pos - 1))
Pos2 = InStr(1, LASTNAME, " ", vbTextCompare)
If Pos2 Then
Pos3 = InStr(Pos2 + 1, LASTNAME, " ", vbTextCompare)
If Pos3 Then
Suffix = Right(LASTNAME, Len(LASTNAME) - Pos3)
LASTNAME = Left(LASTNAME, Pos3 - 1)
Else
Suffix = Right(LASTNAME, Len(LASTNAME) - Pos2)
LASTNAME = Left(LASTNAME, Pos2 - 1)
End If
End If
Pos2 = InStr(Pos + 2, strEnvironUserName, " ", vbTextCompare)
If Pos2 = 0 Then
Pos2 = Len(strEnvironUserName)
End If
If Pos2 > Pos Then
FIRSTNAME = Mid(strEnvironUserName, Pos + 1, Pos2 - Pos)
MidInitial = Right(strEnvironUserName, Len(strEnvironUserName) - Pos2)
End If
Pos = InStr(1, LASTNAME, "-", vbTextCompare)
If Pos Then
LASTNAME = Trim(StrConv(Left(LASTNAME, Pos), vbProperCase)) & _
Trim(StrConv(Right(LASTNAME, Len(LASTNAME) - Pos), vbProperCase))
Else
LASTNAME = Trim(StrConv(LASTNAME, vbProperCase))
End If
FIRSTNAME = Trim(StrConv(FIRSTNAME, vbProperCase))
MidInitial = Trim(StrConv(MidInitial, vbProperCase))
Suffix = Trim(StrConv(Suffix, vbProperCase))
'
' suffix handling
'
Select Case UCase(Suffix)
Case "JR", "SR", "II", "III", "IV", "MD", "PHD", "PH.D", "M.D."
Case Else
If Not IsNumeric(Left(Suffix, 1)) Then
LASTNAME = LASTNAME & " " & Suffix
Suffix = ""
End If
End Select
ParseOutNamesFromUserName = "Last = " & LASTNAME & "First = " & FIRSTNAME & " MidInitial = " & MidInitial & " Suffix = " & Suffix
End Function
Bookmarks