VBA To Change Text Case Within A Selection Of Cells

VBA Code Change Selected Cell Text Case

What This VBA Code Does

the following VBA macro code is something I use all the time to manipulate the look of my report headings. The VBA code mimics a feature that is found in PowerPoint and Word, but unfortunately was left out of the Excel Application. Essentially the macro will loop through applying 3 different case-types to your selected cell text. Just keep re-running the macro until you get your desired case.

The case-types that can be applied are:

  • Uppercase
  • Lowercase
  • Propercase

VBA Code:

Sub ToggleTextCase()
'PURPOSE: Toggle selected cell text values between Upper, Lower, & Proper cases
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim FirstCellValue As String
Dim rng As Range
Dim cell As Range

'Optimize Code
  Application.ScreenUpdating = False

'Was a Range Selected? (Error Handling)
  If TypeName(Selection) <> "Range" Then
    MsgBox "No cell range was selected!", vbCritical, "No Range Selected"
    Exit Sub
  End If

'Remove cells with formulas or cells that are hidden from selection
  On Error Resume Next
    If Selection.Cells.Count > 1 Then
      Set rng = Selection.SpecialCells(xlCellTypeVisible)
      Set rng = rng.SpecialCells(xlConstants)
      Set rng = ActiveCell
    End If

'Store Current Case Structure of first cell in selection
  FirstCellValue = rng.Cells(1, 1).Value
'Apply Case Change (based on first cell's current case structure)
  If FirstCellValue = LCase(FirstCellValue) Then
    'If Lowercased, make Propercased
      For Each cell In rng.Cells
        cell.Value = Application.WorksheetFunction.Proper(cell.Value)
      Next cell
  ElseIf FirstCellValue = UCase(FirstCellValue) Then
    'If Uppercased, make Lowercased
      For Each cell In rng.Cells
        cell.Value = LCase(cell.Value)
      Next cell
    'If Propercased, make Uppercased
      For Each cell In rng.Cells
        cell.Value = UCase(cell.Value)
      Next cell
  End If

End Sub

How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

Founder, TheSpreadsheetGuru.com