I have been using a code that had been working fine but with the new data I'm using it has a flaw. Below is first the actual code I'm currently using followed by an explanation of the flaw I am now encountering.
Sub ConvertSelectionUpper_V4()
' hiker95, 08/27/2014, ME800718
Dim c As Range, s, i As Long, s2, h As String
Application.ScreenUpdating = False
With Selection
.Value = Evaluate("=if(len(" & .Address & "),substitute(proper(" & .Address & "),"" And "","" and ""),"""")")
.Columns.AutoFit
End With
For Each c In Selection
h = ""
If InStr(c, "'") Then
s = Split(Trim(c), " ")
For i = LBound(s) To UBound(s)
If InStr(s(i), "'") Then
s2 = Split(s(i), "'")
h = h & s2(0) & "'" & LCase(s2(1)) & " "
Else
h = h + s(i) & " "
End If
Next i
If Right(h, 1) = " " Then
h = Left(h, Len(h) - 1)
End If
c = h
End If
Next c
Application.ScreenUpdating = True
End Sub
The program above generally works fine. The problem in using the current code above is when a word in the sentence is actually composed of all Caps such as an MRI or CT scan at a hospital. In such a case, the MRI or CT are originally entered as all caps however when the above code is run, it does successfully and properly cap the first letter of each word in each cell however MRI consequently ends up as Mri and CT as Ct, which is the problem. If there was some way to adjust the program above or another way to cap the first letters of all the words without disturbing those such as MRI or CT and others that were already entered in as caps. I am hoping somebody has a solution for this.
Sub ConvertSelectionUpper_V4()
' hiker95, 08/27/2014, ME800718
Dim c As Range, s, i As Long, s2, h As String
Application.ScreenUpdating = False
With Selection
.Value = Evaluate("=if(len(" & .Address & "),substitute(proper(" & .Address & "),"" And "","" and ""),"""")")
.Columns.AutoFit
End With
For Each c In Selection
h = ""
If InStr(c, "'") Then
s = Split(Trim(c), " ")
For i = LBound(s) To UBound(s)
If InStr(s(i), "'") Then
s2 = Split(s(i), "'")
h = h & s2(0) & "'" & LCase(s2(1)) & " "
Else
h = h + s(i) & " "
End If
Next i
If Right(h, 1) = " " Then
h = Left(h, Len(h) - 1)
End If
c = h
End If
Next c
Application.ScreenUpdating = True
End Sub
The program above generally works fine. The problem in using the current code above is when a word in the sentence is actually composed of all Caps such as an MRI or CT scan at a hospital. In such a case, the MRI or CT are originally entered as all caps however when the above code is run, it does successfully and properly cap the first letter of each word in each cell however MRI consequently ends up as Mri and CT as Ct, which is the problem. If there was some way to adjust the program above or another way to cap the first letters of all the words without disturbing those such as MRI or CT and others that were already entered in as caps. I am hoping somebody has a solution for this.