Solved

VBA - Capitalise first letter of each word

Posted on 2011-09-07
16
675 Views
Last Modified: 2012-06-27
Hi,

I have need to ca capitalise the first letter of each word with a string.  I’ve had some success using VBProper however this changes the entire string.

For example this string ‘test data for a WORD’

Becomes ‘Test Data For A Word’

I need a function which does not convert any other letter than the first.  So I need a function that word produce the following string:

Test Data For A WORD

Any suggestions?
0
Comment
Question by:andyw27
  • 8
  • 3
  • 3
  • +1
16 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36494391
strconv("" & strYourString, vbProperCase )
0
 

Author Comment

by:andyw27
ID: 36494430
Thanks, already tried that way.  Unfortunately I need to preserve words that are all upper case.  
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36494450
Okay- understood now.  This will do the trick:

Function UCaseFirstLetter(s As String) As String
    Dim arr() As String
    Dim strTemp As String
    Dim I As Integer
    
    arr = Split(s, " ")
    For I = 0 To UBound(arr)
        arr(I) = UCase(Left(arr(I), 1)) & Right(arr(I), Len(arr(I)) - 1)
        strTemp = strTemp & " " & arr(I)
    Next

    UCaseFirstLetter = Trim(strTemp)
    
End Function

Open in new window


You can test it like this:

debug.print Ucasefirstletter("this is a TEST")

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:andyw27
ID: 36494517
Perfect thanks.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36494578
Glad to help out.
0
 
LVL 10

Expert Comment

by:plummet
ID: 36494865
Or even simpler:

function UpperFirstLetter(sInput as string) as string

  if len(sInput)>0 then
    UpperFirstLetter=ucase$(left$(sInput,1)) & mid$(sInput,2)
  end if

end function

Open in new window


0
 
LVL 61

Expert Comment

by:mbizup
ID: 36494921
That doesn't quite do it (it only works for one word).  

<Capitalise first letter of each word>

The request here is to capitalize the first letter in each word of a string without affecting the other letters in those words.  
0
 
LVL 10

Expert Comment

by:plummet
ID: 36495010
Oh yeah. I should have read the question properly
0
 
LVL 45

Expert Comment

by:aikimark
ID: 36499328
Starting with mbizup's routine:

Function UCaseFirstLetter(parmString As String) As String
    Dim arr() As String
    Dim I As Integer
    
    arr = Split(parmString, " ")
    For I = 0 To UBound(arr)
        If arr(I) = UCase(arr(I)) Then
        Else
            arr(I) = StrConv(arr(I), vbProperCase)
        End If
    Next

    UCaseFirstLetter = Join(arr)
    
End Function

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36499592
aikimark,

That takes away the functionality that made the first letter of each word uppercase.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 36499668
@mbizup

It seems to get the (stated) desired results.  Unless the entire word is capitalized, then it applies a proper case transformation to the word.

?UCaseFirstLetter("test data for a WORD")
Test Data For A WORD

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36499981
Strange.  This is what I was getting:

? ucasefirstletter("this is my NAME")
this is my NAME
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36500016
Which makes sense...
You must have some setting in place for case sensitive comparisons.  

By default, string comparisons are not case sensitive, so this condition will always be true:

If arr(I) = UCase(arr(I)) Then

Open in new window


But you also need to account for cases like mcDonald, which should become McDonald (ie: only the first letter changes)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 36500209
This version will force a case-sensitive comparison.

Function UCaseFirstLetter(parmString As String) As String
    Dim arr() As String
    Dim I As Integer
    
    arr = Split(parmString, " ")
    For I = 0 To UBound(arr)
        If StrComp(arr(I), UCase(arr(I)), vbBinaryCompare) = 0 Then
        Else
            arr(I) = StrConv(arr(I), vbProperCase)
        End If
    Next

    UCaseFirstLetter = Join(arr)
    
End Function

Open in new window


I should have thought about this since I wrote an article about this very subject.
http:/A_1897-The-Case-for-case-sensitive-Modules.html
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36500252
You're still going to run into issues with words/names with capitals inside the string, like 'mcDonald'

<I need a function which does not convert any other letter than the first.>

? UCaseFirstLetter ("old mcDonald had a FARM")
Old Mcdonald Had A FARM  '<-- the capital D gets converted to lower case
0
 

Author Closing Comment

by:andyw27
ID: 36505648
Good answer.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question