Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of each word using VBA

Tags:

excel

vba

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

like image 290
JayyM Avatar asked Oct 23 '17 09:10

JayyM


People also ask

How do you capitalize first letter in VBA?

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.

How do you capitalize every word in VBA?

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.

How do you auto capitalize the first letter of every word?

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.

How do you make first letter capitalize in Excel automatically?

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.


2 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)

enter image description here

like image 193
Mrig Avatar answered Oct 09 '22 22:10

Mrig


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

like image 21
user10186832 Avatar answered Oct 09 '22 21:10

user10186832