Capitalize first letter of first and second word in a text box in a user form

RaymondB
NewLounger
Posts: 9
Joined: 15 Dec 2020, 13:23

Capitalize first letter of first and second word in a text box in a user form

Post by RaymondB »

Good morning (South African time). This problem defeats my limited knowledge of VBA,

I have a text box "TextBoxSection". I would like to force the first and second word to be capitalized and the rest to be lower case.

For example: "section: this IS a Test" must be changed to "Section: This is a test".

Any help will, as always, be greatly appreciated.

Raymond Basson

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Capitalize first letter of first and second word in a text box in a user form

Post by HansV »

In the userform's code module:

Code: Select all

Private Sub TextBoxSection_AfterUpdate()
    Dim arr() As String
    Dim i As Long
    arr = Split(Me.TextBoxSection)
    For i = 0 To UBound(arr)
        Select Case i
            Case 0, 1
                arr(i) = StrConv(arr(i), vbProperCase)
            Case Else
                arr(i) = StrConv(arr(i), vbLowerCase)
        End Select
    Next i
    Me.TextBoxSection = Join(arr)
End Sub
Best wishes,
Hans

RaymondB
NewLounger
Posts: 9
Joined: 15 Dec 2020, 13:23

Re: Capitalize first letter of first and second word in a text box in a user form

Post by RaymondB »

Thank you Hans, it works great.

Raymond