How to format words in Excel sheets using VBA such that first letter is capitalized followed by small letters in each word?
3 Answers
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)
-
Thanks for your answer @Mrig. I tried applying the Formula, but it results into formatting all the letters into smaller one's...– JayyMCommented 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.– MrigCommented Oct 23, 2017 at 10:32
-
@JayyM - Instead of
Cells(i + 1, 2).Value
useApplication.Proper (Cells(i + 1, 2).Value)
– MrigCommented Oct 23, 2017 at 10:39 -
@JayyM - Or, instead of
Application.Proper
tryWorksheetFunction.Proper
.– MrigCommented Oct 23, 2017 at 10:41
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:
Capitalise first letter of words without changing currently Capitalised
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)
-
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'– JayyMCommented Oct 23, 2017 at 10:26