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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I am giving the sample sheet below.
E1 to E8 are the pre defined data

A1 to A12 your input data
see formula in B1 which is copied down.

do you get the hang of it?

Book3
ABCDE
1venkatVenkatUSA
2rajaRajaTx
3ramanRamanOh
4ashokAshoka
5USAUSAan
6TxTxat
7OhOhin
8aaon
9anan
10atat
11inin
12onon
Sheet1
 
Upvote 0
I am actually looking for a VBA solution of some sort. The code I've used in the past selected a target range of cells and converted to uppercase, so no problem. It's much tougher to go the other way.

The problem is abbreviations and acromyns normally in uppercase are changed as well. Example: UPS becomes Ups, USA goes to Usa, FBI becomes Fbi, etc.

Some words I just want changed to lower case....a, an, at, in, of, etc.

I'm willing to create lists on a separate sheet of the text I want to be uppercase and lowercase. I just haven't figured out how to put it together in a sub or event procedure.

I'm thinking something like:
If in the uppercase list, make uppercase
Else if in the lowercase list, make lowercase
Else if NOT in the Upper or lower case lists, make proper case.

Thanks,

Ron
 
Upvote 0
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.
<table border=1 cellspacing=0>
<tr align="center" bgcolor=#A0A0A0><td width=25> <td width=125><b>A</b><td width=125><b>B</b><td width=125><b>C</b><td width=25><b>D</b><td width=25><b>E</b><td width=25><b>F</b><td width=25><b>G</b></tr>
<tr><td align="center" bgcolor=#A0A0A0><b>1</b><td align="left" bgcolor=#FFFFFF><u>Input</u><td align="left" bgcolor=#FFFFFF><u>Excel formula</u><td align="left" bgcolor=#FFFFFF><u>UDF</u><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>Case Specific Words</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>2</b><td align="left" bgcolor=#FFFFFF>THE<td align="left" bgcolor=#FFFFFF>The<td align="left" bgcolor=#FFFFFF>The<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>a</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>3</b><td align="left" bgcolor=#FFFFFF>UPS<td align="left" bgcolor=#FFFFFF>UPS<td align="left" bgcolor=#FFFFFF>UPS<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>AMA</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>4</b><td align="left" bgcolor=#FFFFFF>DRIVER<td align="left" bgcolor=#FFFFFF>dRiVeR<td align="left" bgcolor=#FFFFFF>dRiVeR<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>an</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>5</b><td align="left" bgcolor=#FFFFFF>MAY <td align="left" bgcolor=#FFFFFF>May <td align="left" bgcolor=#FFFFFF>May <td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>at</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>6</b><td align="left" bgcolor=#FFFFFF>DRIVE<td align="left" bgcolor=#FFFFFF>Drive<td align="left" bgcolor=#FFFFFF>Drive<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>dRiVeR</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>7</b><td align="left" bgcolor=#FFFFFF>IN<td align="left" bgcolor=#FFFFFF>in<td align="left" bgcolor=#FFFFFF>in<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>etc</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>8</b><td align="left" bgcolor=#FFFFFF>A<td align="left" bgcolor=#FFFFFF>a<td align="left" bgcolor=#FFFFFF>a<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>FBI</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>9</b><td align="left" bgcolor=#FFFFFF>TRUCK<td align="left" bgcolor=#FFFFFF>Truck<td align="left" bgcolor=#FFFFFF>Truck<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>in</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>10</b><td align="left" bgcolor=#FFFFFF>OF <td align="left" bgcolor=#FFFFFF>of <td align="left" bgcolor=#FFFFFF>of <td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>of</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>11</b><td align="left" bgcolor=#FFFFFF>STEEL<td align="left" bgcolor=#FFFFFF>Steel<td align="left" bgcolor=#FFFFFF>Steel<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>PD</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>12</b><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>TX</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>13</b><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>UPS</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>14</b><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF>USA</tr>
<tr><td align="center" bgcolor=#A0A0A0><b>15</b><td align="left" bgcolor=#FFFFFF>THE UPS DRIVER
MAY DRIVE IN
A TRUCK OF STEEL<td align="left" bgcolor=#FFFFFF>The Ups Driver
May Drive In
A Truck Of Steel<td align="left" bgcolor=#FFFFFF>The UPS dRiVeR
May Drive in
a Truck of Steel<td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF><td align="left" bgcolor=#FFFFFF></tr>
</table>
 
Last edited:
Upvote 0
Thank you for a very nice example. I like the use of the array and the delimiting characters. I'll work with this for a while and see what I can do with it. I appreciate your help.

Ron
 
Upvote 0
Hello,

I have attempted the UDF with the following exception range name, sorted...
=CorrectCase (textCell,excludeDB)

where excludeDB range is the following:
II
NY
NYTM

Input string= "NY TECH MEETUP (NYTM)"
Output = "Ny Tech Meetup (Nytm)"
Desired Output = "NY Tech Meetup (NYTM)"

However, it does not produce the desired results. Is there a possibility that the coding is slightly off?

Thanks, in advance, for any replies.
 
Upvote 0
Welcome to the MrExcel board!

Could you give a bit larger set of sample data (say 8-10) and desired output and include (if it is possible with your data) things like
- any text that might include apostrophes or other punctuation.
- examples where something other than one of your excludeDB values is enclosed in parentheses.
- examples where the original text is not all upper case.
 
Upvote 0
The UDF WordsOf strips punctuation. Parenthesis were not included when this was posted. Change WordsOf to

Code:
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

' ...
End Function
 
Upvote 0
Thanks for the replay Mike.

I'm a tad confused.
I've copied your updated lines (which seem to be same as original) as follows:


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


My result is:
#NAME?

What am I missing?
 
Upvote 0
.. (which seem to be same as original) as follows:


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


My result is:
#NAME?

What am I missing?
The original did not include the red text.

You need to have both of Mike's functions and use the CorrectCase function in your worksheet.

If that doesn't resolve your issue some samples as I asked for would be useful to me as I am investigating a slightly different method but would like to know what I have to deal with.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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