+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    06-08-2009
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Proper case textbox with returns.

    Hi. I have a spreadsheet using a textbox to enter notes into a cell from a user. I would like the text to be formatted in the correct case for sentences taking into account multiple line/carriage returns in the textbox. I have the following code I found on another website which seems to work on the first line but will not captalize the following lines.

    Code:
    Private Sub CommandButton1_Click()
      Dim myStr As String, m As Object, myPatn As String
      myStr = TextBox1.Value
      With CreateObject("VBScript.RegExp")
        .Pattern = "(^|[!\.\?](\s+)?)\S"
        .Global = True
        If .test(myStr) Then
          For Each m In .Execute(myStr)
            myStr = WorksheetFunction.Replace(myStr, m.FirstIndex + 1, m.Length, StrConv(m.Value, 3))
          Next
        End If
        myPtn = "(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|jun(e)?|jul(y)?|aug(st)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?)"
        .Pattern = "\b(" & myPtn & "|(sun|mon|tue(s)?|wed(nes)?|thu(rs)?|fri|sat(ur)?)(day)?)/b" '<- Weekday/Month  names
        .Global = True
        .IgnoreCase = True
        If .test(myStr) Then
          For Each m In .Execute(myStr)
            myStr = WorksheetFunction.Replace(myStr, m.FirstIndex + 1, m.Length, StrConv(m.Value, 3))
          Next
        End If
        TextBox1.Value = myStr
      End With
      Sheets("List Items").Range("H1") = TextBox1.Value
      End
    End Sub
    Would anyone have any ideas on how this can be altered to incorporate carriage returns?

    Thanks for any help.

    Foseco

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,485

    Re: Proper case textbox with returns.

    Hello foseco,

    My guess is you don't need all the pattern recognition in this script for what you are doing. It would be easier to provide you with an answer if you tell us more about what needs to be capitalized. Posting your workbook would be a good idea.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    06-08-2009
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Proper case textbox with returns.

    Thanks for that. I have attached a copy of the spreadsheet. Its an automated shopping list for my Mother. The notes are used to add sundry items that she doesn't want on the main lists. I just want it to capitalize the first letter of each line from the notes she enters so it's tidier when it's printed. I think you're right about the pattern, she won't be entering thing like proper names or months or days of the week. I did notice one thing. The routine works if we add a full stop to the end of each line but trying to get my Mother to remember to do that would be like trying to knit fog. If you click the Notes button, you will see some items we have already added.

    Thanks

    Foseco
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    06-08-2009
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Proper case textbox with returns.

    Actually, I'm lying. I tried it again and it only capitalizes the first line no matter what is at the end.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,485

    Re: Proper case textbox with returns.

    Hello foseco,

    This macro will capitalize the first word of each sentence. I could not write this for your workbook because I don't have Excel 2007. As far as I know, all the code I used is compatible with 2007. Copy this code into a VBA module.
    Code:
    Function CapitalizeFirstWord(ByVal S1 As String) As String
    
      Dim RE As Object
      Dim S As Variant
      Dim S2 As String
        
        Set RE = CreateObject("VBScript.RegExp")
        RE.Pattern = "^[a-z]"
            
          For Each S In Split(S1, vbLf)
            If RE.Test(S) Then
               S2 = S2 & RE.Replace(S, UCase(Left(S, 1))) & vbLf
            End If
          Next S
          
       Set RE = Nothing
       CapitalizeFirstWord = S2
       
    End Function
    Example of Using the Function
    Code:
      'Change the name of the TextBox to the name you will be using
        TextBox1.Value = CapitalizeFirstWord(TextBox1.Value)
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0