Kayleigh
Member
- Local time
- Today, 20:44
- Joined
- Sep 24, 2020
- Messages
- 709
Public Function GetInitials(ByVal pString As Variant) As String
Dim var As Variant
Dim v As Variant
Dim sValue As String
If IsNull(pString) Then Exit Function
var = Split(pString, " ")
For Each v In var
If Len(Trim$(v)) > 0 Then
sValue = sValue & UCase(Left$(v, 1))
End If
Next
GetInitials = sValue
End Function
What does picking out the last word have to do with getting the initials?
Plus decide what to do when there are more/less than 2 values.
So obviously there can be many combinations. But I am targetting at strings usually with two/three words so function should find last word and put at beginning of string then a comma and other words in string is put after it. I am not concerned about title of name - surname is most important. (If less than 2 values function not necessary.)
Public Function reorderText(ByVal textString As Variant) As String
Dim var As Variant
Dim firstVal As String
Dim lastVal As String
If (IsNull(textString)) Or (Len(textString) < 2) Then Exit Function
var = Split(textString, " ")
firstVal = UBound(var, 1)
lastVal = LBound(var, 1)
reorderText = Trim(lastVal) & ", " & Trim(firstVal)
End Function
Public Function reorderText(ByVal textString As Variant) As String
Dim var As Variant
Dim firstVal As String
Dim lastVal As String
Dim aVals() As String
Dim I As Integer
If Not IsNull(textString) Then
aVals = Split(textString, " ")
reorderText = aVals(UBound(aVals)) & ", "
For I = 0 To UBound(aVals) - 1
reorderText = reorderText & aVals(I) & " "
Next I
reorderText = Trim(reorderText)
End If
End Function
Public Sub test()
Debug.Print reorderText(Null)
Debug.Print reorderText("Madonna")
Debug.Print reorderText("Mr Jones")
Debug.Print reorderText("Mr. John Smith")
Debug.Print reorderText("John Michael Smith")
End Sub
Madonna,
Jones, Mr
Smith, Mr. John
Smith, John Michael
Public Function reorderText(ByVal textString As Variant) As String
Dim var As Variant, i As Integer
textString = textString & vbNullString
reorderText = textString
If Len(textString) < 1 Then Exit Function
Do Until InStr(1, textString, " ") = 0
textString = Replace$(textString, " ", " ")
Loop
var = Split(textString, " ")
Select Case UBound(var)
Case Is > 1
If HaveJr(var(UBound(var))) Then
textString = var(UBound(var) - 1) & " " & var(UBound(var)) & ", "
For i = 0 To UBound(var) - 2
textString = textString & var(i) & " "
Next
Else
textString = var(UBound(var)) & ", "
For i = 0 To UBound(var) - 1
textString = textString & var(i) & " "
Next
End If
textString = Trim$(textString)
Case Is = 1
textString = var(1) & ", " & var(0)
End Select
reorderText = textString
End Function
Public Function HaveJr(ByVal p As String) As Boolean
Const t As String = "/jr/sr/i/ii/iii/iv/v/vi/vii/viii/ix/x/xi/xii/xii/xiv/xv/"
If Len(p) < 1 Then Exit Function
p = Replace$(p, ".", "")
HaveJr = t Like "*/" & p & "/*"
End Function