Need Help To Make First Letter Of All Words In A Textbox To Uppercase

so, i have this code to convert first letter of word to uppercase:

Private Sub txtFields_Change(Index As Integer)

txtFields(Index).Text = UCase$(Left(txtFields(Index).Text, 1)) & LCase$(Mid$(txtFields(Index).Text, 2))
txtFields(Index).SelStart = Len(txtFields(Index).Text)
End Sub

but, this was before i realize that i'm actually working with textboxes where more than two words are entered, i need some help with this code so that first letter of all words in a textbox will become uppercase...I'm using Multiline Option in textbox.

anyone, pls? tnx.

Dani AI

Generated

Quick summary and practical fix (ties to the thread)

was right — the fastest built‑in way is the VB/VBA StrConv proper‑case conversion. StrConv with vbProperCase capitalizes the first letter of every word. (learn.microsoft.com)

Use StrConv (or any formatting) on LostFocus/AfterUpdate instead of the Change event to avoid caret jumping and re-entrancy while the user types. The LostFocus/AfterUpdate style runs when the control loses focus and keeps the typing experience smooth. (learn.microsoft.com)

Why you might not want StrConv alone

StrConv lowercases the rest of each word and can mangle names that contain apostrophes, hyphens, internal mixed case (iPhone), or certain locale/diacritic situations — it’s great for simple titlecasing but not perfect for real names. If you need to preserve interior capitals or handle O'Connor / McDonald / hyphenated names correctly, post‑processing or a custom routine is required. (techonthenet.com)

A compact alternative (preserves interior case and handles multiline)

This routine only uppercases the first letter after common separators (space, CR/LF, hyphen, apostrophe, tabs, etc.) and leaves all other characters alone — so it preserves existing interior capitals and acronyms:

Public Function TitleCasePreserveInner(ByVal s As String) As String
  Dim i As Long, ch As String, out As String
  Dim nextCap As Boolean
  nextCap = True
  For i = 1 To Len(s)
    ch = Mid$(s, i, 1)
    If nextCap Then
      out = out & UCase$(ch)
      nextCap = False
    Else
      out = out & ch
    End If
    Select Case ch
      Case " ", vbTab, vbCr, vbLf, "-", "'", "/", "\", "(", "[", "]", ".", ",", ";", ":"
        nextCap = True
    End Select
  Next i
  TitleCasePreserveInner = out
End Function

Wire it on leave:

Private Sub Text1_LostFocus()
  Text1.Text = TitleCasePreserveInner(Text1.Text)
End Sub

If you must format while typing (Change event), keep a guard and restore cursor position:

Private Sub Text1_Change()
  Static inChange As Boolean
  If inChange Then Exit Sub
  inChange = True
  Dim pos As Long: pos = Text1.SelStart
  Dim s As String: s = TitleCasePreserveInner(Text1.Text)
  If s <> Text1.Text Then Text1.Text = s: Text1.SelStart = pos
  inChange = False
End Sub

Notes and tips

  • ’s Split approach works when words are space‑delimited, but it won’t cover hyphens/newlines without extra logic.
  • For accented letters or language‑specific rules consider StrConv with the LCID parameter and then patch known exceptions (Mc/McDonald, O'... , iPhone, acronyms) with a small replacement list. (learn.microsoft.com)

This approach keeps multiline text intact, preserves existing interior capitals, and avoids the main pitfalls people ran into earlier in this thread.

Recommended Answers

All 5 Replies

hi clarkpoon, try this...

need a command button and a listbox

just change the input string to your data

Private Sub Command1_Click()
Dim s, x, y, z As String
Dim i, n As Integer
Dim spwords() As String

'change s to your input data 
s = "string1 string2 string3 string4 string5 string6 string7"


' Split the string at the space characters.
spwords() = Split(s, " ")

For i = 0 To UBound(spwords)
     
   'get the first character and change to upper case
   x = spwords(i)
   y = UCase(Mid(x, 1, 1))
 
   'get the remaining characters
   n = Len(x)
   z = y & Mid(x, 2, n - 1)
   
  List1.AddItem z
   
Next

End Sub

How about one line...

Option Explicit

Private Sub Form_Load()
Dim S As String
S = "string1 string2 string3 string4 string5 string6 string7"

S = StrConv(S, vbProperCase)
Debug.Print S

End Sub

Good Luck

cguan_77

I'm using only one Textbox with a Multiline optione enabled. thats it...and both are not working or can solve it ;)

I'm using

Text1

in form.


Thanks for fast reply!!!

Are you using .NET? if so you are in wrong forum...
but this works for me

Option Explicit

Private Sub Form_Load()
Dim S As String
S = "string1" & vbNewLine & "string2" & vbNewLine & "string3" & vbNewLine & "string4" & vbNewLine
S = S & "string5" & vbNewLine & "string6" & vbNewLine & "string7" & vbNewLine & "string8"
Text1.Text = S
End Sub

Private Sub Command1_Click()
Text1.Text = StrConv(Text1.Text, vbProperCase)
End Sub

Its working Now :)
Very Very thanks bro :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.