VBA macros · Data

VBA macro to change text case (UPPER, lower, Proper) in Excel

Change the selected text to UPPER CASE, lower case or Proper Case in place, without helper columns. Formulas and numbers are left alone.

Windows + Mac

Word has a Change Case button; Excel does not. Names typed in capitals, product codes in mixed case and email addresses that should be lower case are usually fixed with UPPER, LOWER or PROPER in a helper column, then pasted back as values. This macro changes the case directly in the selected cells and asks which style you want.

VBA
Option Explicit

Sub ChangeTextCase()
    Dim rng As Range, c As Range
    Dim choice As Variant
    Dim s As String

    If TypeName(Selection) <> "Range" Then Exit Sub

    choice = Application.InputBox("1 = UPPER CASE" & vbLf & "2 = lower case" & vbLf & _
                                  "3 = Proper Case", "Change case", 3, Type:=1)
    If VarType(choice) = vbBoolean Then Exit Sub          ' Cancel
    choice = Int(choice)
    If choice < 1 Or choice > 3 Then Exit Sub

    ' Only typed text (no formulas, no numbers)
    If Selection.Cells.CountLarge = 1 Then
        If Not Selection.HasFormula And VarType(Selection.Value) = vbString Then Set rng = Selection
    Else
        On Error Resume Next
        Set rng = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
        On Error GoTo 0
    End If
    If rng Is Nothing Then Exit Sub

    Application.ScreenUpdating = False
    For Each c In rng.Cells
        Select Case choice
            Case 1: s = UCase$(c.Value)
            Case 2: s = LCase$(c.Value)
            Case 3: s = Application.WorksheetFunction.Proper(c.Value)
        End Select
        If s <> c.Value Then c.Value = s
    Next c
    Application.ScreenUpdating = True
End Sub
Change the case of the selected text

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick ChangeTextCase and click Run.
  5. Select the cells first; type 1, 2 or 3 in the box that appears.
  6. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

Application.InputBox with Type:=1 only accepts numbers, so a typing mistake produces Excel's own number is not valid message instead of a crash. The default answer is 3, Proper Case. Cancel returns False, which the VarType test catches.

As in VBA macro to trim spaces in selected cells in Excel, SpecialCells(xlCellTypeConstants, xlTextValues) limits the work to cells with typed text, and a one-cell selection is handled separately because SpecialCells on a single cell looks at the whole sheet. Formulas are never overwritten with their results.

UCase$ and LCase$ are VBA's own case functions. For Proper Case the macro calls Excel's PROPER through WorksheetFunction, not VBA's StrConv(s, vbProperCase). The two differ: StrConv only capitalises a letter after a space, while PROPER capitalises after any character that is not a letter, which turns o'neil-smith into O'Neil-Smith. Select Case picks the right function; see Making decisions: If, ElseIf and Select Case and Working with text (strings) in VBA.

Change it

For a fourth option, Sentence case (first letter capital, the rest lower), add a Case 4 and allow 4 in the check:

VBA
            Case 4: s = UCase$(Left$(c.Value, 1)) & LCase$(Mid$(c.Value, 2))
Sentence case: only the first letter in capitals

To skip the question and always use one style, delete the InputBox lines and set choice = 1 (or 2, or 3) at the top. Three separate macros, one per style, can then be put on the Quick Access Toolbar. To change the case of everything in the used range of a sheet, select all cells with Ctrl+A (Mac: Cmd+A) first; the macro still touches text only.

What can go wrong

Proper Case is not perfect for names: McDonald becomes Mcdonald, van der Berg becomes Van Der Berg and abbreviations like USA become Usa. Check the results and correct those by hand.

Watch out. Writing the text back lets Excel reinterpret it. Changing the text true to TRUE turns it into the logical value TRUE, and some codes can become numbers or dates. Format such columns as Text first. A macro cannot be undone with Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data.
Tip. Works on Windows and Mac. For a live formula instead, use =UPPER(A2), =LOWER(A2) or =PROPER(A2) in a helper column.

Related: VBA macro to trim spaces in selected cells in Excel, PROPER, Working with text (strings) in VBA.

More vba macros