16

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

For Each sent In arr
    sent = UCase(Left(sent, 1)) & Right(sent, Len(sent) - 1)
    arr(i) = sent
    i = i + 1
Next
Community
  • 1
  • 1
brietsparks
  • 4,776
  • 8
  • 35
  • 69

6 Answers6

28

You can just use the StrConv() function for this. e.g. :

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

or without a loop:

arr = Split(StrConv(Join$(arr, " "), vbProperCase), " ")
SierraOscar
  • 17,507
  • 6
  • 40
  • 68
15

It would be easier to use the Mid function for building everything after the letter capitalization. This function doesn't require that you specify 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

Also, you can just iterate arr using i as your enumerator. Mixing and matching index updates using a separate enumerator can lead to trouble.

Jason Faulkner
  • 6,378
  • 2
  • 28
  • 33
6

The difference between using strConv with vbPropercase and the the solution with UCase(left(xx,1)) & mid(xx,2) is that vbPropercase changes all first characters to capitals and all others to lowercase. That Is Not Always What You Want, sometimes you just want the first as an uppercase and the rest as a lowercase.

You then can use a slightly improved solution:

UCase(Left(<string>,1)) & LCase(Mid(<string>,2))
greuze
  • 4,250
  • 5
  • 43
  • 62
Arthur
  • 69
  • 1
  • 1
1

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()
WonderWorker
  • 8,539
  • 4
  • 63
  • 74
Ricardo
  • 29
  • 1
  • 3
    Please add more than code to your answers explaining what your code does and why it will fix the problem the OP is having. It will help for future readers to understand also. Please see [answer] for more guidance. – Bugs May 10 '17 at 14:57
  • Also this question is VBA, not VB.NET – SierraOscar Feb 25 '19 at 11:58
0

In my case, the best solution was similar do Mid: =Replace(string, Left(string, 1), UCase(Left(string, 1)), , 1)

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31241398) – Ike Mar 08 '22 at 21:19
0
StrConv(myString, vbProperCase)