Kayleigh
Member
- Local time
- Today, 13:20
- 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