Change Case

Jayden

Board Regular
Joined
Mar 28, 2003
Messages
141
I am just wondering if there is a way to change the font case in excel.

This is want I have. I have created a template for which sorts info supplied form a email. What I do is copy the entire email and past it into a spacific cell in a spreadsheet. Then the formulas that I have set up put the email into a better format. The problem is that not all the e-mails are the same case some have upper case and some are all lower. Is there a way that I can change them automaticly with excel instead of retyping them?


thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Take a look at =PROPER.

Or this nifty converter that Ivan wrote:
Code:
Sub TextCon()
'code by Ivan F. Moala
    Dim ocell As Range, ans As String
    
    ans = Application.InputBox("Type in Letter" & vbCr & _
        "(L)owercase, (U)ppercase, (S)entence, (T)itles ")
    If ans = "" Then Exit Sub
    
    For Each ocell In Selection.SpecialCells(xlCellTypeConstants, 2)
        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
Hope that helps,

Smitty
 
Upvote 0
I inserted the code but I am not sure if I am doing it right. I placed the code and ran the Macro but the case stayed the same.

I am Getting a 1004 Run time error. any reason
 
Upvote 0
I am Getting a 1004 Run time error.
What line is it bombing on?

I also should have pointed out that in addition to PROPER, there are UPPER & LOWER functions as well.

Hope that helps,

Smitty
 
Upvote 0
I'm not sure what's the source of your problem's with Ivan's code. It works reasonably well for me (a few glitches with Sentence case, but that may be happening because of merged cells).

Anyhow, I wrote most of this a while ago, but went back and tried to shore up some weaknesses. It does not ask which case you want, but rather it toggles through U-->L-->P. It does this on a cell-by-cell basis. It will also work for some drawing objects and chart objects.

Hope you find it useful:

<font face=Courier New><SPAN style="color:#007F00">'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯</SPAN>
<SPAN style="color:#00007F">Sub</SPAN> ChangeCase()
<SPAN style="color:#007F00">'_______________</SPAN>

<SPAN style="color:#007F00">' Toggles case on items in selection.  Selection can be</SPAN>
<SPAN style="color:#007F00">' cells, certain drawing objects and certain chart objects.</SPAN>

<SPAN style="color:#007F00">' written by Greg Truby</SPAN>

    <SPAN style="color:#00007F">If</SPAN> TypeName(Selection) <> "Range" <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> OtherItemSelected
    
    <SPAN style="color:#00007F">Dim</SPAN> strMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> Range, r1 <SPAN style="color:#00007F">As</SPAN> Range, r2 <SPAN style="color:#00007F">As</SPAN> Range

    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> r1 = Selection.SpecialCells(xlCellTypeConstants, XlSpecialCellsValue.xlTextValues)
    <SPAN style="color:#00007F">If</SPAN> r1 <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">If</SPAN> ActiveSheet.ProtectContents <SPAN style="color:#00007F">Then</SPAN>
            strMsg = "This macro will fail if the worksheet is protected."
        <SPAN style="color:#00007F">Else</SPAN>
            strMsg = "No text cells in selection.  Only numbers, dates &/or formulas"
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        MsgBox strMsg, vbExclamation, "No Text Cells"
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> r = Selection
<SPAN style="color:#007F00">'    UndoLoad r</SPAN>
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> r1 <SPAN style="color:#00007F">In</SPAN> r.Cells
        <SPAN style="color:#00007F">Set</SPAN> r2 = r1.SpecialCells(xlCellTypeConstants, XlSpecialCellsValue.xlTextValues)
        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> r2 <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(r1, r2) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
                <SPAN style="color:#00007F">If</SPAN> r2.Count > r1.Count <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Set</SPAN> r2 = r1
                r2.Value = ChangeTextCase(r2.Value)
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> r1
<SPAN style="color:#007F00">'    Application.OnUndo "Undo ChangeCase", "UndoRestore"</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    
OtherItemSelected:
<SPAN style="color:#007F00">'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrorHandler
    <SPAN style="color:#00007F">Dim</SPAN> varObject <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    <SPAN style="color:#00007F">If</SPAN> TypeName(Selection) = "DrawingObjects" <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> varObject <SPAN style="color:#00007F">In</SPAN> Selection
            CaseForShapes varObject
        <SPAN style="color:#00007F">Next</SPAN> varObject
    <SPAN style="color:#00007F">Else</SPAN>
        CaseForShapes Selection
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

ErrorHandler:
<SPAN style="color:#007F00">'¯¯¯¯¯¯¯¯¯¯¯¯</SPAN>
    Beep
    MsgBox "Could not execute macro." & vbCrLf & vbCrLf & _
           "Check to see if the Worksheet or Workbook has Protection turned on.", _
           vbCritical, "Error"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#007F00">'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯</SPAN>
<SPAN style="color:#00007F">Sub</SPAN> CaseForShapes(varObject <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>)
<SPAN style="color:#007F00">'______________________________________</SPAN>
    
    <SPAN style="color:#00007F">Dim</SPAN> strText <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> TypeName(varObject)
        <SPAN style="color:#00007F">Case</SPAN> "Rectangle", "Oval"
            strText = varObject.Characters.Text
            strText = ChangeTextCase(strText)
            varObject.Characters.Text = strText
        <SPAN style="color:#00007F">Case</SPAN> "TextBox", "ChartTitle", "AxisTitle"
            strText = varObject.Text
            strText = ChangeTextCase(strText)
            varObject.Text = strText
        <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN>
            Beep
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#007F00">'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯</SPAN>
<SPAN style="color:#00007F">Function</SPAN> ChangeTextCase(<SPAN style="color:#00007F">ByVal</SPAN> strText <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#007F00">'_________________________________________________________</SPAN>
    
    <SPAN style="color:#00007F">If</SPAN> strText = UCase(strText) <SPAN style="color:#00007F">Then</SPAN>
        strText = LCase(strText)
    <SPAN style="color:#00007F">ElseIf</SPAN> strText = LCase(strText) <SPAN style="color:#00007F">Then</SPAN>
        strText = StrConv(strText, vbProperCase)
    <SPAN style="color:#00007F">Else</SPAN>
        strText = UCase(strText)
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    ChangeTextCase = strText
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>

Oh yeah, I commented out a couple of lines that refer to making the case change Undo-able.

Regards,
 
Upvote 0
To Greg Truby

With your code as it stands, if only one cell is selected that is not a text cell, r1 will be set as all of the text cells on the worksheet by the line that reads :-

Code:
Set r1 = Selection.SpecialCells(xlCellTypeConstants, XlSpecialCellsValue.xlTextValues)

Suggest that it should be replaced with something like this :-

Code:
If Selection.Cells.Count = 1 Then
    Set r1 = Selection
    If Selection.HasFormula Or TypeName(Selection.Value) <> "String" And IsNumeric(Selection) Then Set r1 = Nothing
Else
    On Error Resume Next
    Set r1 = Selection.SpecialCells(xlCellTypeConstants, XlSpecialCellsValue.xlTextValues)
End If

The same comment applies to Ivan Moala's procedure.
With that code, if only one cell (of any type) is selected, ALL of the cells on the worksheet that contain text constants will be converted.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top