Proper Case Function

This neat function capitalizes the first characters of each word setting, and turns all other characters to lowercase.

Public Function ProperCase(strText As String) As String 'Capitalize all first characters of the input string 'and set all the others to lowercase
    On Error GoTo Err_ProperCase
   
    Dim I As Integer, intLen As Integer, strTemp As String, strFinal As String
    Dim isSpace As Boolean
    strTemp = LCase(Trim(strText))
    intLen = Len(Trim(strText))
    strFinal = String(intLen, " ")
    isSpace = True
    For I = 1 To intLen
        If Mid(strTemp, I, 1) = Chr(32) Then
            strFinal = Mid(strFinal, 1, I - 1) & Chr(32) & Mid(strFinal, I + 1)
            isSpace = True
        ElseIf isSpace Then
            strFinal = Mid(strFinal, 1, I - 1) & UCase(Mid(strTemp, I, 1)) & Mid(strFinal, I + 1)
            isSpace = False
        Else
            strFinal = Mid(strFinal, 1, I - 1) & Mid(strTemp, I, 1) & Mid(strFinal, I + 1)
        End If
    Next I
    ProperCase = strFinal
   
Exit_ProperCase:
    Exit Function
   
Err_ProperCase:
    MsgBox Err.Description & vbCrLf & strText, vbInformation, "ProperCase function"
End Function

You might also like...

Comments

 hobbes68

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond