Count The Number of Words in a Cell or Range of Cells in Excel - With User-Specified Delimiter / Separator - UDF

Add to Favorites
UDF to count the number of words in a cell or range with a user-specified delimiter. This means that you can tell the function what separates the words in your cells in Excel. For example, if you have words separated by a dash or a period etc., you can tell this function that that character separates your words and the words will then be counted correctly. This UDF counts words in a single cell and words in a range of cells in Excel.

The separator argument is optional. If you leave the separator argument blank, the function will default to use a space as the separator.

Where to install the macro:  Module

UDF to Count The Number of Words in a Cell or Range of Cells in Excel - With User-Specified Delimiter / Separator

Function COUNTWORDSC(rRange As Range, Optional separator As Variant) As Long

Dim rCell As Range
Dim Count As Long

If IsMissing(separator) Then
separator = " "
End If

For Each rCell In rRange

lCount = lCount + Len(Trim(rCell)) - Len(Replace(Trim(rCell), separator, "")) + 1

Next rCell

COUNTWORDSC = lCount

End Function










How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.


Excel Forum