Forum Statistics
- Forum Members:
- Total Threads:
- Total Posts: 5
There are 1 users currently browsing forums.
|
 |

07-03-2009, 05:25 PM
|
|
Registered User
|
|
Join Date: 08 Jun 2009
Location: Birmingham, England
MS Office Version:Excel 2007
Posts: 12
|
|
|
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
|

07-03-2009, 07:33 PM
|
 |
Forum Moderator
|
|
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,517
|
|
|
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!)
|

07-04-2009, 08:55 AM
|
|
Registered User
|
|
Join Date: 08 Jun 2009
Location: Birmingham, England
MS Office Version: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
|

07-04-2009, 09:28 AM
|
|
Registered User
|
|
Join Date: 08 Jun 2009
Location: Birmingham, England
MS Office Version: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.
|

07-04-2009, 03:52 PM
|
 |
Forum Moderator
|
|
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,517
|
|
|
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!)
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|