Capitalize only the first letter in text string

ChrisHT

New Member
Joined
Dec 2, 2008
Messages
2
In Excel 2003, I am referencing a text string and would like to return the text string with only the first letter capitalized. The PROPER function does not do this, as it capitalizes the first letter of every word. The formula below does however perform this, but I would like to write it as a User-Defined Function, similar to the Proper function, so it can be applied to multiple different workbooks:

=UPPER(LEFT(A1,1))&LOWER(RIGHT(A1,LEN(A1)-1))

Is it even possible to write a User-Defined Function to complete this seemingly simple task and, if so, how do I do it?

Thanks,
Chris
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
A UDF would mean you would have to enter a formula into each cell. It would be a little shorter than the above but still the same effort really.

Would you not be better writing a simple sub that does it for each cell in the selected range?

Something like (Freehand so there may be errors)

Code:
Sub CapsFirstLetter()
Dim Cell As Variant
For Each Cell In Selection
    Cell.Formula = UCase(Left(Cell.Text, 1)) & LCase(Right(Cell.Text, Len(Cell.Text) - 1))
Next
End Sub

Save that to your personal workbook, assign it to a button or shortcut key, select your range and hit the button or shortcut key.
 
Upvote 0
UDF
Code:
Function SentenceCase(txt As String) As String
Dim e
For Each e In Split(txt, ".")
    SentenceCase = SentenceCase & ". " & UCase(Left$(Trim(e),1)) & _
        LCase(Mid$(Trim(e),2))
Next
SenteceCase = Mid$(SenteceCase, 3)
End Function
 
Upvote 0
Typo mate:

SenteceCase = Mid$(SenteceCase, 3)

Missing an n in both instances :).
Thanks
Rich (BB code):
Function SentenceCase(txt As String) As String
Dim e
For Each e In Split(txt, ".")
    SentenceCase = SentenceCase & ". " & UCase(Left$(Trim(e),1)) & _
        LCase(Mid$(Trim(e),2))
Next
SentenceCase = Mid$(SentenceCase, 3)
End Function
 
Upvote 0
Blade Hunter - that macro worked like a charm. Thank you! You've made my job so much easier and less tedious. Thanks for the other posts, too. The other suggestions also worked, but the macro was the best for what I need to do.
 
Upvote 0
A UDF would mean you would have to enter a formula into each cell. It would be a little shorter than the above but still the same effort really.

Would you not be better writing a simple sub that does it for each cell in the selected range?

Something like (Freehand so there may be errors)

Code:
Sub CapsFirstLetter()
Dim Cell As Variant
For Each Cell In Selection
    Cell.Formula = UCase(Left(Cell.Text, 1)) & LCase(Right(Cell.Text, Len(Cell.Text) - 1))
Next
End Sub

Save that to your personal workbook, assign it to a button or shortcut key, select your range and hit the button or shortcut key.

Works graeat, but when I used on a table with some empty fields started to stop cos of bad lenght. So I added condition now works even with empty fields

Code:
Sub CapsFirstLetter()

Dim Cell As Variant
For Each Cell In Selection
    If Len(Cell.Text) = 0 Then GoTo 10
    
    Cell.Formula = UCase(Left(Cell.Text, 1)) & LCase(Right(Cell.Text, Len(Cell.Text) - 1))

10

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top