Friday, June 20, 2008

Change Case in Excel

Have you ever needed to change the case of text in selected cells within an Excel spreadsheet? After searching the Internet, I found something close then modified it to meet my needs. Others may need to do the same so here is the macro that I ended up with.




Sub Change_Case()

Dim ocell As Range
Dim Ans As String
Ans = Application.InputBox("Type in Letter" & vbCr & _
"(L)owercase, (U)ppercase, (S)entence, (T)itles ")
If Ans = "" Then Exit Sub
If TypeName(Selection) <> "Range" Then Exit Sub

For Each ocell In Selection

Select Case UCase(Ans)
Case "L": ocell = LCase(ocell.Text)
Case "U": ocell = UCase(ocell.Text)
Case "S": ocell = UCase(Left(ocell.Text, 1)) & _
LCase(Right(ocell.Text, Len(ocell.Text) - 1))
Case "T": ocell = Application.WorksheetFunction.Proper(ocell.Text)
End Select

Next

End Sub




I only needed it to force all upper or all lower case. I have not tested the other options so use it at your own risk.

No comments: