5

How to format words in Excel sheets using VBA such that first letter is capitalized followed by small letters in each word?

3 Answers 3

10

PROPER function should help. See this for details.

Using VBA

Sub Demo()
    Dim ws As Worksheet
    Dim rng As Range, cel As Range
    Dim lastRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet3")  'change Sheet3 to data sheet
    With ws
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For Each cel In .Range("A1:A" & lastRow)
            cel.Offset(0, 1).Value = Application.Proper(cel.Value)
        Next
    End With
End Sub

Using Formula

=PROPER(A1)

enter image description here

4
  • Thanks for your answer @Mrig. I tried applying the Formula, but it results into formatting all the letters into smaller one's...
    – JayyM
    Commented Oct 23, 2017 at 10:30
  • @JayyM - I've tested my solution, see image for reference. Can you share the screen shot of how are you using formula.
    – Mrig
    Commented Oct 23, 2017 at 10:32
  • @JayyM - Instead of Cells(i + 1, 2).Value use Application.Proper (Cells(i + 1, 2).Value)
    – Mrig
    Commented Oct 23, 2017 at 10:39
  • @JayyM - Or, instead of Application.Proper try WorksheetFunction.Proper.
    – Mrig
    Commented Oct 23, 2017 at 10:41
2

This answer is the top Stack Overflow hit on Google for this question:

"vba code to capitalise the first letter of a word"

This code works in Office 365 Excel on Windows 10:

Public Function fnProperCase(sWord As String) As String
    fnProperCase = StrConv(sWord, vbProperCase)
End Function

Here is the Excel with a test suit; row 5 has all three outputs; test data in col 1, =fnProperCase() in col 2 and =PROPER() in col 3

Below are various other answers on Stack Overflow and elsewhere:

StrConv

Capitalise first letter of words without changing currently Capitalised

Making wrong code look wrong

0

This is known as Proper Case. You can use the following line to convert a cell value to proper case:

YourCell.Value = StrConv(YourCell.Value, vbProperCase)
1
  • Thanks for the help @dotNET . My code is like: 'If Cells(i + 1, 1).Value = id Then flag = True NameCity.Controls("TextBoxName").Value = Cells(i + 1, 2).Value NameCity.Controls("TextBoxCity").Value = Cells(i + 1, 3).Value'
    – JayyM
    Commented Oct 23, 2017 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.