How to use Excel VBA to Change the Case of Text – 6 Examples

Download Practice Workbook

Download the practice workbook.


To change the case of text in the cells:

Sample Dataset to Change Case of Selected Text Using Excel VBA


Example 1 – Transform Text to Upper Case

Steps:

  • Go to the Developer tab >> Visual Basic.

Access the Visual Basic Tool

  • In the Microsoft Visual Basic for Applications window, select Sheet2.

Choose Sheet2 to Write Appropriate VBA Code

  • Enter the VBA code below and press CTRL + S.
Sub Change_Selected_Text_to_Upper_Case()
On Error Resume Next
Dim ChangeCell As Range
For Each ChangeCell In Selection.Cells
ChangeCell.Value = UCase(ChangeCell.Value)
Next
On Error GoTo 0
End Sub

Excel VBA to Upper Case Selected Text

  • In the dialog box, click No.

Microsoft Excel Dialogue Box

  • In Save As, select Save as type:
  • Choose .xlsm and click Save.

Save As Dialogue Box

  • Close the code window and select the text.
  • Go to the Developer tab >> Macros.

Access the Macros Tool

  • Choose Sheet2.Change_Selected_Text_to_Upper_Case and click Run.

Run the Appropriate Macro to Upper Case Selected Text by Excel VBA

This is the output.

Changed Selected Text to Upper Case by Excel VBA


Example 2 – Convert Text to Lower Case

Steps:

  • Open the Microsoft Visual Basic for Applications window.
  • Choose Sheet3.

Choose Sheet3 to Apply VBA Code

  • Enter the VBA code below and press CTRL + S.
Sub Change_Selected_Text_to_Lower_Case()
On Error Resume Next
Dim ChangeCell As Range
For Each ChangeCell In Selection.Cells
ChangeCell.Value = LCase(ChangeCell.Value)
Next
On Error GoTo 0
End Sub

Excel VBA to Lower Case Selected Text

  • Close the code window.
  • Select the text >> go to the Developer tab >> Macros.

Access the Macros Tool

 

  • Select Sheet3.Change_Selected_Text_to_Lower_Case and click Run.

Run a Macro to Lower Case Selected Text by Excel VBA

This is the output.

Changed Selected Text to Lower Case by Excel VBA


Example 3: Change the Case of the Selected Text to Proper Case Text

Steps:

  • Open the Microsoft Visual Basic for Applications window.
  • Select Sheet4.

Choose Sheet4

  • Enter the VBA code below and press CTRL + S.
Sub Change_Selcted_Text_to_Proper_Case()
On Error Resume Next
Dim ChangeCell As Range
For Each ChangeCell In Selection.Cells
ChangeCell.Value = WorksheetFunction.Proper(ChangeCell.Value)
Next
On Error GoTo 0
End Sub

Excel VBA to Change Selected Text to Proper Case

  • Close the code window.
  • Select the text >> go to the Developer tab >> Macros.

Access the Macros Tool

  • Select Sheet4.Change_Selected_Text_to_Proper_Case  and click Run.

Run VBA Macro to Change Case of Selected Text to Proper Case

This is the output.

Changed Selected Text to Proper Case by Excel VBA


Example 4 – Convert the text to Sentence Case

Capitalize the first letter of each sentence only:

Steps:

  • Open the Microsoft Visual Basic for Applications window.
  • Select Sheet5.

Choose Sheet5

  • Enter the VBA code below and press CTRL + S.
Sub Change_Selected_Text_to_Sentence_Case()
On Error Resume Next
Dim ChangeCell As Range
For Each ChangeCell In Selection.Cells
If Len(ChangeCell.Value) >= 2 Then
ChangeCell.Value = UCase(Left(ChangeCell.Value, 1)) & _
LCase(Right(ChangeCell.Value, (Len(ChangeCell.Value) - 1)))
End If
Next
On Error GoTo 0
End Sub

Excel VBA to Change Selected Text to Sentence Case

  • Close the code window.
  • Select the text >> go to the Developer tab >> Macros.

Access the Macros Tool

  • Choose Sheet5.Change_Selected_Text_to_Sentence_Case and click Run.

Run Required VBA Macro to Change Selected Text to Sentence Case

This is the output.

Sentence Case Text by Excel VBA


Example 5 – Change the Selected Text to Toggle Case

 Steps:

  • Open the Microsoft Visual Basic for Applications window.
  • Click  Sheet6.

Choose Sheet6

  • Enter the VBA code below and press CTRL + S.
Sub Change_Selected_Text_to_Toggle_Case()
  Dim ChangeCell As Range
  Dim n As Integer
  On Error Resume Next
  For Each ChangeCell In Selection.Cells
     If Len(ChangeCell.Value) >= 2 And IsNumeric(ChangeCell.Value) = False And _
     IsEmpty(ChangeCell.Value) = False And IsNull(ChangeCell.Value) = False Then
          For n = 1 To Len(ChangeCell.Value) Step 2
               ChangeCell.Characters(n, 1).Text = UCase(ChangeCell.Characters(n, 1).Text)
          Next
          For n = 2 To Len(ChangeCell.Value) Step 2
               ChangeCell.Characters(n, 1).Text = LCase(ChangeCell.Characters(n, 1).Text)
          Next
     End If
  Next
  On Error GoTo 0
End Sub

Excel VBA to Change Case of Selected Text to Toggle Case

  • Close the code window.
  • Select the text >> go to the Developer tab >> Macros.

Access the Macros Tool

 

  • Choose Sheet6.Change_Selected_Text_to_Toggle_Case and click Run.

Run Macro to Change Case of Selected Text to Toggle Case

This is the output.

Toggle Case Text by Excel VBA


