Capital letter of string

I am trying to extract the first line of a sent string into an arr array. The part of the code that does not work is the Right function, which causes the code to return an error. What could be a fix?

 For Each sent In arr sent = UCase(Left(sent, 1)) & Right(sent, Len(sent) - 1) arr(i) = sent i = i + 1 Next 
+6
source share
4 answers

It would be easier to use the Mid function to build everything after a capital letter. This function does not require specifying the full length:

 For i = LBound(arr) To UBound(arr) sent = arr(i) ' Capitalize the first letter, then append everything else. sent = UCase(Left(sent, 1)) & Mid(sent, 2) arr(i) = sent Next 

Alternatively, you can simply arr over arr with i as your enumerator. Mixing and matching indices using a separate counter can cause problems.

+8
source

You can simply use the StrConv() function to do this. eg

 For i = LBound(arr) To UBound(arr) sent = arr(i) arr(i) = StrConv(sent, vbProperCase) Next 

or without loop:

 arr = Split(StrConv(Join$(arr, " "), vbProperCase), " ") 
+13
source

The difference between using strConv with vbPropercase and a solution with UCase(left(xx,1)) & mid(xx,2) is that vbPropercase changes all first characters to capitals and all the rest to lower case. This is not always what you want, sometimes you just want the first as uppercase and the rest as lowercase.

Then you can use a slightly improved solution:

 UCase(Left(<string>,1)) & LCase(Mid(<string>,2)) 
+4
source

Try the following:

 NewArr As List<string> NewArr = new List<string>() For Each sent As String In arr NewArr.Add(Application.WorksheetFunction.Proper(sent)) Next sent arr = NewArr.ToArray() 
-1
source

Source: https://habr.com/ru/post/980204/


All Articles