Tuesday, November 11, 2008

StrConv function - to capitalize the first character in the sentence

Reference: Chennai IQ

I wanted to write a function to capitalize the first character of every word. For example, I want to change "cell address" to "Cell Address". Before I started the function, I was thinking perhaps there already has a easy way to do that. Luckily, I did a search in the Google and found this: StrConv

This is the function I've written:
Public Function CapitalizeFirstCharacter(ByVal Value As String) As String
    ' Make the whole string lower case, then only capitalize the first character
    CapitalizeFirstCharacter = StrConv(LCase$(Value), vbProperCase)

End Function

StrConv (String, conversion[, LCID])

String - required. String expression to be converted.
conversion - required. Integer. The sum of values specifying the type of conversion to perform.
LCID - optional. The LocaleID, if different than the system LocaleID. (default: system LocaleID)

conversion argument:
vbUpperCase1Convert to uppercase
vbLowerCase2Convert to lowercase
vbProperCase3Convert the first letter of every word in string to uppercase

There are also vbWide, where values are 4, 8, 16, 32, 64, 128 to convert the string with specific usage. (Please refer to the reference page)

No comments:

Post a Comment