Solved

VBA - Capitalise first letter of each word

Posted on 2011-09-07
16
685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

737 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