How to Convert Text String to Proper Case with Exceptions in Excel?


Have you ever wondered if there is a way in Excel to automatically correct the case of strings in a sentence? It is possible to make it happen in Excel. This tutorial will help you understand how you can convert text strings to proper cases with exceptions in Excel. We can complete this task using the VBA application, as it can’t be completed directly in Excel. When the letters of the words are in the correct case according to grammar, they are called "proper case."

Converting Text String to Proper Case with Exceptions in Excel

Here, we will first create the list of exceptions, then insert the VBA module and run it to complete the task. Let us look at a simple procedure for converting strings to proper case with exception in Excel.

Step 1

Let us consider an Excel sheet where the data sheet is similar to the data shown in the below image.

Then right-click on the sheet name and select View Code to open the VBA application. Then click on "Insert" and select "Module".

Right click > View code > Inset > Module

Step 2

Type the following program code in the textbox, as shown in the image below.

Program

Sub CellsValueChange()
'Update By Nirmal
    Dim xSRg As Range
    Dim xDRg As Range
    Dim xPRg As Range
    Dim xSRgArea As Range
    Dim xRgVal As String
    Dim xAddress As String
    Dim I As Long
    Dim K As Long
    Dim KK As Long
    On Error Resume Next
    xAddress = Application.ActiveWindow.RangeSelection.Address
    Set xSRg = Application.InputBox("Original cells:", "Proper Text String", xAddress, , , , , 8)
    If xSRg Is Nothing Then Exit Sub
    Set xDRg = Application.InputBox("Output cells:", "Proper Text String", , , , , , 8)
    If xDRg Is Nothing Then Exit Sub
    Set xPRg = Application.InputBox("Cells to exclude:", "Proper Text String", , , , , , 8)
    If xPRg Is Nothing Then Exit Sub
    Set xDRg = xDRg(1)
    For I = 1 To xSRg.Areas.Count
        Set xSRgArea = xSRg.Areas.Item(I)
        For K = 1 To xSRgArea.Count
            xRgVal = xSRgArea(K).Value
            If Not IsNumeric(xRgVal) Then
                xRgVal = CorrectCase(xRgVal, xPRg)
                xDRg.Offset(KK).Value = xRgVal
            End If
            KK = KK + 1
        Next
    Next
End Sub
Function CorrectCase(ByVal xRgVal As String, ByVal xPRg As Range) As String
    Dim xArrWords As Variant
    Dim I As Integer
    Dim xPointer As Integer
    Dim xVal As String
    xPointer = 1
    xVal = xRgVal
    xArrWords = WordsOf(xRgVal)
    For I = 0 To UBound(xArrWords)
        xPointer = InStr(xPointer, " " & xVal, " " & xArrWords(I))
        Debug.Print xPointer
        Mid(xVal, xPointer) = CorrectCaseOneWord(CStr(xArrWords(I)), xPRg)
    Next I
    CorrectCase = xVal
End Function
Function WordsOf(xRgVal As String) As Variant
    Dim xDelimiters As Variant
    Dim xArrRtn As Variant
    xDelimiters = Array(",", ".", ";", ":", Chr(34), vbCr, vbLf)
    For Each xEachDelimiter In xDelimiters
        xRgVal = Application.WorksheetFunction.Substitute(xRgVal, xEachDelimiter, " ")
    Next xEachDelimiter
    xArrRtn = Split(Trim(xRgVal), " ")
    WordsOf = xArrRtn
End Function
Function CorrectCaseOneWord(xArrWord As String, xERg As Range) As String
    With xERg
        If IsError(Application.Match(xArrWord, .Cells, 0)) Then
            CorrectCaseOneWord = Application.Proper(xArrWord)
        Else
            CorrectCaseOneWord = Application.VLookup(xArrWord, .Cells, 1, 0)
        End If
    End With
End Function

Step 3

Now save the sheet as a macro-enabled workbook and click F5 to run the code. Then select the range of original data and click OK.

Step 4

Again, select the cells where you need the output and click OK.

Step 4

Finally, select the range of exceptions and click OK.

Conclusion

In this tutorial, we used a simple example to demonstrate how you can convert text strings to proper cases with exceptions in Excel.

Updated on: 06-Mar-2023

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements