Sample macro code to change the case of text in Excel

Applies To
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel 2024 Excel 2024 for Mac Excel 2021 Excel 2021 for Mac

Summary

Although Microsoft Excel includes functions for changing the case of text, you can also use Visual Basic for Applications (VBA) macros to change text case programmatically.

This article provides three sample VBA macros that convert text to uppercase, lowercase, or proper case (the first letter of each word is capitalized). These macros perform operations similar to the Excel UPPER, LOWER, and PROPER functions.

Sample macros

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Change text to uppercase

Sub Uppercase()
   Dim x As Range

   ' Loop to cycle through each cell in the specified range.
   For Each x In Range("A1:A5")
      ' Change the text in the range to uppercase letters.
      x.Value = UCase(x.Value)
   Next
End Sub

Change text to lowercase

Sub Lowercase()
   Dim x As Range

   ' Loop to cycle through each cell in the specified range.
   For Each x In Range("B1:B5")
      x.Value = LCase(x.Value)
   Next
End Sub

Change text to proper case

Sub Proper_Case()
   Dim x As Range

   ' Loop to cycle through each cell in the specified range.
   For Each x In Range("C1:C5")
      ' Use the Excel PROPER worksheet function to convert text to proper case.
      x.Value = Application.Proper(x.Value)
   Next
End Sub

Test the sample macros

To test the sample macros, follow these steps:

  1. In the same workbook that contains the macros, enter the following sample data into a new worksheet:

    A1: toM B1: toM C1: toM
    A2: sUe B2: sUe C2: sUe
    A3: joe SMITH B3: joe SMITH C3: joe SMITH
    A4: mary B4: mary C4: mary
    A5: LORI B5: LORI C5: LORI

  2. Make sure the worksheet containing the sample data is active. On the Developer tab, select Macros. Select a macro, and then select Run. Repeat this step for each macro.

Each macro converts the text in its specified range:

  • Uppercase: Converts text in cells A1:A5 to uppercase.
  • Lowercase: Converts text in cells B1:B5 to lowercase.
  • Proper_Case: Converts text in cells C1:C5 to proper case.

See also

How to change the case of text in Excel