Home>Blogs>VBA>UDF to get the frequency of specific word in a Paragraph
Word Count in Excel
VBA

UDF to get the frequency of specific word in a Paragraph

I. Introduction

Data analysis – a critical aspect of modern business and organizations – provides valuable insights into various datasets, allowing informed decisions to be made. While Microsoft Excel boasts a multitude of built-in functions for data analysis, there may be times when these functions simply aren’t enough. That’s where User Defined Functions (UDFs) come into play.

 

UDFs are custom functions that can be created in Excel to perform tasks not covered by built-in functions. In this blog post, we’ll delve into the creation of a UDF in Excel VBA to calculate the frequency of a specific word in a given paragraph.

 

II. Understanding the Problem Statement

Suppose you have a vast dataset consisting of multiple paragraphs, and you wish to determine the frequency of a specific word within each paragraph. That’s where the UDF in Excel VBA comes in handy. The UDF takes two inputs – the paragraph and the specific word – and returns the frequency of the word in the paragraph.

Word Count in Excel
Word Count in Excel

III. Creating the UDF in Excel VBA

Follow these steps to create the UDF:

  • Open the VBA editor in Excel by pressing ALT + F11
  • In the editor, go to the module and define the UDF as follows:
 Function Word_Count(Complete_Text As String, Optional Specific_Word As String, Optional Case_Sensitive As Boolean)

 

The two inputs to the UDF are Complete_Text and Specific_Word, representing the paragraph and the specific word, respectively.

 

The function logic is as follows:

Option Explicit

Function Word_Count(Complete_Text As String, Optional Specific_Word As String, Optional Case_Sensitive As Boolean)

Application.Volatile

If Len(Specific_Word) = 0 Then

    Word_Count = (Len(Complete_Text) - Len(Application.WorksheetFunction.Substitute(Complete_Text, " ", ""))) + 1

Else

    If Case_Sensitive Then

        Word_Count = (Len(Complete_Text) - Len(Application.WorksheetFunction.Substitute(Complete_Text, Specific_Word, ""))) / Len(Specific_Word)

    Else

        Word_Count = (Len(Complete_Text) - Len(Application.WorksheetFunction.Substitute(UCase(Complete_Text), UCase(Specific_Word), ""))) / Len(Specific_Word)

    End If

End If

End Function

 

IV. Testing the UDF in Excel

To test the UDF, follow these steps:

  • In Excel, enter a paragraph in a cell and a specific word in another cell.
  • In another cell, enter the UDF with the two input parameters as follows:
 = Word_Count (A1, B1)

 

The UDF will return the frequency of the specific word in the paragraph.

V. Conclusion

In this blog post, we’ve explored the creation of a UDF in Excel VBA to calculate the frequency of a specific word in a given paragraph. UDFs offer a convenient solution when built-in functions in Excel are not enough, and they can save time and effort by automating repetitive tasks.

By being easy to modify and update, UDFs can adapt to changing requirements, making them an indispensable tool for data analysis in Excel. So, if you have a specific requirement not met by built-in functions in Excel, consider creating a UDF to meet your needs. With the proper understanding and knowledge, anyone can create a UDF and simplify data analysis in Excel.

Using this UDF you will be able to get the Total Words, Specific word count (Case Sensitive Check) and Specific word count (Non-Case Sensitive Check).

For example, we have below given paragraph, and we have the Total word count and Specific word count also-

Click here to download the Practice file-

Watch the step by step video tutorial:

PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com