Convert to Proper Case with Exceptions

Capt_Ron

New Member
Joined
Sep 30, 2007
Messages
34
I have found plenty of code to convert text in a range from UPPERCASE to Proper Case but I want to be able to exclude from the conversion any text that matches text found in a list on another worksheet (USA, TX, PD, UPS, a, an, at, in , on, etc.).

Can this be done referencing the list as a named range or would it be best to simply refer to the list of words by Sheet.Range?

Anyone have an example I could follow?

Ron
 
This approach uses a single list of non-proper case words.
The column of Case Sprecific Words is a named range Words (=Sheet1!$G$2:$G$14).
The words in this range can have characters of mixed cases.
This range needs to be sorted ascending.

For single words, the spreadsheet formula (in B2) works:
=IF(ISNUMBER(MATCH(A2,Words,0)),LOOKUP(A2,Words,Words),PROPER(A2))

If you have cells with more than one word, the UDF CorrectCase works
=CorrectCase(A2,Words) is in C2

If needed, more punctuation can be added to the array in WordsOf.
Code:
Function CorrectCase(ByVal inputString As String, ByVal caseListRange As Range) As String
    Dim arrWords As Variant
    Dim i As Long, Pointer As Long
    On Error GoTo Halt
    Pointer = 1
    CorrectCase = inputString
    arrWords = WordsOf(inputString)
    
    For i = 0 To UBound(arrWords)
        Pointer = InStr(Pointer, inputString, arrWords(i))
        Mid(CorrectCase, Pointer) = CorrectCaseOneWord(CStr(arrWords(i)), caseListRange)
    Next i
    
Halt:
    On Error GoTo 0
End Function

Function WordsOf(inputString As String) As Variant
    Dim Delimiters As Variant, aDelimiter As Variant
    Dim arrResult As Variant
    
    Delimiters = Array(" ", ",", ".", ";", ":", Chr(34), vbCr, vbLf): Rem add to as needed
    
    For Each aDelimiter In Delimiters
        inputString = Application.Substitute(inputString, aDelimiter, Delimiters(0))
    Next aDelimiter
    
    arrResult = Split(inputString, CStr(Delimiters(0)))
    WordsOf = arrResult
End Function

Function CorrectCaseOneWord(inWord As String, caseListRange As Range) As String
    With caseListRange
        If IsError(Application.Match(inWord, .Cells, 0)) Then
            CorrectCaseOneWord = Application.Proper(inWord)
        Else
            CorrectCaseOneWord = Application.Lookup(inWord, .Cells)
        End If
    End With
End Function
In the below sheet,
The Excel formula in B2 and down is
=IF(ISNUMBER(MATCH(A2,Words,0)),LOOKUP(A2,Words,Words),PROPER(A2))

The UDF is in C2 downwards,
=CorrectCase(A2,Words)

Words is a named range for G2:G14.
Note that Words is sorted ascending.

















ABCDEFG
1InputExcel formulaUDFCase Specific Words
2THETheThea
3UPSUPSUPSAMA
4DRIVERdRiVeRdRiVeRan
5MAYMayMayat
6DRIVEDriveDrivedRiVeR
7INininetc
8AaaFBI
9TRUCKTruckTruckin
10OFofofof
11STEELSteelSteelPD
12TX
13UPS
14USA
15THE UPS DRIVER
MAY DRIVE IN
A TRUCK OF STEEL
The Ups Driver
May Drive In
A Truck Of Steel
The UPS dRiVeR
May Drive in
a Truck of Steel

<tbody>
</tbody>

Mike, this doesn't work for me.

If I want to change "GT 6# SAFFLOWER BAG" to "GT 6# Safflower Bag", I add GT to the case specific words list. I also have AWG included in that list, and the formula is grabbing AWG instead of GT, so the output is "AWG6# Safflower Bag". Why is it grabbing AWG instead of GT?

Thanks
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,215,417
Messages
6,124,787
Members
449,188
Latest member
Hoffk036

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