by Matt Dittman (1 Submission)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 7th February 2001
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
This function properly capitalizes last names, even hyphenated ones.
Dim NumLetters As Integer
Dim SearchChar As String
Dim SearchFlag As Boolean
Dim CurrChar As String
Dim Index As Long
'Capitalize after spaces...
strText = Trim(StrConv(strText, vbProperCase))
SearchChar = "-" ' search character (hyphen)
'remove extra hyphens
strText = RemoveDuplicates(strText, "-")
'remove extra spaces
strText = RemoveDuplicates(strText)
NumLetters = Len(strText)
Index = 1
'Capitalize after hyphens (SearchChar)...
Do While Index < NumLetters
CurrChar = Mid(strText, Index, 1)
'Now searching for embedded spaces and letters to make caps
If SearchFlag Then
'split string in two...
Dim FirstPart As String, LastPart As String
FirstPart = Left(strText, Index - 2)
LastPart = StrConv(Right(strText, (NumLetters - Index + 1)), vbProperCase)
'rejoin string
strText = FirstPart & SearchChar & LastPart
End If
If CurrChar = SearchChar Then
SearchFlag = True
Else
SearchFlag = False
End If
Index = Index + 1
Loop
LastNameCaps = strText
End Function
Public Function RemoveDuplicates(ByVal strText As String, _
Optional strChar As String = " ") As String
Dim strDblChar As String
Dim strTemp As String
Dim flag As Boolean
strDblChar = strChar & strChar
Do While InStr(1, strText, strDblChar, vbTextCompare) > 0
If InStr(1, strText, strDblChar, vbTextCompare) > 0 Then
strText = Replace(strText, strDblChar, strChar)
End If
Loop
RemoveDuplicates = strText
End Function
No comments have been posted about This function properly capitalizes last names, even hyphenated ones.. Why not be the first to post a comment about This function properly capitalizes last names, even hyphenated ones..