Applying Automatic Uppercase in Excel VBA – 2 Examples

 

Introduction to 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.

1. Go to Microsoft Visual Basic Application

Go to the Insert tab and click Module to open the code Module.

Insert a code Module in VBA


 

Example 1 – Apply Automatic Uppercase to a Selected Range in Excel VBA

To capitalize all the letters  in column B:

  • Select B5:B12.

dataset for Automatic Uppercase for a Selected Range in Excel VBA

  • 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 for Automatic Uppercase for a Selected Range in Excel VBA

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.

Automatic Uppercase for a Selected Range in Excel VBA


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

Automatic Uppercase in Excel VBA for Cell Value Changes

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

VBA Capitalize First Letter

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.

VBA Capitalize First Letter

  • Click OK.

First letters are capitalized.

VBA Capitalize First Letter

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


<< Go Back to Change Case | Text Formatting | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Alif Bin Hussain
Alif Bin Hussain

Alif Bin Hussain earned a BSc in Civil Engineering from Bangladesh University of Engineering and Technology. As an engineering graduate, he has a deep passion for research and innovation. He loves to play with Excel. In his role as an Excel & VBA Content Developer at ExcelDemy, he not only solves tricky problems but also shows enthusiasm and expertise in handling tough situations with finesse, emphasizing his dedication to delivering top-notch content. He is interested in C, C++,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo