How to remove caps and switch to lowercase

kerrysr

New Member
Joined
Mar 7, 2002
Messages
11
I have many records that are in all caps. I need to switch them to lower case with only the 1st letter capitalized. Also, these records are mixed with records that are in the proper format (only 1st letter capitalized)

How can I do this?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Excel has a built in function called Propper.
=Propper(WORds wiTH some Lower case)

results in
Words With Some Lower Case
 
Upvote 0
If you're doing this infrequently, then what "Teach" ( :LOL: ) suggests is all you need.

If you're doing it a lot (and the change from all caps to proper case is all you're doing) then you might be interested in this.
Rich (BB code):
Sub FormatToProper()
'Makes the entire sheet formatted as "Proper Case"
Application.ScreenUpdating = False
Dim Rng As Range
Dim c As Range
On Error Resume Next
'Set next line to any range you like
Set Rng = Cells.SpecialCells(xlCellTypeConstants, 2)
    
For Each c In Rng
    c.Value = Application.WorksheetFunction.Proper(c.Text)
Next c

Application.ScreenUpdating = True
End Sub
If you don't want every word capitalized, but just the first word in each cell, then maybe this...
Rich (BB code):
Sub FormatToSentence()
'Makes the entire sheet formatted in "Sentence case"
Application.ScreenUpdating = False
Dim Rng As Range
Dim c As Range
On Error Resume Next
'Set next line to any range you like
Set Rng = Cells.SpecialCells(xlCellTypeConstants, 2)
    
For Each c In Rng
    c.Value = UCase(Left(c.Text, 1)) & _
    LCase(Right(c.Text, Len(c.Text) - 1))
Next c

Application.ScreenUpdating = True
End Sub
A third option...(Gives you several choices of text case on the selected range. - Don't remember who wrote it, just that I like it.)
Rich (BB code):
Option Explicit
Sub TextCaseChange()
Dim RgText As Range
Dim oCell As Range
Dim Ans As String
Dim strTest As String
Dim sCap As Integer, _
    lCap As Integer, _
    i As Integer
Again:
Ans = Application.InputBox("[L]owercase" & vbCr & "ppercase" & vbCr & _
        "entence" & vbCr & "[T]itles" & vbCr & "[C]apsSmall", _
        "Type in a Letter", Type:=2)
If Ans = "False" Then Exit Sub
If InStr(1, "LUSTC", UCase(Ans), vbTextCompare) = 0 Or Len(Ans) > 1 Then GoTo Again
On Error GoTo NoText
If Selection.Count = 1 Then
    Set RgText = Selection
Else
    Set RgText = Selection.SpecialCells(xlCellTypeConstants, 2)
End If
On Error GoTo 0
For Each oCell In RgText
    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)
        Case "C"
                lCap = oCell.Characters(1, 1).Font.Size
                sCap = Int(lCap * 0.85)
                'Small caps for everything.
                oCell.Font.Size = sCap
                oCell.Value = UCase(oCell.Text)
                strTest = oCell.Value
                'Large caps for 1st letter of words.
                strTest = Application.Proper(strTest)
                For i = 1 To Len(strTest)
                    If Mid(strTest, i, 1) = UCase(Mid(strTest, i, 1)) Then
                        oCell.Characters(i, 1).Font.Size = lCap
                    End If
                Next i
    End Select
Next
Exit Sub
NoText:
MsgBox "No Text in your selection @ " & Selection.Address
End Sub
And even a fourth option... download ASAP Utilities. This has (roughly) a million really useful things, changing text case being just one. (With even more choices than offered here!)

(Sorry you asked now? :LOL: )
Dan
 
Upvote 0
Hello,

Another option which handles multi-sentence strings (with varying punctuation):

Code:
Public Function sCase(ByRef strIn As String) As String
Dim bArr() As Byte, i As Long, i2 As Long
If strIn = vbNullString Then Exit Function
Let bArr = StrConv(strIn, vbFromUnicode)
Select Case bArr(0)
    Case 97 To 122
        bArr(0) = bArr(0) - 32
End Select
For i = 1 To UBound(bArr)
    Select Case bArr(i)
        Case 105
            If Not i = UBound(bArr) Then
                Select Case bArr(i + 1)
                    Case 32, 33, 39, 44, 46, 58, 59, 63, 148, 160
                        If bArr(i - 1) = 32 Then _
                            bArr(i) = bArr(i) - 32
                End Select
            ElseIf bArr(i - 1) = 32 Then _
                bArr(i) = bArr(i) - 32
            End If
        Case 33, 46, 58, 63
            For i2 = i + 1 To UBound(bArr)
                Select Case bArr(i2)
                    Case 97 To 122
                        bArr(i2) = bArr(i2) - 32
                        i = i2:   Exit For
                End Select
                Select Case bArr(i2)
                    Case 32, 33, 46, 63, 160
                    Case Else
                        i = i2:   Exit For
                End Select
            Next
    End Select
Next
sCase = StrConv(bArr, vbUnicode)
End Function
 
 
 
Sub Sentence_Case()
Dim cl As Range, clcMode As Long
 
With Application
    .ScreenUpdating = False
    clcMode = .Calculation
    .Calculation = xlCalculationManual
End With
 
On Error GoTo ErrorTrap
 
For Each cl In Range("a:a").SpecialCells(xlConstants, xlTextValues)
    cl.Value = sCase(LCase$(cl.Value))
Next cl
 
ErrorTrap:
 
