![]() |
#1
|
|||
|
|||
![]()
HI,
I saw a function created by Paul Edstein / macropod few years ago which I cannot make it to work in my case. What I need: if an word is containing any capital letter then that word to start with a Capital letter. If the word is written in lowercases had to be left as it is ThIs TeXT has to bE transformed wITh CaPITAL LETTERS into This Text has to Be transformed With Capital Letters I spent all day long but I could not find proper solution ![]() Code:
Function ProperCase(StrTxt As String, Optional Caps As Long, Optional Excl As Long) As String 'Convert an input string to proper-case. 'Surnames like O', Mc and hyphenated names are converted to proper case also. 'If Caps = 0, then upper-case strings like ABC are preserved; otherwise they're converted. 'If Excl = 0, selected words are retained as lower-case, except when they follow specified punctuation marks. Dim i As Long, j As Long, k As Long, l As Long, bChngFlg As Boolean Dim StrTmpA As String, StrTmpB As String, StrExcl As String, StrPunct As String, StrChr As String StrExcl = " a , an , and , as , at , but , by , for , from , if , in , is , of , on , or , the , this , to , with " StrPunct = "!,:,.,?,""" If Excl <> 0 Then StrExcl = "" StrPunct = "" End If If Len(Trim(StrTxt)) = 0 Then ProperCase = StrTxt Exit Function End If If Caps <> 0 Then StrTxt = LCase(StrTxt) StrTxt = " " & StrTxt & " " For i = 1 To UBound(Split(StrTxt, " ")) StrTmpA = " " & Split(StrTxt, " ")(i) & " " StrTmpB = UCase(Left(StrTmpA, 2)) & Right(StrTmpA, Len(StrTmpA) - 2) StrTxt = Replace(StrTxt, StrTmpA, StrTmpB) Next i StrTxt = Trim(StrTxt) 'Code for handling O' names For i = 1 To UBound(Split(StrTxt, "'")) ' If InStr(Right(Split(StrTxt, "'")(i - 1), 2), " ") = 1 Then If InStr(Right(Split(StrTxt, "'")(i - 1), 2), " ") = 1 Or _ Right(Split(StrTxt, "'")(i - 1), 2) = Right(Split(StrTxt, "'")(i - 1), 1) Then StrTmpA = Split(StrTxt, "'")(i) StrTmpB = UCase(Left(StrTmpA, 1)) & Right(StrTmpA, Len(StrTmpA) - 1) StrTxt = Replace(StrTxt, StrTmpA, StrTmpB) End If Next 'Code for handling hyphenated names For i = 1 To UBound(Split(StrTxt, "-")) StrTmpA = Split(StrTxt, "-")(i) StrTmpB = UCase(Left(StrTmpA, 1)) & Right(StrTmpA, Len(StrTmpA) - 1) StrTxt = Replace(StrTxt, StrTmpA, StrTmpB) Next 'Code for handling names starting with Mc If Left(StrTxt, 2) = "Mc" Then Mid(StrTxt, 3, 1) = UCase(Mid(StrTxt, 3, 1)) End If i = InStr(StrTxt, " Mc") If i > 0 Then Mid(StrTxt, i + 3, 1) = UCase(Mid(StrTxt, i + 3, 1)) End If 'Code for handling names starting with Mac If Left(StrTxt, 3) = "Mac" Then If Len(Split(Trim(StrTxt), " ")(0)) > 5 Then Mid(StrTxt, 4, 1) = UCase(Mid(StrTxt, 4, 1)) End If End If i = InStr(StrTxt, " Mac") If i > 0 Then If Len(StrTxt) > i + 5 Then Mid(StrTxt, i + 4, 1) = UCase(Mid(StrTxt, i + 4, 1)) End If End If 'Code to restore excluded words to lower case For i = 0 To UBound(Split(StrExcl, ",")) StrTmpA = Split(StrExcl, ",")(i) StrTmpB = UCase(Left(StrTmpA, 2)) & Right(StrTmpA, Len(StrTmpA) - 2) If InStr(StrTxt, StrTmpB) > 0 Then StrTxt = Replace(StrTxt, StrTmpB, StrTmpA) 'Make sure an excluded words following punctution marks are given proper case anyway For j = 0 To UBound(Split(StrPunct, ",")) StrChr = Split(StrPunct, ",")(j) StrTxt = Replace(StrTxt, StrChr & StrTmpA, StrChr & StrTmpB) Next End If Next ProperCase = StrTxt End Function Last edited by macropod; 05-08-2016 at 04:03 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
![]()
The function you posted appears to be an edited variant of code I've posted in threads such as:
https://www.msofficeforums.com/word/24807-how-do-i-convert-line-text-title.html https://www.msofficeforums.com/word-...html#post55885 It is not deigned to do as you say you want it to and wouldn't even do as it's supposed to with your modifications. For what you say you want, you could use a macro like: Code:
Sub Reformat() Dim i As Long, StrChr As String, StrTmp As String With Selection For i = 1 To .Words.Count StrChr = Left(.Words(i), 1) If StrChr = LCase(StrChr) Then StrTmp = StrTmp & LCase(.Words(i)) Else StrTmp = StrTmp & StrChr & LCase(Mid(.Words(i), 2, Len(.Words(i)) - 1)) End If Next .Text = StrTmp End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
thank you.
ok, I will use # ... now this started to give me some results. my text: thIs TeXT has to bE transformed wITh CaPITAL LETTERS became: this Text has to be transformed with Capital Letters and it should be: This Text has to Be transformed With Capital Letters This, Be and With are not having the Capital letter. |
#4
|
||||
|
||||
![]()
How do you expect the macro to know when a word that begins with a lower-case letter should actually start with an upper-case letter???
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
That was the problem I could not solve: one way theoretically could be that it checks first if the word is lowercase and if yes it skips it. If there are some capital letters in it then it should put it with capital letter. This is the rule I need but I do not know how to do it.
thank you for your patience with me ![]() this function I guess it does check if the word is lowercase? Code:
Function AllLowerCase(stringToCheck As String) As Boolean AllLowerCase = StrComp(stringToCheck, LCase(stringToCheck), _ vbBinaryCompare) = 0 End Function |
#6
|
||||
|
||||
![]()
In that case, try:
Code:
Sub Reformat() Dim i As Long, StrTmp As String With Selection For i = 1 To .Words.Count If .Words(i) = LCase(.Words(i)) Then StrTmp = StrTmp & .Words(i) Else StrTmp = StrTmp & UCase(Left(.Words(i), 1)) & LCase(Mid(.Words(i), 2, Len(.Words(i)) - 1)) End If Next .Text = StrTmp End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
That is doing the right thing! Thank you Paul. After spending days trying to figure out I can go further with my project. That text I have to insert in a different program that has to have the words configured like that....
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
IanM_01 | Word | 5 | 11-21-2015 02:29 AM |
IF Function | Azleenda | Excel | 2 | 11-10-2015 10:06 PM |
![]() |
lcaretto | Excel Programming | 2 | 05-26-2014 07:19 PM |
Creating a graph for Future Value function (FV function) | bmoody | Excel | 2 | 11-06-2013 10:52 AM |
if function help | jim831 | Excel | 2 | 10-29-2010 07:06 PM |