Example 6 – Create a UserForm Using VBA

 Step 1: Creating a UserForm

  • Go to the Developer tab >> Code >> Visual Basic >> open the VB Editor window.

Access the Visual Basic Tool

  • Go to the Insert tab >> UserForm.

Insert a User Form

UserForm1 and the Toolbox are displayed.

  • Press F4 to open the Properties window.
  • Change:

(Name) = ChangeCaseForm

Caption = Change Case of the Text

User Form Properties

  • Add a CommandButton.

Add Command Buttons to Create User Form

  • For the first command button, change the following properties:

(Name) = OKButton

Caption = OK

Default = True

OK Button Properties

  • Add another CommandButton and change the following properties:

(Name) = CancelButton

Caption = Cancel

Default = True

Cancel Button Properties

  • Add an OptionButton.

Add Option Buttons to Change Case of Selected Text by Excel VBA

  • For the first option button, change the following properties:

(Name) = OptionUpper

Caption = Upper Case

Value = True

Upper Case Option Button Properties

  • Add a second OptionButton and change the following properties:

(Name) = OptionLower

Caption = Lower Case

Value = True

Lower case Option Button Properties

  • Add a third OptionButton and change the following properties:

(Name) = OptionProper

Caption = Proper Case

Value = True

Proper Case Option Buttons

  • Adjust the size and position of the controls as shown below.

 

User Form to Change Case of Selected Text using Excel VBA


 Step 2: Formatting the Controls in the UserForm

 

  • Press CTRL, select two Commanduttons, and right-click.

Select the Command Buttons

You can also choose a command in Format.

  • Choose Make Same Size and Both.

Make the Buttons as Same Size

  • Choose Align and select Lefts.

Align the Buttons

  • Align and size the three OptionsButton.

Complete User Form


Step 3: Creating Event Handler Procedures

To create two event handler procedures: one to handle the Click event of the CancelButton, and the other to handle the Click event of the OKButton:

Read More: Why We Use Form Controls on a Worksheet?

 

  • Select ChangeCaseForm by double-clicking its name in Project.
  • Double-click the OKButton.

Access the OkButton's Code Window

  • Enter the VBA code:
Private Sub OKButton_Click()
Dim WorkRange As Range

    Dim cell As Range
    ChangeCaseForm.Hide

'   Exit if a range is not selected
    If TypeName(Selection) <> "Range" Then Exit Sub
    Application.ScreenUpdating = False

'   Avoid processing cells out of the used area
    Set WorkRange = Intersect(Selection, ActiveSheet.UsedRange)

'   Upper case
    If OptionUpper Then
        For Each cell In WorkRange
        If Not cell.HasFormula Then
        cell.Value = StrConv(cell.Value, vbUpperCase)
        End If
        Next cell
    End If

'   Lower case
    If OptionLower Then
        For Each cell In WorkRange
        If Not cell.HasFormula Then
        cell.Value = StrConv(cell.Value, vbLowerCase)
        End If
        Next cell
    End If

'   Proper case
    If OptionProper Then
        For Each cell In WorkRange
        If Not cell.HasFormula Then
        cell.Value = StrConv(cell.Value, vbProperCase)
        End If
        Next cell
    End If

End Sub

The macro asks the user to select a cell or a range of cells. If a range is not selected, the procedure ends. The procedure uses three separate blocks. Only one block is executed, depending on which OptionButton is selected. The selected OptionButton has the value: True. When the user clicks OK, the UserForm is unloaded.

  • Enter the code to handle the Click event for the CancelButton control. and the CancelButton click event.
Private Sub CancelButton_Click()
    Unload ChangeCaseForm
End Sub

This procedure will be executed when someone will click the Cancel button (CancelButton). The dialog box will vanish when this code executes.


Step 4: Testing the UserForm

  • Go to the Developer tab >> Visual Basic
  • Choose Insert >> Module.

Insert Module

  • Enter the following code.
Sub ShowCaseChangeBox()
   ChangeCaseForm.Show
End Sub

Enable the ShowCaseChangeBox User Form

 

  • Enter text into a range of cells.
  • Select the range with the text.
  • Go to the Developer tab >> Macros or press Alt+F8.

Access the Macros Tool

 

  • Select ShowCaseChangeBox  and click Run.

Run ShowCaseChangeForm Macro to Change Case of Selected Text by Excel VBA

  • In the UserForm, select: Upper Case, Lower Case, or Proper Case.
  • Click OK.

Select Option to Change Case of Selected Text

If you click Cancel, no changes happen.

Upper Case Text by Excel VBA

Read More: How to add a macro to your Quick Access toolbar in Excel


Making the Macro Available from a Worksheet Button:

  • Go to: Developer >> Controls >> Insert
  • Click Button in Form Controls.

Insert a Button to Enable Required Macro

  • Click and drag in the worksheet to create the button.
  • In Assign Macro, select ShowCaseChangeBox.
  • Click OK.

Assign Required Macro to Change Case of Selected Text

You can right-click the button and change the text.

  • Click Change Case to run the macro. (You can change color, text size , etc.)

Button to Change Case of Selected Text Through Excel VBA

 

Get FREE Advanced Excel Exercises with Solutions!

Kawser Ahmed
Kawser Ahmed

Kawser Ahmed is a Microsoft Excel Expert, Udemy Course Instructor, Data Analyst, Finance professional, and Chief Editor of ExcelDemy. He is the founder and CEO of SOFTEKO (a software and content marketing company). He has a B.Sc in Electrical and Electronics Engineering. As a Udemy instructor, he offers 8 acclaimed Excel courses, one selected for Udemy Business. A devoted MS Excel enthusiast, Kawser has contributed over 200 articles and reviewed thousands more. His expertise extends to Data Analysis,... Read Full Bio

3 Comments

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo