Consulting

Results 1 to 8 of 8

Thread: Toggle Text Case

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Toggle Text Case

    I have the below code from an Excel macro that I would like to use in Word. Can someone help modify the code so that it will work?

    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)
        Else
          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
        
      Else
        'If Propercased, make Uppercased
          For Each cell In rng.Cells
            cell.Value = UCase(cell.Value)
          Next cell
        
      End If
    
    
    End Sub
    Any help would be GREATLY appreciated!!
    Last edited by Rob Conklin; 02-09-2021 at 02:20 PM. Reason: Needing this Excel macro to work in Word.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •