Convert text into Sentence Case text

pedy

Board Regular
Joined
Jan 6, 2006
Messages
217
Hi

I found this macro that will convert text into Sentence Case text:

Sub Sentence_Case()
For Each Cell In ActiveSheet.UsedRange. _
SpecialCells(xlTextValues)
s = Cell.Value
Start = True
For I = 1 To Len(s)
ch = Mid(s, I, 1)
Select Case ch
Case "."
Start = True
Case "?"
Start = True
Case "a" To "z"
If Start Then ch = UCase(ch): Start = False
Case "A" To "Z"
If Start Then Start = False Else ch = LCase(ch)
End Select
Mid(s, I, 1) = ch
Next
Cell.Value = s
Next
End Sub

My questions are:

1 - How would I go about applying it to specific columns (F-J for instance),
2 - How would I apply the Capital "I" rule ?

I appreciate any help

Pedy
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
For just columns F:J you could use:

Code:
Sub Sentence_Case()
For Each Cell In ActiveSheet.Range("F:J"). _
SpecialCells(xlTextValues)
s = Cell.Value
Start = True
For I = 1 To Len(s)
ch = Mid(s, I, 1)
Select Case ch
Case "."
Start = True
Case "?"
Start = True
Case "a" To "z"
If Start Then ch = UCase(ch): Start = False
Case "A" To "Z"
If Start Then Start = False Else ch = LCase(ch)
End Select
Mid(s, I, 1) = ch
Next
Cell.Value = s
Next
End Sub
Do you mean if it is a lowercase i in a sentence it is not making it uppercase?
 
Upvote 0
Wow that was fast. Thanks !

Correct, the "i" is remainning lower case...

Pedy
 
Upvote 0
Maybe try and see what this does:

Code:
Sub Sentence_Case()
For Each Cell In ActiveSheet.Range("F:J"). _
SpecialCells(xlTextValues)
s = Cell.Value
Start = True
For I = 1 To Len(s)
ch = Mid(s, I, 1)
Select Case ch
Case "."
Start = True
Case "?"
Start = True
Case "a" To "z"
If Start Then ch = UCase(ch): Start = False
Case "A" To "Z"
If Start Then Start = False Else ch = LCase(ch)
End Select
Mid(s, I, 1) = ch
Next
Cell.Value = s
Next
With ActiveSheet.Range("F:J")
    .Replace What:=" i ", Replacement:=" I ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False
    .Replace What:=" i~?", Replacement:=" I?", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False
End With
End Sub
Hope that helps. Also I assume you know that the code does not work after an exclamation point, as there is nothing in the code to denote that as the end of a sentence?
 
Upvote 0
hi again,

I have a header that I want to keep in upper case. How could I include something that would change it back to upper case in Range(A1:ZZ1)?
 
Upvote 0
At the end of the code put:

Code:
Range("A1:ZZ1").value = ucase(Range("A1:ZZ1").value)
Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,202,909
Messages
6,052,493
Members
444,587
Latest member
ezza59

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