Home > How-To Library > String Manipulation

How To Capitalize The First Character Of Each Word In A String/Phrase.

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
'Example Usage: 'Dim strTest As String 'strTest = "microsoft access 97" ' 'MakeInitialCaps strTest 'Debug.Print strTest 'Displays: "Microsoft Access 97" Public Function MakeInitialCaps(ByRef strArg As String) Const strSpace As String = " " Dim ID As Long 'Remove Leading and Trailing Spaces strArg = Trim$(strArg) 'Capitalize first letter in String strArg = UCase(Left(strArg, 1)) & Mid(strArg, 2) 'Loop through string looking for spaces and 'Capitalizing the next Character ID = 1 'Set index into string Do ID = InStr(ID, strArg, strSpace, _ vbBinaryCompare) If ID > 0 Then strArg = Left$(strArg, ID) & UCase$(Left$(Right$(strArg, Len(strArg) - ID), 1)) & Mid$(strArg, ID + 2) ID = ID + 1 Else Exit Do End If Loop End Function

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.