With Application
    .Calculation = clcMode
    .ScreenUpdating = True
End With
End Sub

Change:

For Each cl In Range("a:a").SpecialCells(xlConstants, xlTextValues)

To the range in question. ;)

This will also look at specific cases of the letter I, changing:

i'm, i, i'd, i'll

to

I'm, I, I'd, I'll
 
Last edited:
Upvote 0
All right, when writing an algorithm, the trick is to make it right before you make it faster. Now that we have the desired functionality in place, our next step is to optimize.

StrConv() is a handy function, but it is an expensive function, and we don't need it in this case. While the resulting function might be a little harder to follow, as you only want to flip the bytes on every other element in the coerced byte array, the results might just be worth it, especially on smaller strings.

So, with this in mind, we make the following adjustments:

<font face=Courier New><SPAN style="color:darkblue">Public</SPAN> <SPAN style="color:darkblue">Function</SPAN> sCase(<SPAN style="color:darkblue">ByRef</SPAN> strIn <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>) <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> bArr() <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Byte</SPAN>, i <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>, i2 <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>
<SPAN style="color:darkblue">If</SPAN> strIn = vbNullString <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Function</SPAN>
<SPAN style="color:darkblue">Let</SPAN> bArr = strIn
<SPAN style="color:darkblue">Select</SPAN> <SPAN style="color:darkblue">Case</SPAN> bArr(0)
    <SPAN style="color:darkblue">Case</SPAN> 97 <SPAN style="color:darkblue">To</SPAN> 122
        bArr(0) = bArr(0) - 32
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Select</SPAN>
<SPAN style="color:darkblue">For</SPAN> i = 2 <SPAN style="color:darkblue">To</SPAN> <SPAN style="color:darkblue">UBound</SPAN>(bArr) <SPAN style="color:darkblue">Step</SPAN> 2
    <SPAN style="color:darkblue">Select</SPAN> <SPAN style="color:darkblue">Case</SPAN> bArr(i)
        <SPAN style="color:darkblue">Case</SPAN> 105
            <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> i = <SPAN style="color:darkblue">UBound</SPAN>(bArr) - 1 <SPAN style="color:darkblue">Then</SPAN>
                <SPAN style="color:darkblue">Select</SPAN> <SPAN style="color:darkblue">Case</SPAN> bArr(i + 2)
                    <SPAN style="color:darkblue">Case</SPAN> 32, 33, 39, 44, 46, 58, 59, 63, 148, 160
                        <SPAN style="color:darkblue">If</SPAN> bArr(i - 2) = 32 Then _
                            bArr(i) = bArr(i) - 32
                <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Select</SPAN>
            <SPAN style="color:darkblue">ElseIf</SPAN> bArr(i - 2) = 32 Then _
                bArr(i) = bArr(i) - 32
            <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
        <SPAN style="color:darkblue">Case</SPAN> 33, 46, 58, 63
            <SPAN style="color:darkblue">For</SPAN> i2 = i + 2 <SPAN style="color:darkblue">To</SPAN> <SPAN style="color:darkblue">UBound</SPAN>(bArr) <SPAN style="color:darkblue">Step</SPAN> 2
                <SPAN style="color:darkblue">Select</SPAN> <SPAN style="color:darkblue">Case</SPAN> bArr(i2)
                    <SPAN style="color:darkblue">Case</SPAN> 97 <SPAN style="color:darkblue">To</SPAN> 122
                        bArr(i2) = bArr(i2) - 32
                        i = i2:   <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">For</SPAN>
                <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Select</SPAN>
                <SPAN style="color:darkblue">Select</SPAN> <SPAN style="color:darkblue">Case</SPAN> bArr(i2)
                    <SPAN style="color:darkblue">Case</SPAN> 32, 33, 46, 63, 160
                    <SPAN style="color:darkblue">Case</SPAN> <SPAN style="color:darkblue">Else</SPAN>
                        i = i2:   <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">For</SPAN>
                <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Select</SPAN>
            <SPAN style="color:darkblue">Next</SPAN>
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Select</SPAN>
<SPAN style="color:darkblue">Next</SPAN>
sCase = bArr
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Function</SPAN>

<SPAN style="color:darkblue">Sub</SPAN> testTime()
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("hello? erm, i<SPAN style="color:green">'M only testing, eh. indeed, " & _
    "i am inquisitve."))</SPAN>
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("how old?! 22 Years."))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("how old?! twenty-two Years."))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("hmmmm.... wOrking?!?! sam i am. yes-no? " & _
    "isn<SPAN style="color:green">'t i'm isn't."))</SPAN>
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("THE DAY WAS SUNNY AND I WORE A HAT.PETER WAS THERE "))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("no WorRies, i<SPAN style="color:green">'m ONLY testIng!         yes-no?"))</SPAN>
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("mY fRiend & i"))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("iiiiiiiiiiiiii"))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("***T. toast %T i @"))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase(LCase$("re: sentences."))
<SPAN style="color:darkblue">Debug</SPAN>.<SPAN style="color:darkblue">Print</SPAN> sCase("hello? thought i<SPAN style="color:green">'d test this for David McRitchie. NOTHING.")</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>

I haven't tested the code with a high-resolution timer, while I could, but on small strings I would imagine it's quite a bit faster than its predecessor (which was pretty fast to begin with). :)
 
Upvote 0

Forum statistics

Threads
1,215,761
Messages
6,126,735
Members
449,333
Latest member
Adiadidas

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