Home>Blogs>VBA>User Defined Function for Sub-strings count from Excel Cell
UDF for Sub String Count
VBA

User Defined Function for Sub-strings count from Excel Cell

We often need the count of sub-strings from out text available in Excel. For example we have comma separated ticket number in a cell and we have to count how many ticket numbers are available in the cell. We have semicolon “;” separated email ids the how can get the count of email ids.

User Defined Function

User Defined Function
User Defined Function

  

Here we have given the solution of this problem with a User defined function in VBA.

Below is the VBA code:

  • Copy the below given code and go to Visual Basic Editor (Press Alt+F11)
  • Insert a module (Press Alt+I+M)
  • Paste this code in the module.
  • Save as the workbook as Macro enable workbook.

 

Option Explicit

Function Sub_String_Count(str As String, Separator As String)

Application.Volatile

If Len(str) = 0 Then
    Sub_String_Count = 0
Else
    Sub_String_Count = (Len(str) - Len(Replace(str, Separator, ""))) / Len(Separator)
    Sub_String_Count = Sub_String_Count + 1
End If

End Function

 

Click here to download Excel template.

 

Watch the step by step tutorial:

 

Visit our YouTube channel to learn step-by-step video tutorials

Youtube.com/@PKAnExcelExpert

 

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

Leave a Reply