The Excel VBA UCase Function
VBA Ucase function is a built-in function used to convert text to uppercase. It takes strings as input and converts each letter of the string to uppercase.
Syntax
Ucase(String)
The String can be a text string or a variable that contains a text string or a range containing text strings.
How to Launch VBA Editor in Excel
- Go to the Developer tab and click Visual Basic. You can also open it by pressing Alt+F11.
- Go to the Insert tab and click Module to open the code Module.
Example 1 – Apply Automatic Uppercase to a Selected Range in Excel VBA
To capitalize all the letters in column B:
- Select B5:B12.
- Go to the VBA code Module and enter the following code.
Sub UpperCaseRange()
Dim myRange As Range
Set myRange = Selection
For Each myCell In myRange
myCell.Value = UCase(myCell)
Next myCell
End Sub
Code Breakdown
For Each myCell In myRange
myCell.Value = UCase(myCell)
Next myCell
starts a loop that iterates through each cell in the selected range.
converts each cell string into uppercase.
- Press F5 to run the code.
This is the output.
Example 2 – Apply Automatic Uppercase When a Cell Value Changes in Excel VBA
Use this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("C:C")
If Not Intersect(Target, rng) Is Nothing Then
Application.EnableEvents = False
Target.Value = UCase(Target.Value)
Application.EnableEvents = True
End If
End Sub
Code Breakdown
Private Sub Worksheet_Change(ByVal Target As Range)
This event is triggered when any cell in the worksheet is changed.
If Not Intersect(Target, rng) Is Nothing Then
Application.EnableEvents = False
Target.Value = UCase(Target.Value)
Application.EnableEvents = True
End If
The code checks if the Target range intersects with the rng range.
If it does, it converts the values of cells in the Target range to uppercase.
- Enter data in column C .
- Press Enter and the text will turn into uppercase.
Read More: How to Use VBA in Excel to Capitalize All Letters
Using Excel VBA to Capitalize the First Letter
- Use the Proper function in VBA to capitalize the first letter of each word (Propercase). Enter the following VBA code:
Sub VBACapitalizeFirstLetter()
Dim selectedRange As Range
Set selectedRange = Application.Selection
Set selectedRange = Application.InputBox("Select Range", _
"Capitalize Each Word", selectedRange.Address, Type:=8)
Dim cell As Range
For Each cell In selectedRange
cell.Value = Application.WorksheetFunction.Proper(cell.Value)
Next cell
End Sub
Code Breakdown
For Each cell In selectedRange
cell.Value = Application.WorksheetFunction.Proper(cell.Value)
Next cell
converts each cell of the selected range into the proper case.
- Run the code by pressing F5.
- In the InputBox, select the range to convert.
- Click OK.
First letters are capitalized.
Read More: Excel VBA to Capitalize First Letter of Each Word
Things to Remember
- Use the Ucase function for lowercase text strings only.
Download Practice Workbook
Download the practice workbook.
Related Articles
- How to Stop Auto Capitalization in Excel
- How to Change Case in Excel Without a Formula
- How to Change to Title Case in Excel
<< Go Back to Change Case | Text Formatting | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!