A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi,
I now see the problem. If you select a single cell and call your code it's changing the case of every cell on the worksheet instead of just the selected cell.
I have never understood why but this is an oddity in the way specialcells seems to behave. If you select a single cell and execute this line
Set WorkRange = Selection.SpecialCells(xlCellTypeConstants, xlCellTypeConstants)
then Workrange will be set to Set to all cells so if you try and execute your code on a single cell it will fail and change the case of cells that weren't selected. If you select multiple cells then the code works fine and sets Workrange to cells containing text values within that selection. Here's one way
EDIT.... In the sample workbook then If TypeName (Selection)... does nothing because there's nothing else to select but if the worksheet had some shapes or a chart in that was selected then it would be useful. I would leave it in.
Private Sub OKButton_Click()
Dim WorkRange As Range
'Process only text cells, no formulas
'On Error Resume Next
If Selection.Cells.Count > 1 Then
Set WorkRange = Selection.SpecialCells(xlCellTypeConstants, xlCellTypeConstants)
ElseIf WorksheetFunction.IsText(ActiveCell.Value) Then
Set WorkRange = ActiveCell
Else
MsgBox "Only numeric cells selected"
Unload Me
Exit Sub
End If
'Set WorkRange = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
'Upper Case
If OptionUpper Then
For Each cell In WorkRange
If Not cell.HasFormula Then
cell.Value = UCase(cell.Value)
End If
Next cell
End If
'Lower Case
If OptionLower Then
For Each cell In WorkRange
If Not cell.HasFormula Then
cell.Value = LCase(cell.Value)
End If
Next cell
End If
'Proper Case
If OptionProper Then
For Each cell In WorkRange
If Not cell.HasFormula Then
cell.Value = Application. _
WorksheetFunction.Proper(cell.Value)
End If
Next cell
End If
Unload UserForm1
End Sub