Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 07-03-2009, 05:25 PM
foseco foseco is offline
Registered User
 
Join Date: 08 Jun 2009
Location: Birmingham, England
MS Office Version:Excel 2007
Posts: 12
foseco is becoming part of the community
Proper case textbox with returns.

Please Register to Remove these Ads

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
Reply With Quote
  #2  
Old 07-03-2009, 07:33 PM
Leith Ross's Avatar
Leith Ross Leith Ross is online now
Forum Moderator
 
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,517
Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding
Send a message via AIM to Leith Ross
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 scales above each post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.


Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Reply With Quote
  #3  
Old 07-04-2009, 08:55 AM
foseco foseco is offline
Registered User
 
Join Date: 08 Jun 2009
Location: Birmingham, England
MS Office Version:Excel 2007
Posts: 12
foseco is becoming part of the community
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
File Type: xlsm Shopping List.xlsm (102.4 KB, 3 views)
Reply With Quote
  #4  
Old 07-04-2009, 09:28 AM
foseco foseco is offline
Registered User
 
Join Date: 08 Jun 2009
Location: Birmingham, England
MS Office Version:Excel 2007
Posts: 12
foseco is becoming part of the community
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.
Reply With Quote
  #5  
Old 07-04-2009, 03:52 PM
Leith Ross's Avatar
Leith Ross Leith Ross is online now
Forum Moderator
 
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,517
Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding
Send a message via AIM to Leith Ross
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 scales above each post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.


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


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump