Userform text box question

paulsolar

Well-known Member
Joined
Aug 21, 2013
Messages
689
Office Version
  1. 365
Hi All

I have a question, I'm not sure if it is possible. If it is I'll have a go at doing it.

Is it possible to capitalize the first letter of of each row in a Userform text box?

I have a text box where the user can enter multiple lines and my pet hate is when the users do not keep to the correct capitalization. Proper case wouldn't work as every word would be capitalized.

Is this possible

kind regards

Paul
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The following code uses the BeforeUpdate event handler. Change the name of the textbox accordingly.

VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    Dim txt As String
    txt = Me.TextBox1.Value
    
    Dim arr() As String
    arr() = Split(txt, vbCrLf)
    
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        Dim tmp As String
        tmp = arr(i)
        If Len(tmp) > 0 Then
            Mid(tmp, 1, 1) = UCase(Mid(tmp, 1, 1))
            arr(i) = tmp
        End If
    Next i
    
    Me.TextBox1.Value = VBA.Join(arr, vbCrLf)
    
End Sub

Hope this helps!
 
Upvote 0
Hi Domenic

worked like a charm, brilliant thanks. I managed to get the very first letter to go to a capital and tried everything and couldnt improve on it.

Look out Fluff, you have a hard act to follow after this (only joking, hope you are keeping well)

thanks again

cheers

Paul
 
Upvote 0
You might also be interested in the code below ...

VBA Code:
Public Function ProperCaseOnSentence(ByRef argText As String) As String

    Const DELIMS As String = ".?!" & vbLf

    If Len(argText) = 1 Then
        ProperCaseOnSentence = UCase(argText)
    ElseIf Len(argText) > 1 Then
        ProperCaseOnSentence = CapitalizeAfter(argText, DELIMS)
    End If
End Function

Private Function CapitalizeAfter(ByVal argText As String, ByVal argDelims As String) As String
    Dim arr As Variant, delim As String, i As Long, n As Long
    CapitalizeAfter = argText
    For i = 1 To Len(argDelims)
        delim = VBA.Mid$(argDelims, i, 1)
        If InStr(CapitalizeAfter, delim) > 0 Then
            arr = Split(CapitalizeAfter, delim)
            For n = LBound(arr) To UBound(arr)
                arr(n) = Capitalize(arr(n))
            Next n
            CapitalizeAfter = Join(arr, delim)
        Else
            CapitalizeAfter = Capitalize(CapitalizeAfter)
        End If
    Next i
End Function

Private Function Capitalize(ByVal argText As String) As String
    Dim i As Long, chr As String
    If Len(argText) > 0 Then
        Capitalize = argText
        For i = 1 To Len(Capitalize)
            chr = VBA.Mid$(Capitalize, i, 1)
            If chr Like "[A-ž]" Then
                Mid$(Capitalize, i, 1) = UCase(chr)
                Exit For
            End If
        Next i
    End If
End Function


Usage example:

VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Me.TextBox1.Value = ProperCaseOnSentence(Me.TextBox1.Value)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,745
Messages
6,126,634
Members
449,323
Latest member
Smarti1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top