How to Extract Capital Letters or Words Starting with Capital Letter from Text Strings in Excel


Microsoft Excel is a strong tool in the realm of data analysis and manipulation, with a vast range of operations and capabilities. Extraction of specific information from text strings is a common activity. For example, suppose you have a dataset with text entries and wish to extract all capital letters or terms that begin with a capital letter.

By the end of this tutorial, you will have a firm grasp on the procedures used to extract capital letters and words beginning with capital letters from text strings in Excel. This expertise will enable you to manage comparable duties in your data analysis projects, which will save you time and effort.

Extract Capital Letters or Words Starting with Capital Letter from Text Strings

Here we will first create a user-defined formula using VBA and then use the formula to complete the task. So let us see a simple process to know how you can extract capital letters or words starting with a capital letter from text strings in Excel.

Step 1

Consider an Excel sheet where the data in the sheet is similar to the below image.

First, right-click on the sheet name and select View code to open the VBA application.

Step 2

Then click on Insert and select Module, then copy the below code into the text box.

Example

Function ExtractCap(Txt As String) As String
   Application.Volatile
   Dim xRegEx As Object
   Set xRegEx = CreateObject("VBSCRIPT.REGEXP")
   xRegEx.Pattern = "[^A-Z]"
   xRegEx.Global = True
   ExtractCap = xRegEx.Replace(Txt, "")
   Set xRegEx = Nothing
End Function

Step 3

Then in Excel, click on an empty cell and enter the formula as =EXTRACTCAP(A2), and click enter to extract the capital letters for the first value. Then drag down using the autofill to complete the task.

Empty cell > Formula > Enter > Drag.

Note

If you want to extract the words, use the following code and use the formula "=StrExtract(A2)".

Example

Function StrExtract(Str As String) As String
   Application.Volatile
   Dim xStrList As Variant
   Dim xRet As String
   Dim I As Long
   If Len(Str) = 0 Then Exit Function
   xStrList = Split(Str, " ")
   If UBound(xStrList) >= 0 Then
      For I = 0 To UBound(xStrList)
         If xStrList(I) = StrConv(xStrList(I), vbProperCase) Then
            xRet = xRet & xStrList(I) & " "
         End If
     Next
     StrExtract = Left(xRet, Len(xRet) - 1)
   End If
End Function

Conclusion

In this tutorial, we have used a simple example to demonstrate how you can extract capital letters or words starting with a capital letter from text strings in Excel to highlight a particular set of data.

Updated on: 12-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements