How to format words in Excel sheets using VBA such that first letter is capitalized followed by small letters in each word?
LOWER(A2) – This converts the entire text into lower case. UPPER(LEFT(A2,1) – This converts the first letter of the text string in the cell into the upper case.
In Excel worksheet, the UPPER function converts all the lowercase characters of a text string into uppercase. There is a similar function in that also does the same – the UCase function. The VBA UCase function takes a string as the input and converts all the lower case characters into upper case.
To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.
In cell B2, type =PROPER(A2), then press Enter. This formula converts the name in cell A2 from uppercase to proper case. To convert the text to lowercase, type =LOWER(A2) instead. Use =UPPER(A2) in cases where you need to convert text to uppercase, replacing A2 with the appropriate cell reference.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With